0
点赞
收藏
分享

微信扫一扫

deepin20.9安装及配置

small_Sun 02-12 21:30 阅读 38

1、先上效果

树型控件,选中项形成一棵新的树,若父选中,子自动选中,子取消,父不取消,子选中,所有的父节点自动取消。同时支持模糊检索,会检索出所有包含该内容的关联节点。

2、环境准备

1、react18

2、antd 4+

3、代码实现

原理:利用antd的tree组件,可以通过设置Tree组件的checkable属性为true,启用了多选功能,当节点被选中或取消选中时,会触发onCheck事件,我们可以在该事件处理函数中更新checkedKeys状态 通过控制checkedKeys来实现你想要的选中,核心代码如下:

checkStrictly设置为true,表示子节点选择受控,

     <DirectoryTree
      ...
         checkable={checkable}
          expandedKeys={expandedKeys}
          treeData={treeData || []}
  
            checkedKeys: checkKeys,
            checkStrictly: true,
            onCheck: (selectedKeys: any, other) => {
               // 当前节点的所有下级子节点
              const childrenNodeKeys = getAllChildrenNodeKey(other?.node);
              const node: any = other?.node;
              if (other?.checked) {
               // 当前节点的所有上级父节点
                const parentKeys = Array.isArray(node?.parentId) ? node?.parentId : [];
                let currentSelectedKeys = [...selectedKeys?.checked, ...parentKeys, ...childrenNodeKeys].filter(
                  (item: any, i: number, self: any): item is React.Key =>
                    !!(item && self?.indexOf?.(item) === i),
                );
                setCheckKeys?.(currentSelectedKeys);
                onCheck?.(currentSelectedKeys)
              } else {
                const currentSelectedKeys = (selectedKeys?.checked || []).filter(((key: string) => !childrenNodeKeys.includes(key) && key !== node?.rowId))
                setCheckKeys(currentSelectedKeys);
                onCheck?.(currentSelectedKeys);
              }
            }
         

        />

 当前节点的所有下级子节点

  const getAllChildrenNodeKey = (node: any) => {
    const result: any = [];
    const getChildrenKey = (childrenList: any) => {
      if (childrenList && childrenList.length > 0) {
        childrenList.forEach((item: any) => {
          if (item?.rowId) {
            result.push(item?.rowId)
          }
          if (item?.children && item?.children.length > 0) {
            getChildrenKey(item?.children || []);
          }

        });
      }
    }
    getChildrenKey(node?.children || []);
    return result;

  }

tree属性如下:

allowDrop是否允许拖拽时放置在该节点({ dropNode, dropPosition }) => boolean-
autoExpandParent是否自动展开父节点booleanfalse
blockNode是否节点占据一行booleanfalse
checkable节点前添加 Checkbox 复选框booleanfalse
checkedKeys(受控)选中复选框的树节点(注意:父子节点有关联,如果传入父节点 key,则子节点自动选中;相应当子节点 key 都传入,父节点也自动选中。当设置 checkable 和 checkStrictly,它是一个有checkedhalfChecked属性的对象,并且父子节点的选中与否不再关联string[] | {checked: string[], halfChecked: string[]}[]
checkStrictlycheckable 状态下节点选择完全受控(父子节点选中状态不再关联)booleanfalse
defaultCheckedKeys默认选中复选框的树节点string[][]
defaultExpandAll默认展开所有树节点booleanfalse
defaultExpandedKeys默认展开指定的树节点string[][]
defaultExpandParent默认展开父节点booleantrue
defaultSelectedKeys默认选中的树节点string[][]
disabled将树禁用booleanfalse
draggable设置节点可拖拽,可以通过 icon: false 关闭拖拽提示图标boolean | ((node: DataNode) => boolean) | { icon?: React.ReactNode | false, nodeDraggable?: (node: DataNode) => boolean }falseconfig: 4.17.0
expandedKeys(受控)展开指定的树节点string[][]
fieldNames自定义节点 title、key、children 的字段object{ title: title, key: key, children: children }4.17.0
filterTreeNode按需筛选树节点(高亮),返回 truefunction(node)-
height设置虚拟滚动容器高度,设置后内部节点不再支持横向滚动number-
icon自定义树节点图标。ReactNode | (props) => ReactNode-
loadData异步加载数据function(node)-
loadedKeys(受控)已经加载的节点,需要配合 loadData 使用string[][]
multiple支持点选多个节点(节点本身)booleanfalse
rootStyle添加在 Tree 最外层的 styleCSSProperties-4.20.0
selectable是否可选中booleantrue
selectedKeys(受控)设置选中的树节点,多选需设置 multiple 为 truestring[]-
showIcon是否展示 TreeNode title 前的图标,没有默认样式,如设置为 true,需要自行定义图标相关样式booleanfalse
showLine是否展示连接线boolean | { showLeafIcon: ReactNode | ((props: AntTreeNodeProps) => ReactNode) }false
switcherIcon自定义树节点的展开/折叠图标ReactNode | ((props: AntTreeNodeProps) => ReactNode)-renderProps: 4.20.0
titleRender自定义渲染节点(nodeData) => ReactNode-4.5.0
treeDatatreeNodes 数据,如果设置则不需要手动构造 TreeNode 节点(key 在整个树范围内唯一)array<{key, title, children, [disabled, selectable]}>-
virtual设置 false 时关闭虚拟滚动booleantrue4.1.0
onCheck点击复选框触发function(checkedKeys, e:{checked: boolean, checkedNodes, node, event, halfCheckedKeys})-
onDragEnddragend 触发时调用function({event, node})-
onDragEnterdragenter 触发时调用function({event, node, expandedKeys})-
onDragLeavedragleave 触发时调用function({event, node})-
onDragOverdragover 触发时调用function({event, node})-
onDragStart开始拖拽时调用function({event, node})-
onDropdrop 触发时调用function({event, node, dragNode, dragNodesKeys})-
onExpand展开/收起节点时触发function(expandedKeys, {expanded: boolean, node})-
onLoad节点加载完毕时触发function(loadedKeys, {event, node})-
onRightClick响应右键点击function({event, node})-
onSelect点击树节点触发function(selectedKeys, e:{selected: boolean, selectedNodes, node, event})-

 关注我并且留言发源码

或者自动下载

https://download.csdn.net/download/yalywq/88814803?spm=1001.2014.3001.5503

举报

相关推荐

0 条评论