Skip to content

导出

FlexTreeMnaagerFlexTree支持将树导出为JsonList格式。

下面以下面的树为例,说明如何导出树。

  • Root
    • A
    • B
    • C

toJson

FlexTreeManagerFlexTreeFlexTreeNode均支持toJson方法,用于将树导出为Json格式。

ts
toJson(
    options?: FlexTreeExportJsonOptions<Fields, KeyFields>
): FlexTreeExportJsonFormat<Fields, KeyFields>

interface FlexTreeExportJsonOptions<
    Fields extends Record<string, any> = object,
    KeyFields extends CustomTreeKeyFields = DefaultTreeKeyFields,
> {
    childrenField?: string
    level?: number // 限定导出的级别
    fields?: (keyof IFlexTreeNode<Fields, KeyFields>)[]
    includeKeyFields?: boolean
    countField?: string // 指定后附加后代数量字段
}
  • 参数
参数 类型 默认 描述
options FlexTreeExportJsonOptions 导出选项
options.childrenField string 'children' 子节点字段名
options.level number 限定导出的级别
options.fields (keyof IFlexTreeNode<Fields, KeyFields>)[] 导出的字段
options.includeKeyFields boolean false 是否导出关键字段
options.countField string 后代数量字段名
  • 返回

返回一个JSON对象,子节点字段名默认为children,可以通过options.childrenField参数自定义。

ts
type FlexTreeExportJsonFormat<
  Fields extends Record<string, any> = object,
  KeyFields extends CustomTreeKeyFields = DefaultTreeKeyFields,
  TreeNode extends IFlexTreeNode<Fields, KeyFields> = IFlexTreeNode<Fields, KeyFields>,
  NodeId = NonUndefined<KeyFields["id"]>[1],
> = TreeNode & {
  children?: FlexTreeExportJsonFormat<Fields, KeyFields, TreeNode, NodeId>[];
};
  • 示例
ts
import type { FlexTreeOptions, IFlexTreeNode } from "flextree";
import { FlexTreeManager, FlexTree, FlexTreeVerifyError } from "flextree";

import SqliteAdapter from "flextree-sqlite-adapter";
const sqliteDriver = new SqliteAdapter();
await sqliteDriver.open();

const tree = new FlexTree("tree", {
  adapter: sqliteDriver,
});
await tree.load();

tree.toJson();

输出的结果如下:

json
{
  "id": 1,
  "name": "root",
  "children": [
    {
      "id": 2,
      "name": "A",
      "children": [
        { "id": 3, "name": "A1" },
        { "id": 4, "name": "A2" },
        { "id": 5, "name": "A3" }
      ]
    },
    {
      "id": 6,
      "name": "B",
      "children": [
        { "id": 7, "name": "B1" },
        { "id": 8, "name": "B2" },
        { "id": 9, "name": "B3" }
      ]
    },
    {
      "id": 10,
      "name": "C",
      "children": [
        { "id": 11, "name": "C1" },
        { "id": 12, "name": "C2" },
        { "id": 13, "name": "C3" }
      ]
    }
  ]
}
  • 说明

    • toJson方法会将树导出为Json格式,可以通过options.level参数限定导出的级别。
    • toJson方法可以在FlexTreeFlexTreeNode中调用。
    • 可以通过options.fields参数指定导出的字段
    • 默认情况下不会导出leftValuerightValue字段,可以通过options.includeKeyFields参数指定是否导出关键字段
    • 可以通过options.childrenField参数指定子节点字段名

countField 后代数量

指定options.countField后,每个节点会附加一个表示后代节点数量的字段(叶子节点为0):

ts
await manager.toJson({ countField: "count" })
// { "id": 1, "name": "root", "count": 12, "children": [
//    { "id": 2, "name": "A", "count": 3, "children": [...] },
//    ...
// ] }
  • 数量按(rightValue - leftValue - 1) / 2计算,恒为全量后代数——不受level截断影响(level: 2 导出时根节点的数量仍包含深层后代)
  • countFieldid同地位:指定fields过滤时照样附加,不受includeKeyFields控制
  • 与节点已有字段重名时会抛出FlexTreeError
  • 启用回收站时为可见口径:默认视角下数量不含已被回收的节点(与导出内容一致);includeRecyclebin: true时为物理全集数量

toList

FlexTreeManagerFlexTreeFlexTreeNode均支持toList方法,用于将树导出为带pid字段的list节点数组格式。

ts
toList(
    options?: FlexTreeExportListOptions<Fields, KeyFields>
): FlexTreeExportListFormat<Fields, KeyFields>
interface FlexTreeExportListOptions<
    Fields extends Record<string, any> = object,
    KeyFields extends CustomTreeKeyFields = DefaultTreeKeyFields,
> {
    pidField?: string
    level?: number // 限定导出的级别
    fields?: (keyof IFlexTreeNode<Fields, KeyFields>)[]
    includeKeyFields?: boolean
    countField?: string // 指定后附加后代数量字段
}
  • 参数
参数 类型 默认 描述
options FlexTreeExportListOptions 导出选项
options.pidField string 'pid' 父节点字段名
options.level number 限定导出的级别
options.fields (keyof IFlexTreeNode<Fields, KeyFields>)[] 导出的字段
options.includeKeyFields boolean false 是否导出关键字段
options.countField string 后代数量字段名
  • 返回

返回一个list节点数组,父节点字段名默认为pid,可以通过options.pidField参数自定义。

ts
export type FlexTreeExportListFormat<
  Fields extends Record<string, any> = object,
  KeyFields extends CustomTreeKeyFields = DefaultTreeKeyFields,
  TreeNode extends IFlexTreeNode<Fields, KeyFields> = IFlexTreeNode<Fields, KeyFields>,
  NodeId = NonUndefined<KeyFields["id"]>[1],
  OPTIONS extends FlexTreeExportListOptions<Fields, KeyFields> = FlexTreeExportListOptions<
    Fields,
    KeyFields
  >,
> = ((OPTIONS["fields"] extends string[]
  ? Extract<TreeNode, OPTIONS["fields"][number]>
  : TreeNode) & { [P in OPTIONS["pidField"] & string]: NodeId })[];
  • 示例
ts
import type { FlexTreeOptions, IFlexTreeNode } from "flextree";
import { FlexTreeManager, FlexTree, FlexTreeVerifyError } from "flextree";
import SqliteAdapter from "flextree-sqlite-adapter";
const sqliteDriver = new SqliteAdapter();
await sqliteDriver.open();

const tree = new FlexTree("tree", {
  adapter: sqliteDriver,
});
await tree.load();

tree.toList();

输出的结果如下:

json
[
  { "id": 1, "name": "root", "pid": 0 },
  { "id": 2, "name": "A", "pid": 1 },
  { "id": 3, "name": "A1", "pid": 2 },
  { "id": 4, "name": "A2", "pid": 2 },
  { "id": 5, "name": "A3", "pid": 2 },
  { "id": 6, "name": "B", "pid": 1 },
  { "id": 7, "name": "B1", "pid": 6 },
  { "id": 8, "name": "B2", "pid": 6 },
  { "id": 9, "name": "B3", "pid": 6 },
  { "id": 10, "name": "C", "pid": 1 },
  { "id": 11, "name": "C1", "pid": 10 },
  { "id": 12, "name": "C2", "pid": 10 },
  { "id": 13, "name": "C3", "pid": 10 }
]

提示

toListtoJson方法均支持level参数,用于限定导出的级别。可以在FlexTreeFlexTreeNode中调用。countField的语义见上文 countField 后代数量

getTree

FlexTreeManager提供了getTree方法,用于基于当前管理器构建一个内存中的FlexTree对象,从而可以使用FlexTree提供的丰富API(如getByPathfindtoJsontoList等)。

ts
getTree(options?: FlexTreeOptions): FlexTree<Fields, KeyFields>
  • 示例
ts
const tree = manager.getTree()
await tree.load()           // FlexTree 需手动加载后才能访问
tree.getByPath('/A/A-1')

提示

FlexTreeManager还提供了toJsontoList两个异步便捷方法,内部会自动通过getTree构建并加载树后再导出:

ts
await manager.toJson()
await manager.toList()