Element的table结合树形结构tree多选的问题

Element的table结合树形结构tree多选的问题

一、效果图:

(1)全选:
在这里插入图片描述
(2)单选(选择父级子节也选中):
在这里插入图片描述
(3)table有disable的项不能选中(这里列表项条件是status == "1"就禁止掉):
在这里插入图片描述

二、数据结构:

注意:父级和子节都必须有唯一的id,因为后端没有给所以前端自己加了一个唯一标识的id
在这里插入图片描述
比如:
在这里插入图片描述

三、代码:

注意:cliExpenseSplitList是父级数组里children数组名称,名称可以看后端返回前端什么名称来写!`

         :render-option="tableOption"
          :data="tableList"
          :loading="isTableLoading"
          :props="{ width: '100%', height: '100%', size: 'mini' }"
          row-key="id"
          :tree-props="{
            children: 'cliExpenseSplitList',
            hasChildren: 'hasChildren',
          }"
          ref="multipleTable"
          @select-all="selectAll"
          @select="selectTr"
          @selection-change="handleSelectionChange"
data() {
    return {
    		tableList: [], // table列表
    		 isAllSelect: false, // 判断是否全选
    }
 }
  methods: {
 // 1.全选
    selectAll() {
      this.isAllSelect = !this.isAllSelect;
      let data = this.tableList;
      // 银行代扣、异常费用 不能选中缴费
      data.filter(item => {
        return (
          (item.taskType != null && item.taskType == "markException") ||
          item.status == "5"
        );
      });
      this.toggleSelect(data, this.isAllSelect, "all");
    },
    
   // 2.选择某行
    selectTr(selection, row) {
      this.$set(row, "children", !row.children);
      this.$nextTick(() => {
        this.isAllSelect = row.children;
        // 解决子级没有被勾选到
        if (
          selection.some(el => {
            return row.id === el.id;
          })
        ) {
          if (row.cliExpenseSplitList) {
            this.setChildren(row.cliExpenseSplitList, true);
          }
        } else {
          if (row.cliExpenseSplitList) {
            this.setChildren(row.cliExpenseSplitList, false);
          }
        }
      });
      this.toggleSelect(row, row.children, "tr");
    },
  
  // 3.子级跟着父级变动
    setChildren(children, type) {
      children.map(j => {
        this.toggleSelection(j, type);
        if (j.cliExpenseSplitList) {
          this.setChildren(j.cliExpenseSplitList, type);
        }
      });
    },

    
   // 4.递归子级
    toggleSelect(data, flag, type) {
      if (type === "all") {
        if (data.length > 0) {
          data.forEach(item => {
            this.toggleSelection(item, flag);
            if (
              item.cliExpenseSplitList &&
              item.cliExpenseSplitList.length > 0
            ) {
              this.toggleSelect(item.cliExpenseSplitList, flag, type);
            }
          });
        }
      } else {
        if (data.cliExpenseSplitList && data.cliExpenseSplitList.length > 0) {
          data.cliExpenseSplitList.forEach(item => {
            item.children = !item.children;
            this.$refs.multipleTable
              .getTableRef()
              .toggleRowSelection(item, flag);
            this.toggleSelect(item, flag, type);
          });
        }
      }
    },
    
    // 5.改变选中
    toggleSelection(row, flag) {
      this.$set(row, "isChecked", flag);
      this.$nextTick(() => {
        if (flag) {
          this.$refs.multipleTable.getTableRef().toggleRowSelection(row, flag);
        } else {
          this.$refs.multipleTable.getTableRef().clearSelection();
        }
      });
    },
    
 // 6.处理选中后的数据
    handleSelectionChange(val) {.......}
  }

注意:我这里的this.$refs.multipleTable.getTableRef().toggleRowSelection()写法是table的ref有封装的写法,如果没有封装可以直接写成this.$refs.multipleTable.toggleRowSelection(),在复制的时候请注意您们自己取的ref和方法的名称 。

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
如果您想要实现Element Table树形表格的拖动功能,可以考虑使用Vue.js框架并结合element-ui组件库,再使用一些自定义指令和事件来实现。 以下是一种实现方式: 1. 在Vue组件中引入element-ui组件库,并在模板中使用el-table组件,设置其tree-props属性为{children: 'children', hasChildren: 'hasChildren'},以支持树形结构。 2. 使用自定义指令v-draggable,在表格行上绑定该指令,使得行可以被拖动。 3. 监听el-table组件的row-drop事件,在该事件中更新数据源,以实现拖拽行的排序。 下面是示例代码: ```html <template> <el-table :data="tableData" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" @row-drop="handleRowDrop" > <el-table-column prop="name" label="名称"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <button v-if="scope.row.hasChildren" @click="toggleNode(scope.row)"> {{ scope.row.expanded ? '收起' : '展开' }} </button> </template> </el-table-column> <el-table-column label="拖动" width="80"> <template slot-scope="scope"> <div v-draggable class="drag-handle"> <i class="el-icon-s-grid"></i> </div> </template> </el-table-column> </el-table> </template> <script> import { directive } from 'vuedraggable' export default { directives: { draggable: directive }, data() { return { tableData: [ { name: '张三', age: 18, address: '北京市朝阳区', children: [ { name: '李四', age: 22, address: '北京市海淀区', children: [], hasChildren: false } ], hasChildren: true }, { name: '王五', age: 30, address: '上海市浦东区', children: [], hasChildren: false } ] } }, methods: { toggleNode(node) { this.$refs.table.toggleRowExpansion(node) }, handleRowDrop(event) { const { treeData, newIndex, oldIndex } = event // 更新数据源 treeData.splice(newIndex, 0, treeData.splice(oldIndex, 1)[0]) } } } </script> <style> .drag-handle { cursor: move; display: inline-block; padding: 5px; border: 1px solid #ccc; border-radius: 4px; text-align: center; } </style> ``` 在上面的代码中,我们首先引入了vuedraggable库中的directive指令,并将其作为自定义指令draggable注册到Vue实例中。 在模板中,我们将el-table组件的tree-props属性设置为指定的树形结构字段,以支持树形表格的展示。然后在拖动列中使用v-draggable指令,使得行可以被拖拽。 最后,我们监听了el-table组件的row-drop事件,在事件中更新数据源,以实现行的拖拽排序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值