antdvue table 树形数据一键展开一键收起

本文介绍了如何在antdvue的Table组件中实现树形数据的一键展开和一键收起功能,通过递归方法和展开收起事件来控制节点的显示状态。在初始化时,通过遍历所有数据获取key设置expandedRowKeys。在节点点击事件onExpand中,动态管理expandedRowKeys的key,实现节点的展开与收起。

antd 表格的一键展开 以及 节点的展开收起功能

在这里插入图片描述
html

 <a-table
      style="margin-top: 20px"
      :columns="columns"
      :data-source="dataSource"
      bordered
      rowKey="key"
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :expandedRowKeys="expandedRowKeys"
      @expand="onExpand"
      :expanded-keys="expandedKeys"
    >
      <span slot="action" slot-scope="text, record, index">
        <a @click="reloadFn(record)">重置</a>
      </span>
    </a-table>
      columns:[],
	  dataSource:[],
	  expandedRowKeys: [],
      selectedRowKeys: [],
      selectionRows: [],
      expandedKeys: [],

递归方法 和展开收起事件

 recursiveExpand(arr, tmp) {
      for (let i = 0; i < arr.length; i++) {
        tmp.push(arr[i].key)
        if (arr[i].children !== null && arr[i].children !== undefined && arr[i].children.length > 0) {
          this.recursiveExpand(arr[i].children, tmp)
        }
      }
      return tmp
    },
      handleOPen(){
      if(this.openStatus){
        this.expandedRowKeys = [];
        this.$emit('update:openStatus', false)
      }else{
        this.recursiveExpand(this.dataSource, this.expandedRowKeys)
        this.$emit('update:openStatus', true)
      }

    },
    onExpand(expandedKeys, record){
      this.expandedKeys = expandedKeys
      let arr = []
      this.recursiveExpand([record], arr)
      if(expandedKeys){
        this.expandedRowKeys = this.expandedRowKeys.concat(arr)
      }else {
        arr.forEach(v => {
          this.expandedRowKeys.splice( this.expandedRowKeys.indexOf(v),1)
        })
      }
    },
    onSelectChange(selectedRowKeys, selectedRows){
      console.log(selectedRowKeys, selectedRows, 'onSelectChange事件')
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectedRows;
    },

总结:初始化的时候递归所有数据拿到key给expandedRowKeys赋值。收起清空。
点击节点的时候删除或增加expandedRowKeys里面的key数据(onExpand方法)。

在Vue2中使用el-table树形数据懒加载模式实现一键展开,可参考引用中提供的解决思路。虽然未直接给出Vue2的el-table树形数据懒加载模式下一键展开的完整代码,但可以从相关内容中获取一些启示。 首先,需要在模板中定义一个按钮来触发一键展开操作,同时要绑定对应的方法。在`el-table`组件中,利用`lazy`属性开启懒加载,通过`load`方法加载节点数据。 以下是一个示例代码框架: ```vue <template> <div> <el-button @click="expandAll">一键展开</el-button> <el-table :data="tableData" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" lazy :load="loadChildren" > <!-- 表格列定义 --> <el-table-column prop="name" label="名称"></el-table-column> </el-table> </div> </template> <script> export default { data() { return { tableData: [ // 初始表格数据 ] }; }, methods: { expandAll() { // 实现一键展开的逻辑 // 遍历表格数据,设置展开状态 this.tableData.forEach(item => { this.$set(item, 'expanded', true); // 递归处理节点 if (item.children) { this.expandChildren(item.children); } }); }, expandChildren(children) { children.forEach(child => { this.$set(child, 'expanded', true); if (child.children) { this.expandChildren(child.children); } }); }, loadChildren(tree, treeNode, resolve) { // 懒加载节点数据的逻辑 // 调用接口获取节点数据 // 示例: const children = [ // 节点数据 ]; resolve(children); } } }; </script> ``` 在上述代码中,`expandAll`方法用于一键展开表格的所有节点,通过递归调用`expandChildren`方法处理节点。`loadChildren`方法用于懒加载节点数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值