|
1 |
| -# treeSub |
2 |
| - |
3 |
| -get sub tree by id, like `{ id, parentId, children: [] }` |
4 |
| - |
5 |
| -## 调用 |
| 1 | +# 剪取子树 |
6 | 2 |
|
7 | 3 | ```js
|
8 | 4 | treeSub(tree, id, [options])
|
9 | 5 | ```
|
10 | 6 |
|
| 7 | +根据主键从树中剪取一棵子树, 如:`{ id, parentId, children: [] }`。 |
| 8 | + |
| 9 | +## 参数 |
| 10 | + |
| 11 | +| 参数 | 类型 | 默认 | 说明 | |
| 12 | +| --------- | -------- | ---- | ------------------------------------------- | |
| 13 | +| `tree` | `tree` | | [树状结构数据](./param.md#tree) | |
| 14 | +| `id` | `any` | | [主键](./param.md#id)的值,如:`chartIndex` | |
| 15 | +| `options` | `object` | | [配置选项](./param.md#options) | |
| 16 | + |
11 | 17 | ## 返回
|
12 | 18 |
|
13 |
| -tree |
| 19 | +| 参数 | 类型 | 说明 | |
| 20 | +| ---- | -------- | ---------------------------------- | |
| 21 | +| * | `object` | 剪取下的子树,注意其类型为**对象** | |
| 22 | + |
| 23 | +## 示例 |
| 24 | + |
| 25 | +::: code-group |
| 26 | +```js [调用] |
| 27 | +import { treeSub } from '@axolo/tree-array' |
| 28 | + |
| 29 | +const tree = [{ |
| 30 | + id: 'home', |
| 31 | + path: '/home', |
| 32 | + parentId: null |
| 33 | +}, { |
| 34 | + id: 'chart', |
| 35 | + path: '/chart', |
| 36 | + parentId: null, |
| 37 | + children: [{ |
| 38 | + id: 'chartIndex', |
| 39 | + path: '/chart/index', |
| 40 | + parentId: 'chart', |
| 41 | + children: [{ |
| 42 | + id: 'chartIndexTop', |
| 43 | + path: '/chart/index/top', |
| 44 | + parentId: 'chartIndex' |
| 45 | + }, { |
| 46 | + id: 'chartIndexActive', |
| 47 | + path: '/chart/index/active', |
| 48 | + parentId: 'chartIndex', |
| 49 | + children: [{ |
| 50 | + id: 'chartIndexActiveMy', |
| 51 | + path: '/chart/index/active/my', |
| 52 | + parentId: 'chartIndexActive' |
| 53 | + }] |
| 54 | + }] |
| 55 | + }, { |
| 56 | + id: 'chartReview', |
| 57 | + path: '/chart/review', |
| 58 | + parentId: 'chart' |
| 59 | + }, { |
| 60 | + id: 'chartProject', |
| 61 | + path: '/chart/project', |
| 62 | + parentId: 'chart', |
| 63 | + children: [{ |
| 64 | + id: 'chartProjectYou', |
| 65 | + path: '/chart/project/you', |
| 66 | + parentId: 'chartProject', |
| 67 | + test: true |
| 68 | + }, { |
| 69 | + id: 'chartProjectMy', |
| 70 | + path: '/chart/project/my', |
| 71 | + parentId: 'chartProject', |
| 72 | + children: [{ |
| 73 | + id: 'chartProjectMyOne', |
| 74 | + path: '/chart/project/my/one', |
| 75 | + parentId: 'chartProjectMy' |
| 76 | + }, { |
| 77 | + id: 'chartProjectMyTwo', |
| 78 | + path: '/chart/project/my/two', |
| 79 | + parentId: 'chartProjectMy' |
| 80 | + }] |
| 81 | + }] |
| 82 | + }] |
| 83 | +}, { |
| 84 | + id: 'smile', |
| 85 | + path: '/smile', |
| 86 | + parentId: null, |
| 87 | + children: [{ |
| 88 | + id: 'smileIndex', |
| 89 | + path: '/smile/index', |
| 90 | + parentId: 'smile', |
| 91 | + test: true |
| 92 | + }] |
| 93 | +}] |
| 94 | + |
| 95 | +treeSub(tree, 'chartIndex') // object of sub tree |
| 96 | +``` |
| 97 | + |
| 98 | +```json [结果] |
| 99 | +{ |
| 100 | + "id": "chartIndex", |
| 101 | + "path": "/chart/index", |
| 102 | + "parentId": "chart", |
| 103 | + "children": [{ |
| 104 | + "id": "chartIndexTop", |
| 105 | + "path": "/chart/index/top", |
| 106 | + "parentId": "chartIndex" |
| 107 | + }, { |
| 108 | + "id": "chartIndexActive", |
| 109 | + "path": "/chart/index/active", |
| 110 | + "parentId": "chartIndex", |
| 111 | + "children": [{ |
| 112 | + "id": "chartIndexActiveMy", |
| 113 | + "path": "/chart/index/active/my", |
| 114 | + "parentId": "chartIndexActive" |
| 115 | + }] |
| 116 | + }] |
| 117 | +} |
| 118 | +``` |
| 119 | +::: |
0 commit comments