element-ui el-table 树形结构 父子级联动

el-table 表格

为 select 和 select-all 设置回调函数

html 复制代码
<el-table :data="tableData" id="yc_load" ref="yc_load" height="500px" border default-expand-all
                row-key="showId" :tree-props="{children: 'children'}"
                @select="select"
                @select-all="selectAll">
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column label="姓名" :fixed="true" prop="showName" align="left" header-align="left" width="230"></el-table-column>
</el-table>

简要数据格式

json 复制代码
tableData: [
	{showId: xxx,
	showName: xxx,
	children: [
		{showId:xxx,
		showName:xxx,
		parentId:xxx},
		{showId:xxx,
		showName:xxx,
		parentId:xxx}
	]},
	{showId: xxx,
	showName: xxx}
],
ids: []

单选

js 复制代码
select(selection,row){
      let vm = this
      // 操作行 row 在 已选范围 selection 内,认为是选中,否则是取消选中 
      if(selection.some((el)=>row.showId===el.showId)){
      	// 设置当前行选中状态
        row.isChecked = true
        // 记录选中id 
        this.addId(row)
        // 若存在子级,自己全选
        if(row.children) {
          row.children.map(c => {
            this.$refs.yc_load.toggleRowSelection(c,true)
            c.isChecked = true
            this.addId(c)
          })
        }
        // 若存在父级,设置父级选中
        if(row.parentId){
          let pNode = vm.tableData.find(x => x.showId === row.parentId)
          this.$refs.yc_load.toggleRowSelection(pNode,true)
          pNode.isChecked = true
          this.addId(pNode)
        }
      } else {
        row.isChecked = false
        this.deleteId(row)
        // 若存在子级,子级全部取消选中
        if(row.children){
          row.children.map((i)=>{
            this.$refs.yc_load.toggleRowSelection(i,false)
            i.isChecked = false
            this.deleteId(i)
          })
        }
        // 若存在父级,判断父级的子级(当前行的兄弟) ,若全部未选中,取消父级选中
        if(row.parentId) {
          let pNode = vm.tableData.find(x => x.showId === row.parentId)
          if(!pNode.children.some(el => el.isChecked == true)) {
            this.$refs.yc_load.toggleRowSelection(pNode,false)
            pNode.isChecked = false
            this.deleteId(pNode)
          }
        }
      }
      console.log(this.ids)
}

全选

js 复制代码
selectAll(selection) {
	  // 判断当前是否有已选中的
      let rA = this.tableData.some(el => {
        let r = false
        if(el.children) {
          r = el.children.some(e => e.isChecked)
        }
        if(r) return r
        return el.isChecked
      })
      // 若有选中则全部取消,否则全部选中
      if(rA) {
        this.tableData.forEach(el => {
          el.isChecked = false
          this.$refs.yc_load.toggleRowSelection(el, false)
          this.deleteId(el)
          if(el.children) {
            el.children.forEach(e => {
              e.isChecked = false
              this.$refs.yc_load.toggleRowSelection(e, false)
              this.deleteId(e)
            })
          }
        })
      } else {
        this.tableData.forEach(el => {
          el.isChecked = true
          this.$refs.yc_load.toggleRowSelection(el, true)
          this.addId(el)
          if(el.children) {
            el.children.forEach(e => {
              e.isChecked = true
              this.$refs.yc_load.toggleRowSelection(e, true)
              this.addId(e)
            })
          }
        })
      }
      console.log(this.ids)
}

操作 ids

js 复制代码
	// 增加选中
    addId(o) {
      let id = o.showId
      if(this.ids.indexOf(id) < 0) {
        this.ids.push(id)
      }
    },
    // 删除选中
    deleteId(o) {
      let id = o.showId
      this.ids = this.ids.filter(item => item !== id);
    },

不再设置 selection-change 回调函数,直接监听ids

js 复制代码
  // 监听ids
  watch: {
    ids: function (newVal, oldVal) {
      this.handleChange(newVal)
    }
  },

感谢 element-ui el-table 实现全选且父级子级联动 提供的思路

另附 el-table 文档

相关推荐
巧手打字通6 天前
数据结构之美-深入理解树形结构
数据结构·树形结构
是良辰2 个月前
树形结构的一种便捷实现方案
java·树形结构
柳晓黑胡椒2 个月前
vue3实现多表头列表el-table,拖拽,鼠标滑轮滚动条优化
javascript·vue.js·elementui·el-table
雪梅零落4 个月前
Element-ui el-table组件单选/多选/跨页勾选讲解
前端·vue.js·elementui·el-table
叶浩成5204 个月前
el-table组件选中后使用toggleRowSelection无法取消已选中的数据——bug记录-骚操作解决
elementui·el-table
赢乐4 个月前
Vue开发中Element UI/Plus使用指南:常见问题(如Missing required prop: “value“)及中文全局组件配置解决方案
el-table·v-model·element ui·required prop·type=index·不显示序号·配置中文
jackyrongvip5 个月前
收藏:关于闭包表
树形结构·闭包表
Ying(英子)5 个月前
修改element-ui table组件展开/收起图标、支持点击行展开/收起、隐藏不可展开行得图标
ui·elementui·el-table·表格展开收起·el-table展开收起图标·table操作·el-table实践
java的艺术6 个月前
设计模式(8):组合模式
java·设计模式·组合模式·树形结构
蓝胖子的多啦A梦6 个月前
Vue+Element-UI Table表格实现复选框单选效果(隐藏表头上的全选Checkbox)
前端·vue.js·elementui·el-table