element-ui中table組件的toggleRowSelection()方法使用
element-ui中table組件的toggleRowSelection()方法
最近,在做關于翻頁導出功能時,遇到需要將前面勾選過的選項進行回顯的情況,因為table組件在每次翻頁的時候,都會清空上一頁勾選的選項,在切換回前一頁時,勾選過的選項不會保存。因此需要借助toggleRowSelection()方法設置勾選項。
toggleRowSelection()需要頁面渲染完畢之后才有效,因此需要放在this.$nextTick中,如果只有一頁數(shù)據(jù),這樣就可以了,但是在有多頁數(shù)據(jù)的情況下,每次翻頁就會請求數(shù)據(jù),進行數(shù)據(jù)更新,因此this.$nextTick需要放在updated回調中,等數(shù)據(jù)更新和頁面渲染都完成時,才有效。如下所示。
updated() {
this.$nextTick(() => {
this.tableData.forEach(outerItem => {
this.selectRows[this.page.current - 1] && this.selectRows[this.page.current - 1].forEach(item => {
if(outerItem.d == item.d) {
this.$refs.caseTable.toggleRowSelection(outerItem,true);
}
})
})
})
}還有一個問題,如上所示,tableData中是傳入到table組件中的數(shù)據(jù),而selectRows是我保存的勾選的數(shù)據(jù),按理說數(shù)據(jù)的格式完全一樣,但是我向toggleRowSelection傳遞參數(shù)時,只能傳遞outerItem,也就是傳入table組件的數(shù)據(jù),傳入item則無效。具體原因還不是太清楚,但我想應該是因為兩個引用類型的地址不同引起的。
element-ui中table默認選中toggleRowSelection
一.toggleRowSelection
toggleRowSelection(row, selected)接受兩個參數(shù),row傳遞被勾選行的數(shù)據(jù),selected設置是否選中
注意:調用toggleRowSelection這個方法 需要獲取真實dom 所以需要注冊 ref 來引用它
二.操作
(一).默認選中
1.當數(shù)據(jù)源固定在table的
<script>
export default {
mounted() {
this.$refs.multipleTable.toggleRowSelection(this.tableData3[2],true);
}
}
</script>
(二).點擊tr選中
1.在table中設置 @row-click="handleCurrentChange"
row-click 點擊行事件
<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);//點擊選中
}
}
}
</script>(三).獲取選中的值
1.設置table 中@selection-change="selsChange"
2.data 中設置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 點擊 復選框選中 再次點擊 復選框取消選中
①設置一個全局函數(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 ? '女' : '未知';
},
// 點擊事情
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);//默認選中
}
}
</script>
<style>
</style>
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue-admin-template模板添加tagsview的實現(xiàn)
本文主要介紹了vue-admin-template模板添加tagsview的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04
Vue3使用ref和reactive管理狀態(tài)的代碼示例
ref 和 reactive 是 Composition API 中用來聲明響應式數(shù)據(jù)的兩個核心函數(shù),在 Vue 3 中,使用 setup 語法糖可以更簡潔地使用這些功能,本文將探討如何使用 ref 和 reactive 來管理狀態(tài),并解釋它們之間的區(qū)別,需要的朋友可以參考下2024-09-09
Vue-cli3執(zhí)行serve和build命令時nodejs內存溢出問題及解決
這篇文章主要介紹了Vue-cli3執(zhí)行serve和build命令時nodejs內存溢出問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
在Vue中使用Viser說明(基于AntV-G2可視化引擎)
這篇文章主要介紹了在Vue中使用Viser說明(基于AntV-G2可視化引擎),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10

