zhangwencui
2 天以前 6762e8cfe82638f6883640e1fbba39c4512550d2
新增物料时,物料分类下拉选择时做父子层级展示
已修改1个文件
25 ■■■■ 文件已修改
src/views/basicData/mdm/data.ts 25 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/views/basicData/mdm/data.ts
@@ -18,6 +18,24 @@
let unitListCache: any[] | null = null;
let unitListPromise: Promise<any[]> | null = null;
/** 将扁平分类列表转为 TreeSelect 所需的树形结构 */
function buildCategoryTree(list: any[]): any[] {
  const map = new Map<number, any>();
  const roots: any[] = [];
  for (const item of list) {
    map.set(item.id, { label: item.name, value: item.id, children: [] });
  }
  for (const item of list) {
    const node = map.get(item.id)!;
    if (item.parentId && map.has(item.parentId)) {
      map.get(item.parentId)!.children.push(node);
    } else {
      roots.push(node);
    }
  }
  return roots;
}
/** 获取单位列表(带缓存) */
async function getUnitListCached() {
  if (unitListCache) {
@@ -80,19 +98,18 @@
      rules: "required",
    },
    {
      component: "ApiSelect",
      component: "ApiTreeSelect",
      fieldName: "categoryId",
      label: "物料分类",
      rules: "selectRequired",
      componentProps: {
        placeholder: "请选择物料分类",
        allowClear: true,
        treeDefaultExpandAll: true,
        api: async () => {
          const res = await getItemTypeSimpleList();
          return res || [];
          return buildCategoryTree(res || []);
        },
        labelField: "name",
        valueField: "id",
      },
    },
    {