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>
<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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通