-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
130 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export const NODE_DEPTH_FLAG = '__node_depth' | ||
|
||
export function sortByTree(data: any[], pkColumnName: string, parentPkColumnName?: string) { | ||
if (parentPkColumnName === undefined) | ||
return data | ||
|
||
return getTreeData(data, undefined, pkColumnName, parentPkColumnName, 0) | ||
} | ||
|
||
function getTreeData(data: any[], parentPk: any, pkColumnName: string, parentPkColumnName: string, depth: number): any[] { | ||
const treeData = [] | ||
const nodeData = data.filter(item => item[parentPkColumnName] === parentPk) | ||
for (const node of nodeData) { | ||
node[NODE_DEPTH_FLAG] = depth | ||
treeData.push(node) | ||
const children = getTreeData(data, node[pkColumnName], pkColumnName, parentPkColumnName, depth + 1) | ||
if (children.length > 0) | ||
treeData.push(...children) | ||
} | ||
return treeData | ||
} | ||
|
||
export function filterTreeDataPks(filterPks: any[], records: { [key: string]: any }[], pkColumnName: string, parentPkColumnName?: string): any[] { | ||
if (parentPkColumnName === undefined) | ||
return filterPks | ||
|
||
const pksWithChildren: any[] = filterPks.slice() | ||
|
||
pksWithChildren.forEach((pk) => { | ||
const childrenPks = records.filter(record => record[parentPkColumnName!] === pk).map(record => record[pkColumnName]) | ||
if (childrenPks.length > 0) { | ||
pksWithChildren.push(...childrenPks) | ||
pksWithChildren.push(...filterTreeDataPks(childrenPks, records, pkColumnName, parentPkColumnName)) | ||
} | ||
}) | ||
return pksWithChildren | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters