Last active
July 10, 2024 07:09
-
-
Save Moln/44c9487536ec928bb1f3 to your computer and use it in GitHub Desktop.
数据表 转树型格式
This file contains hidden or 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
| function toTreeData(rows, key = 'role_id', childKey = 'items', dataMapCallable = null) | |
| { | |
| var rootId = 0; | |
| var data = {0 : { [childKey]: [] } }; | |
| rows.forEach(function (row) { | |
| if (!data[row[key]]) { | |
| data[row[key]] = { [childKey]: [] }; | |
| } | |
| data[row[key]] = dataMapCallable(row, data[row[key]]); | |
| data[row['parent']][childKey].push(data[row[key]]); | |
| }) | |
| return data; | |
| } | |
| var data = [ | |
| {role_id:1, name: 'admin', parent: 0}, | |
| {role_id:2, name: 'm1', parent: 1}, | |
| {role_id:3, name: 'm2', parent: 1}, | |
| {role_id:4, name: 'G', parent: 0}, | |
| ]; | |
| toTreeData(data, 'role_id', 'items', (row, data) => { return {...row, ...data}; }) | |
| /* | |
| { | |
| "0": { | |
| "items": [ | |
| { | |
| "role_id": 1, | |
| "name": "admin", | |
| "parent": 0, | |
| "items": [ | |
| { | |
| "role_id": 2, | |
| "name": "m1", | |
| "parent": 1, | |
| "items": [] | |
| }, | |
| { | |
| "role_id": 3, | |
| "name": "m2", | |
| "parent": 1, | |
| "items": [] | |
| } | |
| ] | |
| }, | |
| { | |
| "role_id": 4, | |
| "name": "G", | |
| "parent": 0, | |
| "items": [] | |
| } | |
| ] | |
| }, | |
| "1": { | |
| "role_id": 1, | |
| "name": "admin", | |
| "parent": 0, | |
| "items": [ | |
| { | |
| "role_id": 2, | |
| "name": "m1", | |
| "parent": 1, | |
| "items": [] | |
| }, | |
| { | |
| "role_id": 3, | |
| "name": "m2", | |
| "parent": 1, | |
| "items": [] | |
| } | |
| ] | |
| }, | |
| "2": { | |
| "role_id": 2, | |
| "name": "m1", | |
| "parent": 1, | |
| "items": [] | |
| }, | |
| "3": { | |
| "role_id": 3, | |
| "name": "m2", | |
| "parent": 1, | |
| "items": [] | |
| }, | |
| "4": { | |
| "role_id": 4, | |
| "name": "G", | |
| "parent": 0, | |
| "items": [] | |
| } | |
| } | |
| */ |
This file contains hidden or 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
| <?php | |
| function toTreeData($rows, $key = 'role_id', $childKey = 'items', callable $dataMapCallable = null) | |
| { | |
| $rootId = 0; | |
| $data = array($rootId => array($childKey => array())); | |
| foreach ($rows as $row) { | |
| if (!isset($data[$row[$key]])) { | |
| $data[$row[$key]] = []; | |
| } | |
| $data[$row[$key]] = $dataMapCallable($row, $data[$row[$key]]); | |
| $data[$row['parent']][$childKey][] = &$data[$row[$key]]; | |
| } | |
| return $data; | |
| } | |
| $data = [ | |
| [ | |
| 'role_id' => 1, | |
| 'name' => 'Admin', | |
| 'parent' => 0 | |
| ], | |
| [ | |
| 'role_id' => 2, | |
| 'name' => 'Member 1', | |
| 'parent' => 1 | |
| ], | |
| [ | |
| 'role_id' => 3, | |
| 'name' => 'Member 2', | |
| 'parent' => 1 | |
| ], | |
| [ | |
| 'role_id' => 4, | |
| 'name' => 'Guest', | |
| 'parent' => 0 | |
| ], | |
| ]; | |
| $dataMapCallable = function ($row, $data) { | |
| return $data + array( | |
| 'role_id' => $row['role_id'], | |
| 'text' => $row['name'], | |
| 'parent_id' => $row['parent'], | |
| ); | |
| }; | |
| $key = 'role_id'; | |
| $childKey = 'children'; | |
| print_r(toTreeData($data, $key, $childKey, $dataMapCallable)[0][$childKey]); |
This file contains hidden or 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
| type ToTreeDataOptionsArg<T, T2> = { | |
| key?: string, | |
| childKey?: string, | |
| parentKey?: string, | |
| dataTransfer?: (row: T, raw: T2) => T2 | |
| } | |
| function toTreeData<T extends Record<string, any>, T2 = T>(rows: T[], options: ToTreeDataOptionsArg<T, T2> = {}): Record<string, T2> { | |
| const { | |
| key = 'id', | |
| childKey = 'children', | |
| parentKey = 'parent', | |
| dataTransfer = (row) => row | |
| } = options | |
| const data: Record<string, T2> = { | |
| "0" : {[childKey]: []} as T2 | |
| }; | |
| rows.forEach(function (row) { | |
| if (!data[row[key]]) { | |
| data[row[key]] = {[childKey]: []} as T2; | |
| } | |
| data[row[key]] = dataTransfer(row, data[row[key]]) as T2; | |
| data[row[parentKey]][childKey].push(data[row[key]]); | |
| }) | |
| return data; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment