opt:树形结构

This commit is contained in:
2026-01-20 17:39:09 +08:00
parent 3b84113d26
commit cd76de78bf

View File

@@ -158,7 +158,7 @@
startTime: '',
endTime: '',
isTop: true,
parentDetailId: 0
parentDetailId: undefined
})
const formData = ref(defaultForm())
@@ -173,7 +173,7 @@
parentDetailId: [
{
validator: async (_rule, value) => {
if (formData.value.isTop === false && !value) {
if (formData.value.isTop === false && (value === undefined || value === null || value === '')) {
return Promise.reject(new Error('请选择上级类目'))
}
return Promise.resolve()
@@ -183,12 +183,16 @@
]
}
const normalizeParentDetailId = (val) => {
if (val === undefined || val === null || val === '' || val === 0 || val === '0') {
return undefined
}
return val
}
const toTreeNode = (item = {}) => {
const id = item.detailId
const titleParts = []
if (item.modelName) titleParts.push(item.modelName)
if (item.modelDetail) titleParts.push(item.modelDetail)
const title = titleParts.length ? titleParts.join(' - ') : String(id || '')
const title = item.modelName ? item.modelName : String(id || '')
return {
title,
value: id,
@@ -241,7 +245,7 @@
const onTopChange = async () => {
if (formData.value.isTop === true) {
formData.value.parentDetailId = 0
formData.value.parentDetailId = undefined
parentTreeData.value = []
} else {
// 顶级新增同级parentId = 0加载根节点
@@ -253,11 +257,12 @@
const onOpen = async (payload = {}) => {
open.value = true
formData.value = Object.assign(defaultForm(), cloneDeep(payload || {}))
if (typeof formData.value.isTop !== 'boolean') {
formData.value.isTop = !formData.value.parentDetailId
}
if (formData.value.isTop) {
formData.value.parentDetailId = 0
formData.value.parentDetailId = normalizeParentDetailId(formData.value.parentDetailId)
if (formData.value.parentDetailId === undefined) {
// 同级新增且传入 parentDetailId 为 0/'0' 时也视为顶级
formData.value.isTop = true
} else if (typeof formData.value.isTop !== 'boolean') {
formData.value.isTop = false
}
if (formData.value.isTop === false) {
await loadRootTree()