antd tree组件文件名换行 + 点击展开时,自动收起同级其他展开目录

1.在项目中用 antd的tree组件的时候,遇到两个问题

1.文件名太长的话 会超出容器 很难看,解决方法如下

` 引入css在global下设置
:global {
.ant-tree li .ant-tree-node-content-wrapper{
height:auto;
}
.ant-tree-node-content-wrapper{
white-space: normal;
max-width: 100%;
}
}

`

2.节点都展开的话 会太高了。也会撑开盒子,影响美观。
解决方法: 一方面 展开一个的时候 ,可以收起同级别其他已经展开的。代码如下,在tree的api里 展开回调设置,同时 要tree组件绑定 state的展开节点数组。

onExpand = (a,b) => { if(b.expanded){ if(a.length>0){ a.splice(0,a.length-1) } } this.setState({ expandedKeys:a }) };

上边的代码有一个问题 ,就是不能 跨级 收起。改后的代码如下

   onExpand = (a,b) => {
       if(b.expanded){
            if(a.length>0){
                a.splice(0,a.length-1)
            }
            this.setState({
                expandedKeys:a
            }) 
       }else{
           const key = b.node.props.children.map((obj,index)=>{
               if(a.indexOf(obj.key)>-1){
                   return obj.key;
               }
               return ''
           }).filter((v,index)=> v!== '');
           //index  是点击收起节点的下级展开节点
           const index = a.indexOf(key[0]);  //因为展开的时候会收起兄弟节点  所以这里应该只有一个
           if(index>0){
               a.splice(0,index + 1);  //从0开始  删除到点击的下一级已展开节点
           }
          this.setState({
            expandedKeys:a
         })  
       }
      
  };

另一方面就是 展示部分 显示 查看更多... 这个我看看怎么弄。

转载于:https://www.cnblogs.com/chengyunshen/p/10523584.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过在Tree组件中设置onSelect函数来实现点击节点文字展开收起。 具体实现步骤如下: 1. 在Tree组件中设置onSelect函数,该函数会在节点被选中触发。 ```javascript <Tree onSelect={onSelect} // 其他属性 > // 树节点 </Tree> ``` 2. 在onSelect函数中判断被选中的节点是否有子节点,如果有则展开节点,否则不做任何操作。 ```javascript const onSelect = (selectedKeys, { node }) => { const { children } = node.props; if (children) { setExpandedKeys(expandedKeys => { const index = expandedKeys.indexOf(selectedKeys[0]); if (index > -1) { // 如果节点已展开,则收起节点 return [...expandedKeys.slice(0, index), ...expandedKeys.slice(index + 1)]; } else { // 如果节点未展开,则展开节点 return [...expandedKeys, selectedKeys[0]]; } }); } } ``` 3. 在Tree组件中设置expandedKeys属性,该属性展开的节点的key的数组。 ```javascript <Tree onSelect={onSelect} expandedKeys={expandedKeys} // 其他属性 > // 树节点 </Tree> ``` 完整代码如下: ```javascript import React, { useState } from 'react'; import { Tree } from 'antd'; const Demo = () => { const [expandedKeys, setExpandedKeys] = useState([]); const onSelect = (selectedKeys, { node }) => { const { children } = node.props; if (children) { setExpandedKeys(expandedKeys => { const index = expandedKeys.indexOf(selectedKeys[0]); if (index > -1) { // 如果节点已展开,则收起节点 return [...expandedKeys.slice(0, index), ...expandedKeys.slice(index + 1)]; } else { // 如果节点未展开,则展开节点 return [...expandedKeys, selectedKeys[0]]; } }); } } return ( <Tree onSelect={onSelect} expandedKeys={expandedKeys} > // 树节点 </Tree> ); } export default Demo; ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值