<el-tree-select
v-model="commodityInfoForm.categoryCode"
lazy
node-key="catCode"
:props="{ label: 'catName', isLeaf: 'leaf' }"
:cache-data="treeCacheData"
:load="loadTree"
check-strictly
:default-expanded-keys="defaultExpandedKeys"
/>
// 动态生成 cache-data
const treeCacheData = computed(() => {
if (commodityInfoForm.value.categoryCode && commodityInfoForm.value.categoryName) {
return [
{
catName: commodityInfoForm.value.categoryName,
catCode: commodityInfoForm.value.categoryCode,
},
];
}
return [];
});
const loadTree = (node: Node, resolve: (data: CommodityCatTree[]) => void) => {
if (node.level === 0) {
getCommodityCat({ parentCode: "0" })
.then((res: any) => {
if (res.result && res.result.length) {
defaultExpandedKeys.value = [res.result[0].catCode];
return resolve(res.result);
}
return resolve([]);
})
.catch(() => {
return resolve([]);
});
} else {
getCommodityCat({ parentCode: node.data.catCode })
.then((res: any) => {
return resolve(res.result || []);
})
.catch(() => {
return resolve([]);
});
}
};