const treeData = ref([])
let specId = 0 // 用于生成唯一ID
数据结构示例:
[
{
id: 'spec-1',
label: '颜色',
type: 'spec',
children: [
{ id: 'item-1', label: '红色', type: 'item' },
{ id: 'item-2', label: '蓝色', type: 'item' }
]
}
]
const addSpec = () => {
const spec = {
id: `spec-${specId++}`,
label: '',
type: 'spec',
children: []
}
treeData.value.push(spec)
}
const addSpecItem = (spec) => {
const item = {
id: `item-${specId++}`,
label: '',
type: 'item'
}
spec.children.push(item)
}
const removeSpec = (item, specIndex, itemIndex) => {
treeData.value[specIndex].children.splice(itemIndex, 1)
}
.spec-main {
display: flex;
min-height: 40px;
}
.spec-name {
width: 250px;
min-width: 250px;
position: sticky;
left: 0;
}
.spec-items-container {
flex: 1;
border-left: 1px solid #eee;
padding-left: 20px;
overflow: hidden;
}
.add-item-btn {
width: 24px;
height: 24px;
border: 1px solid #dcdfe6;
border-radius: 4px;
transition: all 0.3s;
}
.add-item-btn:hover {
color: #409eff;
border-color: #c6e2ff;
background-color: #ecf5ff;
}
watch(treeData, (newValue) => {
console.log("当前规格数据:", JSON.parse(JSON.stringify(newValue)))
}, { deep: true })
const logSpecData = () => {
console.log("用户输入更新:", JSON.parse(JSON.stringify(treeData.value)))
}
添加规格