Options
All
  • Public
  • Public/Protected
  • All
Menu

@zhengxs/js.tree - v0.5.0

Index

Type aliases

Exporter

Exporter<T>: (nodes: Record<ID, T[]>, result: ParseResult<T>) => T[] | null | undefined

数据导出

Type parameters

  • T

Type declaration

    • (nodes: Record<ID, T[]>, result: ParseResult<T>): T[] | null | undefined
    • Parameters

      Returns T[] | null | undefined

ID

ID: string | number

ID类型

None

None: null | undefined

空数据

ParseOptions

ParseOptions<T, S>: { childrenKey?: string; idKey?: string; insert?: (siblings: S[], node: S) => void; parentKey?: string; transform?: Transform<T, S> }

Type parameters

  • T

  • S

Type declaration

  • Optional childrenKey?: string

    支持自定义 children 的属性名

  • Optional idKey?: string

    id 的属性名

  • Optional insert?: (siblings: S[], node: S) => void

    允许外部接管插入行为

      • (siblings: S[], node: S): void
      • Parameters

        • siblings: S[]
        • node: S

        Returns void

  • Optional parentKey?: string

    parentId 的属性名

  • Optional transform?: Transform<T, S>

    允许外部转换数据

ParseResult

ParseResult<S>: { childNodes: Record<ID, S[]>; childrenKey: string; idKey: string; nodes: Record<ID, S>; parentKey: string }

Type parameters

  • S

Type declaration

  • childNodes: Record<ID, S[]>

    包含所有节点关系的对象

  • childrenKey: string

    支持自定义 children 的属性名

  • idKey: string

    id 的属性名

  • nodes: Record<ID, S>

    包含所有节点的对象

  • parentKey: string

    parentId 的属性名

Predicate

Predicate<T>: (data: T) => boolean

filter 回调

Type parameters

  • T

Type declaration

    • (data: T): boolean
    • Parameters

      • data: T

      Returns boolean

Row

Row: Record<string | number | symbol, unknown>

普通对象

Transform

Transform<T, S>: (data: T, index: number) => S | None

节点转换

Type parameters

  • T

  • S

Type declaration

    • (data: T, index: number): S | None
    • Parameters

      • data: T
      • index: number

      Returns S | None

Variables

Const CHILDREN_KEY

CHILDREN_KEY: "children" = 'children'

子级的属性名

Const ID_KEY

ID_KEY: "id" = 'id'

id 的属性名

Const PARENT_ID_KEY

PARENT_ID_KEY: "parentId" = 'parentId'

parentId 的属性名

Const ROOT_ID

ROOT_ID: "__root__" = '__root__'

根ID

Const version

version: "__VERSION__" = '__VERSION__'

发布版本

Functions

each

  • each<T>(data: T[], callback: (data: T, index: number, parents: T[]) => boolean | void, childrenKey?: string): T[]
  • 遍历所有节点

    Type parameters

    Parameters

    • data: T[]

      数结构数据

    • callback: (data: T, index: number, parents: T[]) => boolean | void

      处理回调,返回 true 将跳过子级的遍历操作

        • (data: T, index: number, parents: T[]): boolean | void
        • Parameters

          • data: T
          • index: number
          • parents: T[]

          Returns boolean | void

    • childrenKey: string = ...

      自定义子节点属性名称

    Returns T[]

exclude

  • exclude<T>(data: T[], callback: (data: T, index: number, parents: T[]) => boolean, childrenKey?: string): T[]
  • 排除某些数据

    Type parameters

    Parameters

    • data: T[]

      数结构数据

    • callback: (data: T, index: number, parents: T[]) => boolean

      处理回调,返回 true 的数据将被过滤掉

        • (data: T, index: number, parents: T[]): boolean
        • Parameters

          • data: T
          • index: number
          • parents: T[]

          Returns boolean

    • childrenKey: string = ...

      自定义子节点属性名称

    Returns T[]

exporter

  • 数据导出,允许外部自定义根节点

    Type parameters

    • T

    Parameters

    Returns T[]

filter

  • filter<T>(data: T[], callback: (data: T, index: number, parents: T[]) => boolean, childrenKey?: string): T[]
  • 类数组的 filter 方法

    Type parameters

    Parameters

    • data: T[]

      数结构数据

    • callback: (data: T, index: number, parents: T[]) => boolean

      处理回调,注意:如果返回的对象子级不存在将不进行递归操作

        • (data: T, index: number, parents: T[]): boolean
        • Parameters

          • data: T
          • index: number
          • parents: T[]

          Returns boolean

    • childrenKey: string = ...

      自定义子节点属性名称

    Returns T[]

map

  • map<T, U>(data: T[], callback: (data: T, index: number, parents: T[]) => U, childrenKey?: string): U[]
  • 类数组的 map 方法

    Type parameters

    Parameters

    • data: T[]

      数结构数据

    • callback: (data: T, index: number, parents: T[]) => U

      处理回调,注意:如果返回的对象子级不存在将不进行递归操作

        • (data: T, index: number, parents: T[]): U
        • Parameters

          • data: T
          • index: number
          • parents: T[]

          Returns U

    • childrenKey: string = ...

      自定义子节点属性名称

    Returns U[]

parse

  • 方便外部二次封装

    如:封装一个类 jQuery 的 API 工具,方便查找节点

    Type parameters

    Parameters

    • data: T[]

      行数据

    • options: ParseOptions<T, S> = {}

      配置项

    Returns ParseResult<S>

Const repairWith

  • repairWith<T, U>(list: T[], config: RepairWithConfig<T, U>): U[]
  • 根据列表修复缺失的节点数据

    Type parameters

    Parameters

    • list: T[]

      不完整列表数据

    • config: RepairWithConfig<T, U>

      配置参数

    Returns U[]

    已修复好的数结构

toRows

  • toRows<T, U>(data: T[], childrenKey?: string): U[]
  • 树转行

    Type parameters

    Parameters

    • data: T[]

      树结构数据

    • childrenKey: string = ...

      children 属性名

    Returns U[]

toTree

  • 行转树

    example
    默认
    toTree([
      { id: 1, parentId: null },
      { id: 2, parentId: null },
      { id: 3, parentId: 1 },
    ])
    // ->
    [
      {
        id: 1,
        parentId: null,
        children: [
          { id: 3, parentId: 1, children: [] }
        ]
      },
      { id: 2, parentId: null, children: [] },
    ]
    
    example
    自定义 id/parentId 属性
    toTree(
      [
        { sub: 1, parent: null },
        { sub: 2, parent: null },
        { sub: 3, parent: 1 },
      ],
      { idKey: 'sub', parentKey: 'parent' }
    )
    // ->
    [
      {
        sub: 1,
        parent: null,
        items: [
          { sub: 3, parent: 1, items: [] }
        ]
      },
      { sub: 2, parent: null, items: [] },
    ]
    
    example
    自定义 children 属性
    toTree(
      [
        { id: 1, parentId: null },
        { id: 2, parentId: null },
        { id: 3, parentId: 1 },
      ],
      { children: 'items' }
    )
    // ->
    [
      {
        id: 1,
        parentId: null,
        items: [
          { id: 3, parentId: 1, items: [] }
        ]
      },
      { id: 2, parentId: null, items: [] },
    ]
    
    example
    自定义根节点
    toTree(
      [
        { id: 1, parentId: '__root__' },
        { id: 2, parentId: '__root__' },
        { id: 3, parentId: 1 },
      ],
      { root: '__root__' }
    )
    // ->
    [
      {
        id: 1,
        parentId: '__root__',
        children: [
          { id: 3, parentId: 1, children: [] }
        ]
      },
      { id: 2, parentId: '__root__', children: [] },
    ]
    
    example
    自定义函数导出
    toTree(
      [
        { id: 1, parentId: '__root__' },
        { id: 2, parentId: '__root__' },
        { id: 3, parentId: 1 },
      ],
      { root: nodes => nodes['__root__'] }
    )
    // ->
    [
      {
        id: 1,
        parentId: '__root__',
        children: [
          { id: 3, parentId: 1, children: [] }
        ]
      },
      { id: 2, parentId: '__root__', children: [] },
    ]
    
    example
    数据转换
    toTree(
      [
        { id: 1, parentId: null },
        { id: 2, parentId: null },
        { id: 3, parentId: 1 },
      ],
      {
        transform(row) {
          // 返回 null 或 undefined 的数据不回保留
          if (row.id === 3) return
          // 可以进行浅拷贝后修改,防止破坏原始对象
          return { ...row, test: true }
        }
      }
    )
    // ->
    [
      { id: 1, parentId: null, test: true, children: [] },
      { id: 2, parentId: null, test: true, children: [] },
    ]
    
    example
    自定义插入顺序
    toTree(
      [
        { id: 2, parentId: null, sort: 2 },
        { id: 5, parentId: 1, sort: 1 },
        { id: 4, parentId: 1, sort: 2 },
        { id: 3, parentId: null, sort: 3 },
        { id: 1, parentId: null, sort: 1 },
      ],
      {
        insert(siblings, node) {
          // ps: 任意层级的数据都是这样处理的
          const index = siblings.findIndex((n) => n.sort > node.sort)
    
          // 根据位置选择插入到兄弟节点当中
          if (index === -1) {
            siblings.push(node)
          } else {
            siblings.splice(index, 0, node)
          }
        }
      }
    )
    // ->
    [
      {
        id: 1,
        parentId: null,
        sort: 1
        children: [
          { id: 4, parentId: null, sort: 1, children: [] },
          { id: 5, parentId: null, sort: 2, children: [] },
        ]
      },
      { id: 2, parentId: null, sort: 2, children: [] },
      { id: 3, parentId: null, sort: 3, children: [] },
    ]
    

    Type parameters

    Parameters

    • data: T[]

      行数据

    • options: ToTreeOptions<S, T> = {}

      配置项

    Returns S[]

Generated using TypeDoc