1 Star 0 Fork 0

白鸢 / void-tree

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

void-tree

npm (scoped)

一个简陋的树型数据工具

只是个人项目,整体较为随便,作为开发途中的经验总结,如有相似的需求,推荐copy或阅读源码,不建议将该包在实际项目中使用

如有幸被实际使用在项目内,不胜荣幸

灵感是treemate

初衷

围绕树状数据解析,总结使用一些组件库的tree组件时的通用业务

一开始想到了使用 naiveui 团队开源的 treemate

treemate无法做到在递归时自定义一些行为

并且个人需求只是围绕树状数据解析

这个简陋至极的包也因此孕育而生

使用

/**
 *  一个占位,返回空空如也的模型
 */
createInitVoidTree()

/**
 * 通过一个单层列表构造树状结构
 * [{
 *    id: 0,
 *    parentId: -1
 * }]
 * 需拥有类似的数据结构
 */
createVoidTreeByList()

/**
 * 构造树状模型
 */
createVoidTree()

数据模型

interface TreeNode<Raw extends Record<string, any>> {
  key: Key;
  label: string;
  raw: Raw;
  level: number;
  index: number;
  parent?: TreeNode<Raw>;
  disabled: boolean;
  isLeaf: boolean;
  path: Key[];
  siblings: Array<TreeNode<Raw>>;
  children?: Array<TreeNode<Raw>>;
  [key: string]: any;
}

declare class VoidTree<Raw> {
  /**
   * 处理后对外展示的数据
   */
  treeNodes: TreeNode<Raw>[];
  /**
   * 自定义的一些缓存
   */
  cache: TreeCache<Raw>;
  /**
   * 选择的key值
   */
  checkKeys: Key[];
  constructor(treeNodes: TreeNode<Raw>[], cache: TreeCache<Raw>);
  /**
   * 获取某项缓存
   * @param key
   */
  getCache(key: string): any;
  /**
   * 获取key-node 结构的Map数据
   */
  get treeNodeMap(): Map<Key, TreeNode<Raw>>;
  /**
   * 通过key获取单个节点信息
   * @param key
   */
  getNode(key: Key): TreeNode<Raw> | undefined;
  /**
   * 通过多个key获取多个节点信息
   * @param key
   * @param ignoreEmpty
   */
  getNodes(key: Key | Key[], ignoreEmpty?: boolean): TreeNode<Raw>[];
  /**
   * 获取子节点
   * @param key
   */
  getChild(key: Key): TreeNode<Raw>[] | undefined;
  /**
   * 获取父节点
   * @param key
   */
  getParent(key: Key): TreeNode<Raw> | undefined;
  /**
   * 选择
   * @param key
   */
  check(key: Key | Key[]): void;
  /**
   * 选择
   * @param key
   */
  uncheck(key: Key | Key[]): void;
  /**
   * 选择
   * @param key
   */
  updateCheckKeys(key: Key | Key[]): void;
  /**
   * 自定义选择
   * @param fn
   */
  customUpdateCheckKeys(fn: (self: VoidTree<Raw>) => Key | Key[]): void;
  /**
   * 搜索
   * 解决一些组件库tree搜索不会筛选数据,而是加下划线
   * 方法改写自naiveui
   * @param keyword
   * @param filter
   */
  search(keyword: string, filter?: (pattern: string, v: string) => boolean): void;
}
MIT License Copyright (c) 2022 白鸢 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
TypeScript 等 2 种语言
MIT
取消

发行版 (1)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/white-kite/void-tree.git
git@gitee.com:white-kite/void-tree.git
white-kite
void-tree
void-tree
master

搜索帮助