Element-ui el-table 树形表格多选

Element-ui el-table 树形表格多选

  1. 关键方法 toggleRowSelection(row,flag) @select="rowSelect"(行选中) @select-all="selectAll"(全选)
  2. 页面代码
    <el-table
      :data="dataList"
      ref="tableRef"
      highlight-current-row
      v-loading="loading"
      style="width: 100%;"
      size="small"
      row-key="id"
      @selection-change="selsChange"
      @select="rowSelect"
      @select-all="selectAll"
      :tree-props="{children: 'children', hasChildren: 'children.length>0'}"
    >
      >
      <el-table-column type="selection" width="50"></el-table-column>
      <el-table-column prop="menuName" label="菜单名称" sortable></el-table-column>
      <el-table-column prop="menuDes" label="菜单描述" sortable></el-table-column>
      <el-table-column prop="menuIcon" label="导航图标" sortable>
        <template slot-scope="scope">
          <i :class="scope.row.menuIcon"></i>
        </template>
      </el-table-column>
      <el-table-column prop="menuPath" label="导航路径" sortable></el-table-column>
      <el-table-column prop="menuComponent" label="Vue文件路径" sortable></el-table-column>
      <el-table-column prop="menuLeaf" label="是否单节点" sortable>
        <template slot-scope="scope">{{scope.row.menuLeaf === 1?'是':'否'}}</template>
      </el-table-column>
      <el-table-column prop="menuHidden" label="是否隐藏" sortable>
        <template slot-scope="scope">{{scope.row.menuHidden === 1?'是':'否'}}</template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag type="success" v-show="scope.row.menuStatus == 1" effect="dark" size="medium">正常</el-tag>
          <el-tag type="danger" v-show="scope.row.menuStatus == 2" effect="dark" size="medium">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="small"
            @click="changeMenuStatus(scope.row)"
            v-if="scope.row.menuStatus == 1"
          >禁用</el-button>
          <el-button
            type="success"
            size="small"
            @click="changeMenuStatus(scope.row)"
            v-if="scope.row.menuStatus == 2"
          >启用</el-button>
          <el-button type="primary" size="small" @click="toUpdate(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
  1. js代码
    toggleSelection(rows, flag) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.tableRef.toggleRowSelection(row, flag);
        });
      } else {
        this.$refs.tableRef.clearSelection();
      }
    },
    rowSelect(selection, row) {
      if (selection.indexOf(row) > -1 && row.menuLevel === 1) {
        this.toggleSelection(row.children, true);
      }
      if (selection.indexOf(row) === -1 && row.menuLevel === 1) {
        this.toggleSelection(row.children, false);
      }
      if (selection.indexOf(row) > -1 && row.menuLevel === 2) {
        let s = this.dataList.filter(item => {
          if (item.id === row.menuPid) {
            return item;
          }
        });
        this.toggleSelection(s, true);
      }
    },
    selectAll(selection) {
      var flag = false; // 默认 为全不选
      selection.forEach(item => {
        if (item.menuLevel === 1) {
          flag = true;
          this.toggleSelection(item.children, true);
        }
      });
      if (!flag) {
        this.toggleSelection();
      }
    },
  1. 目前能实现我想要的结果,可能跟你们需求不太一致,可自行修改
    效果如下:
    该死的水印
  • 3
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 18
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值