咱們再用vue和element-ui,或者其餘的表格的時候,可能須要能記憶翻頁勾選,那麼實現如下幾個方法就ok了vue
示例以下 git
<el-table :data="tableData" ref="table" @selection-change="handleSelectionChange"> <el-table-column type="selection"></el-table-column> <el-table-column prop="personName" label="客戶名稱"></el-table-column> <el-table-column prop="telphone" label="手機號"></el-table-column> <el-table-column prop="idNo" label="身份證號"></el-table-column> <el-table-column prop="linkTypes" label="客戶身份"></el-table-column> </el-table> <el-pagination :page-size="pagination.pageSize" @current-change="currentChange" :current-page="pagination.pageNumber" :page-sizes="pagination.pageSizes" :total="pagination.totalRows" @size-change='sizeChange'> </el-pagination>
首先定義個data值github
data () { return { multipleSelectionAll: [], // 全部選中的數據包含跨頁數據
multipleSelection: [], // 當前頁選中的數據
idKey: 'personId', // 標識列表數據中每一行的惟一鍵的名稱(須要按本身的數據改一下)
tableData: [] // 表格數據
// 此處省略pagination的定義
}
}
方法中定義如下:ajax
methods : {
// 設置選中的方法 setSelectRow() { if (!this.multipleSelectionAll || this.multipleSelectionAll.length <= 0) { return; } // 標識當前行的惟一鍵的名稱 let idKey = this.idKey; let selectAllIds = []; let that = this; this.multipleSelectionAll.forEach(row=>{ selectAllIds.push(row[idKey]); }) this.$refs.table.clearSelection(); for(var i = 0; i < this.tableData.length; i++) { if (selectAllIds.indexOf(this.tableData[i][idKey]) >= 0) {
// 設置選中,記住table組件須要使用ref="table" this.$refs.table.toggleRowSelection(this.tableData[i], true); } } } , // 記憶選擇核心方法 changePageCoreRecordData () { // 標識當前行的惟一鍵的名稱 let idKey = this.idKey; let that = this; // 若是總記憶中尚未選擇的數據,那麼就直接取當前頁選中的數據,不須要後面一系列計算 if (this.multipleSelectionAll.length <= 0) { this.multipleSelectionAll = this.multipleSelection; return; } // 總選擇裏面的key集合 let selectAllIds = []; this.multipleSelectionAll.forEach(row=>{ selectAllIds.push(row[idKey]); }) let selectIds = [] // 獲取當前頁選中的id this.multipleSelection.forEach(row=>{ selectIds.push(row[idKey]); // 若是總選擇裏面不包含當前頁選中的數據,那麼就加入到總選擇集合裏 if (selectAllIds.indexOf(row[idKey]) < 0) { that.multipleSelectionAll.push(row); } }) let noSelectIds = []; // 獲得當前頁沒有選中的id this.tableData.forEach(row=>{ if (selectIds.indexOf(row[idKey]) < 0) { noSelectIds.push(row[idKey]); } }) noSelectIds.forEach(id=>{ if (selectAllIds.indexOf(id) >= 0) { for(let i = 0; i< that.multipleSelectionAll.length; i ++) { if (that.multipleSelectionAll[i][idKey] == id) { // 若是總選擇中有未被選中的,那麼就刪除這條 that.multipleSelectionAll.splice(i, 1); break; } } } }) }, currentChange(val){ // 改變頁的時候調用一次 this.changePageCoreRecordData();
this.pagination.pageNumber = val;
this.query();
}, sizeChange(val){ // 改變每頁顯示條數的時候調用一次
this.changePageCoreRecordData();
this.pagination.pageSize = val;
this.query();
},
handleSelectionChange (val) {// 再執行一次記憶勾選數據匹配,目的是爲了在當前頁操做勾選後直接獲取選中數據
// table組件選中事件,記得加上@selection-change="handleSelectionChange"
this.multipleSelection = val;
}, query () { // 分頁查詢數據方法,在成功返回數據方法裏調用setSelectRow方法,使每次分頁查詢都能勾選中 $.ajax({..., success:(res)=>{
...... setTimeout(()=>{ this.setSelectRow(); }, 200) } }) },
// 獲得選中的全部數據
getAllSelectionData () {
console.log(this.multipleSelectionAll)
}
}
若是你的是自定義組件dialog彈窗裏面的表格選擇,若是想每次打開想選中,那麼就直接在props加一個值,而後加一個watchelement-ui
props: [ "selectData"], watch: { 'selectData' (val) { this.multipleSelectionAll = val; } },
上傳到了GitHub上一個示例,可供參考ui