随笔 - 19  文章 - 0  评论 - 1  阅读 - 8753

element表格多选改为单选,且去除表头的多选框


一、利用el-table自带方法selection-change(当选择项发生变化时触发该事件)

关键代码:

this.$refs.serialnoTable.clearSelection()

this.$refs.serialnoTable.toggleRowSelection(val.pop())

1.@selection-change="handleSelectionChange" 是重点改变点击选择框后的事件:

复制代码
<el-table
  ref="multipleTable"
  :data="persionTable"
  tooltip-effect="dark"
  style="width: 100%"
  @selection-change="handleSelectionChange"
>
  <el-table-column type="selection" width="55"> </el-table-column>
  <el-table-column prop="name" label="姓名" width="120"> </el-table-column>
  <el-table-column prop="address" label="职称" show-overflow-tooltip>
  </el-table-column>
</el-table>
复制代码
2.ref="multipleTable" 是必须的,后面会用到这个的实例方法clear 用来清空所有勾选项 
利用判断来判断若超过一个勾选框就先清空,最后把当前最新勾选的那一项放入用来储存勾选容器this.multipleSelectionUpdate内。
注意点: 需要注意不实时更新的bug: this.$set(this.multipleSelectionUpdate, val[0]);
复制代码
// 勾选框
handleSelectionChange(val) {
// this.multipleSelection = val;
if (val.length > 1) {
this.$refs.multipleTable.clearSelection();
this.$refs.multipleTable.toggleRowSelection(val.pop());
} else {
this.multipleSelectionUpdate = val;
}
// this.multipleSelectionUpdate = val[0];// 这种赋值同样不会实时更新
this.$set(this.multipleSelectionUpdate, val[0]);
console.log(this.multipleSelectionUpdate, this.multipleSelection); // 此时要回显到页面的值
},3.如果简化写法,简单用,这样写也同样可以快速单选切换:handleSelectionChange(val) {
this.multipleSelection = val;
if (val.length > 1) {
this.$refs.multipleTable.clearSelection();
this.$refs.multipleTable.toggleRowSelection(val.pop());
}
}, 
复制代码
二、
1.将element中的 table 表格 组件中的 多选 代码,拷贝到自己的前端项目中:
<el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" >
  <el-table-column type="selection" width="55"></el-table-column>
  <el-table-column label="日期" width="120">
    <template slot-scope="scope">{{ scope.row.date }}</template>
  </el-table-column>
  <el-table-column prop="name" label="姓名" width="120"></el-table-column>
  <el-table-column prop="address" label="地址" show-overflow-tooltip></el-table-column>
</el-table> 
2.去除 表头出行 多选的勾选框,在 el-table 的标签中 加上样式 class = "table-style",从样式上隐藏 该全选的勾选框, 代码如下
复制代码
<el-table class="table-style" ref="multipleTable" :data="tableData" tooltip-effect="dark"  style="width: 100%" >
  <el-table-column type="selection" width="55"> </el-table-column>
  <el-table-column label="日期" width="120">
    <template slot-scope="scope">{{ scope.row.date }}</template>
  </el-table-column>
  <el-table-column prop="name" label="姓名" width="120"> </el-table-column>
  <el-table-column prop="address" label="地址" show-overflow-tooltip>
  </el-table-column>
</el-table>
复制代码
<-- 从样式上 隐藏 全选勾选框 -->
<style lang="less">
  .table-style {
    .el-table-column--selection.is-leaf .el-checkbox {
      display: none;
    }
  }
</style>
3.将多选改为 单选 功能,在el-table 的标签中重新定义 一个方法 @select="selectInfo" ,用于将单选功能实现, 代码如下:
复制代码
<el-table  class= "table-style" ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" @select="selectInfo">
  <el-table-column type="selection" width="55"> </el-table-column>
  <el-table-column label="日期" width="120">
    <template slot-scope="scope">{{ scope.row.date }}</template>
  </el-table-column>
  <el-table-column prop="name" label="姓名"  width="120"></el-table-column>
  <el-table-column prop="address" label="地址" show-overflow-tooltip></el-table-column>
 </el-table>
<script>
  data() {
     return {
       multipleTable: "", //所选择的表格数据指向
     };
   },
  methods: {
        selectInfo(selection, row) {
           console.log(selection, row);
           // 清除 所有勾选项
           this.$refs.multipleTable.clearSelection();
           // 当表格数据都没有被勾选的时候 就返回
           // 主要用于将当前勾选的表格状态清除
           if (selection.length == 0) return;
           this.$refs.multipleTable.toggleRowSelection(row, true);
         },    
      }
</script>
<-- 从样式上 隐藏 全选勾选框 -->
     <style lang="less">
         .table-style {
           .el-table-column--selection.is-leaf .el-checkbox {
             display: none;
           }
         }
     </style>
复制代码
三、
复制代码
<template>
  <div>
    <el-table
      ref="multipleTable"
      :data="tableData"
      highlight-current-row
      @select-all="onSelectAll"
      @selection-change="selectItem"
      @row-click="onSelectOp"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" align="center" />
      <el-table-column label="姓名" prop="name" align="center" />
      <el-table-column label="手机号码" prop="province" align="center" />
    </el-table>
  </div>
</template>
 
<script>
export default {
  data() {
    return {
      tableData: [{
          name: '王小虎1',
          province: '上海1',
        }, {
          name: '王小虎2',
          province: '上海2',
        }, {
          name: '王小虎3',
          province: '上海3',
        }, {
          name: '王小虎4',
          province: '上海4',
        }],
    }
  },
  mounted(){
  },
  methods: {
    onSelectAll() {
      this.$refs.multipleTable.clearSelection();
    },
    selectItem(rows) {
      if (rows.length > 1) {
        const newRows = rows.filter((it, index) => {
          if (index == rows.length - 1) {
            this.$refs.multipleTable.toggleRowSelection(it, true);
            return true;
          } else {
            this.$refs.multipleTable.toggleRowSelection(it, false);
            return false;
          }
        });
        this.multipleSelection = newRows;
      } else {
        this.multipleSelection = rows;
      }
      // this.userId = this.multipleSelection.length? this.multipleSelection[0].guid: "";
      console.log('2',this.multipleSelection)
    },
    onSelectOp(row) {
      this.$refs.multipleTable.clearSelection();
      this.$refs.multipleTable.toggleRowSelection(row, true);
      this.multipleSelection = [];
      this.multipleSelection.push(row);
    },
  }
};
</script>
复制代码

 

posted on   阿宇爱吃鱼  阅读(2051)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示