關(guān)于Element-ui中table默認(rèn)選中toggleRowSelection問題
Element-ui中table默認(rèn)選中toggleRowSelection
一.toggleRowSelection
toggleRowSelection(row, selected)接受兩個參數(shù),row傳遞被勾選行的數(shù)據(jù),selected設(shè)置是否選中
注意:調(diào)用toggleRowSelection這個方法 需要獲取真實(shí)dom 所以需要注冊 ref 來引用它
二.操作
(一).默認(rèn)選中
1.當(dāng)數(shù)據(jù)源固定在table的
<script>
export default {
mounted() {
this.$refs.multipleTable.toggleRowSelection(this.tableData3[2],true);
}
}
</script>
(二).點(diǎn)擊tr選中
1.在table中設(shè)置 @row-click="handleCurrentChange"
row-click 點(diǎn)擊行事件
<template> <el-table :data="tableData3" ref="multipleTable" @row-click="handleCurrentChange"> </el-table> </template>
<script>
export default {
methods: {
handleCurrentChange(row, event, column){
this.$refs.multipleTable.toggleRowSelection(row,true);//點(diǎn)擊選中
}
}
}
</script>(三).獲取選中的值
1.設(shè)置table 中@selection-change="selsChange"
2.data 中設(shè)置sels:[]
<template> <el-table :data="tableData3" ref="multipleTable" @row-click="handleCurrentChange" @selection-change="selsChange"> </el-table> </template>
<script>
export default {
methods: {
selsChange( val){
this.sels=val;
console.log(this.sels)
}
}
}
</script>三.案例
1.table tr 點(diǎn)擊 復(fù)選框選中 再次點(diǎn)擊 復(fù)選框取消選中
①設(shè)置一個全局函數(shù)
exports.install = function (Vue, options) {
//刪除數(shù)組 指定的元素
Vue.prototype.removeByValue=function(arr, val){
for(var i=0; i<arr.length; i++) {
if(arr[i] == val) {
arr.splice(i, 1);
break;
}
}
};
};②table.vue
<template> <el-table :data="tableData3" ref="multipleTable" @row-click="handleCurrentChange" @selection-change="selsChange"> <el-table-column type="selection" width="55" ></el-table-column> <el-table-column type="index" label="序號" width="60"></el-table-column> <el-table-column prop="sex" label="性別" width="100" :formatter="formatSex"></el-table-column> <el-table-column prop="date" label="日期"></el-table-column> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> </template>
<script>
export default {
data() {
return {
tableData3: [{
id:'1',
date: '2016-05-03',
name: '嘎哈和',
address: '上海市普陀區(qū)金沙江路 1518 弄',
sex:'1'
}, {
id:'2',
date: '2016-05-02',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1518 弄',
sex:'0'
}, {
id:'3',
date: '2016-05-02',
name: '莫默模',
address: '上海市普陀區(qū)金沙江路 1518 弄',
sex:'-1'
}],
arrID:[],
}
},
methods: {
formatSex: function (row, column, cellValue, index) {
return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
},
// 點(diǎn)擊事情
handleCurrentChange(row, event, column){
var same=false;
if(this.arrID.length > 0){
for(var i=0; i<this.arrID.length ;i++){
if(this.arrID[i]==row.id){
same=true;
this.removeByValue(this.arrID, row.id);
break;
}
}
if(same==true){
this.$refs.multipleTable.toggleRowSelection(row,false);
}else{
this.$refs.multipleTable.toggleRowSelection(row,true);
this.arrID.push(row.id);
}
}else{
this.$refs.multipleTable.toggleRowSelection(row,true);
this.arrID.push(row.id);
}
},
selsChange(val){
var valId=[];
for(var i=0;i<val.length;i++){
var arrIDsame=false;
valId.push(val[i].id);
}
this.arrID=valId;
}
},
mounted() {
this.$refs.multipleTable.toggleRowSelection(this.tableData3[2],true);//默認(rèn)選中
}
}
</script>
<style>
</style>
Element-ui踩坑之表格選中toggleRowSelection問題
今天遇到一個表格選擇默認(rèn)的問題,表格當(dāng)前頁沒有值,但是默認(rèn)還是選中的,這個時候想要刪除表格選擇:
this.$nextTick(()=>{
? ?this.$refs.multipleTable.selection.forEach((select,index)=>{
? ? ? if(select.id === this.selectedTableData[data].id){
? ? ? ? ? ?this.$refs.multipleTable.toggleRowSelection(select,false);
? ? ? }
? ?})
})- 要點(diǎn)1,加this.$nextTick(()=>{});
- 要點(diǎn)2,選擇的值要從table的selection中取,應(yīng)該key的變化問題。
由于篇幅較短,再加一點(diǎn)之前踩的坑記錄:
僅對 type=selection 的列有效,類型為 Boolean,為 true 則會在數(shù)據(jù)更新之后保留之前選中的數(shù)據(jù)(需指定 row-key)
:row-key="getRowKeys"
//多選時需要
getRowKeys(row) {
? return row.id;
},這種翻頁之類的多選就會保留數(shù)據(jù)。
再說一個想要清空表格的所有選擇:
this.$refs.multipleTable.clearSelection()
還有全選表格行:
this.$refs.multipleTable.toggleAllSelection()
后面遇到再做補(bǔ)充……
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue首屏性能優(yōu)化組件知識點(diǎn)總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于Vue首屏性能優(yōu)化組件知識點(diǎn)總結(jié),有需要的朋友們可以跟著學(xué)習(xí)下。2021-11-11
vue中如何動態(tài)設(shè)置css樣式的hover
這篇文章主要介紹了vue中如何動態(tài)設(shè)置css樣式的hover,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
vue離開當(dāng)前頁面觸發(fā)的函數(shù)代碼
這篇文章主要介紹了vue離開當(dāng)前頁面觸發(fā)的函數(shù)代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
淺析Vue3中通過v-model實(shí)現(xiàn)父子組件的雙向數(shù)據(jù)綁定及利用computed簡化父子組件雙向綁定
這篇文章主要介紹了淺析Vue3中通過v-model實(shí)現(xiàn)父子組件的雙向數(shù)據(jù)綁定及利用computed簡化父子組件雙向綁定,需要的朋友可以參考下2022-12-12

