el-table树形表格结构,勾选联动 父级勾选

vue element el-table树形表格结构,勾选联动 父级勾选

<el-table
        :data="tableData"
        v-loading="loading"
        border
        ref="xTable"
        @select="select" 
        @select-all="handleSelectAllChange"
        row-key="id"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
</el-table>
定义一个变量ids:[]
    select(selection,row) {
      let vm = this
      // 操作行 row 在 已选范围 selection 内,认为是选中,否则是取消选中
      if (selection.some(el => row.id === el.id)) {
        // 设置当前行选中状态
        row.isChecked = true
        // 记录选中id
        vm.addId(row)
        // 若存在子级,自己全选
        if (row.children) {
          row.children.map(c => {
            vm.$refs.xTable.toggleRowSelection(c, true)
            c.isChecked = true
            vm.addId(c)
          })
        }
        // 若存在父级,设置父级选中
        if (row.parentId) {
          let pNode = {}
          vm.tableData.forEach(item => {  //根据后台数据可自行修改
            if(!item['children'])return;
            item['children'].find(x => {
              if(x.id === row.id){
                pNode = item
              }
            })
          })
          //let pNode = vm.tableData.find(x => x.id === row.parentId) // //根据后台数据可自行修改
          vm.$refs.xTable.toggleRowSelection(pNode, true)
          pNode.isChecked = true
          vm.addId(pNode)
        }
      } else {
        row.isChecked = false
        vm.deleteId(row)
        // 若存在子级,子级全部取消选中
        if (row.children) {
          row.children.map(i => {
            vm.$refs.xTable.toggleRowSelection(i, false)
            i.isChecked = false
            vm.deleteId(i)
          })
        }
        // 若存在父级,判断父级的子级(当前行的兄弟) ,若全部未选中,取消父级选中
        if (row.parentId) {
          let pNode = {}
          vm.tableData.forEach(item => { //根据后台数据可自行修改
            if(!item['children'])return;
            item['children'].find(x => {
              if(x.id === row.id){
                pNode = item
              }
            })
          })
          if (pNode.children && !pNode.children.some(el => el.isChecked == true)) {
            vm.$refs.xTable.toggleRowSelection(pNode, false)
            pNode.isChecked = false
            vm.deleteId(pNode)
          }
        }
      }
    },
    // 增加选中
    addId(o) {
      let id = o.id
      console.log('添加')
      if (id && this.ids.indexOf(id) < 0) {
        this.ids.push(id)
      }
    },
    // 删除选中
    deleteId(o) {
      console.log('删除')
      let id = o.id
      this.ids = this.ids.filter(item => item !== id)
    },
    /**
     * 切换全部select的change方法
     */
    handleSelectAllChange(selection) {
      this.toggleAllSelection(selection)
    },
    // 表格全选的 勾选 与 取消 的逻辑
    toggleAllSelection(selectedData) {
      this.isCheckedAll = !this.isCheckedAll
      this.ids = [];
      this.multipleSelection = selectedData
      if (this.isCheckedAll) {
        selectedData.forEach(subRow => {
          this.ids.push(subRow.id)
          this.toggleSelection(subRow['children'] || [])
        })
      } else {
        this.$refs['xTable'].clearSelection()
      }
    },
    toggleSelection(rows) {
      if (!rows.length) {
        return
      }
      rows.forEach(row => {
        // 如果为父数据 直接勾选
        if (!this.rowHasSelected(row)) {
          this.ids.push(row.id)
          this.$refs['xTable'].toggleRowSelection(row)
        }
        // 如果存在子数据  则回调
        if (row['children']) {
          this.toggleSelection(row['children'] || [])
        }
      })
    },
    /**
     *判断是否为父级的id
     */
    rowHasSelected(row) {
      console.log(this.multipleSelection)
      let index = this.multipleSelection.find(item => {
        return item.id === row.id
      })
      return index ? true : false
    },
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你使用的是 ElementUI 中的 el-table 树形表格,那么你可以通过设置 `treeProps` 属性来指定树形结构的相关属性。其中包括 `children` 表示子节点的属性名,`hasChildren` 表示是否有子节点的属性名。 在排序时,你可以使用 `sort` 属性来指定排序规则,例如: ```html <el-table :data="tableData" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" :sort-method="sortTable"> <el-table-column prop="name" label="名称" sortable></el-table-column> <el-table-column prop="age" label="年龄" sortable></el-table-column> </el-table> ``` 其中,`sort-method` 属性指定了排序方法,可以在 `methods` 中定义: ```js methods: { sortTable(a, b) { // 判断是否为根节点 const aParent = this.getParentNode(a) const bParent = this.getParentNode(b) const aIsRoot = !aParent const bIsRoot = !bParent // 根节点优先 if (aIsRoot && !bIsRoot) { return -1 } else if (!aIsRoot && bIsRoot) { return 1 } // 比较节点值 if (a[this.sortField] > b[this.sortField]) { return this.sortOrder === 'ascending' ? 1 : -1 } else if (a[this.sortField] < b[this.sortField]) { return this.sortOrder === 'ascending' ? -1 : 1 } else { return 0 } }, getParentNode(node) { // 获取当前节点的父节点 const parentKey = this.$refs.table.treeProps.children const parentNode = this.$refs.table.getNodeById(node[parentKey]) return parentNode } } ``` 在 `sortTable` 方法中,首先判断当前节点是否为根节点,如果是根节点则优先排在前面。然后再比较节点值进行排序。在获取父节点时,可以使用 `$refs` 获取到 el-table 的实例,然后调用 `getNodeById` 方法获取节点对应的行数据。 以上是一个示例代码,你可以根据自己的实际需求进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值