fix: normalizeTreeData
This commit is contained in:
parent
136a17ce8c
commit
7d1bfd33ff
1 changed files with 23 additions and 2 deletions
|
|
@ -13,6 +13,27 @@ import type {
|
|||
|
||||
const { showLoader, hideLoader } = useCounterMixin();
|
||||
|
||||
/**
|
||||
* ฟังก์ชันแปลงข้อมูลโครงสร้างให้มี children array สำหรับทุกโหนด
|
||||
* เพื่อป้องกันปัญหา q-tree เมื่อ children เป็น undefined
|
||||
*/
|
||||
function normalizeTreeData(nodes: DataStructureTree[]): DataStructureTree[] {
|
||||
return nodes.map((node) => {
|
||||
// Set children to [] for orgLevel: 4, otherwise recurse if children exist
|
||||
const normalizedChildren =
|
||||
node.orgLevel === 4
|
||||
? []
|
||||
: node.children
|
||||
? normalizeTreeData(node.children)
|
||||
: node.children;
|
||||
|
||||
return {
|
||||
...node,
|
||||
children: normalizedChildren,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export const useStructureTree = defineStore("structureTree", () => {
|
||||
const activeId = ref<string>("");
|
||||
const dataStore = ref<{ [key: string]: DataStructureTree[] }>({});
|
||||
|
|
@ -27,11 +48,11 @@ export const useStructureTree = defineStore("structureTree", () => {
|
|||
*/
|
||||
async function fetchStructureTree(sysKey: string, isLoad: boolean = false) {
|
||||
if (dataStore.value[sysKey]) {
|
||||
return dataStore.value[sysKey] || [];
|
||||
return normalizeTreeData(dataStore.value[sysKey]) || [];
|
||||
} else {
|
||||
activeId.value === "" && (await fetchActive());
|
||||
const data = await fetchData(sysKey, isLoad);
|
||||
return data || [];
|
||||
return normalizeTreeData(data || []);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue