背景
后臺管理系統中,使用表格展示數據時,可能的需求是多項選擇然后進行批量操作,也期望能翻頁多選。
實現
頁面結構如下
<el-table class="table" :data="tableData" border @selection-change="handleSelectionChange" ref="asTable" > <el-table-column width="50" type="selection" fixed="left" > </el-table-column> <el-table-column prop="id" label="ID" > </el-table-column> <el-table-column prop="name" label="名字" > </el-table-column> <el-table-column label="操作" width="100" > <template slot-scope="scope"> <el-button type="primary" plain size="small" @click="handleEdit(scope.row)" >編輯</el-button> </template> </el-table-column> </el-table> <el-pagination background @current-change="handleCurrentChange" :current-page.sync="pagination.currentPage" :page-size="pagination.size" layout="total, prev, pager, next, jumper" :total="pagination.total" slot="pagination" > </el-pagination>
模擬數據實現分頁
data () {
return {
tableData: [],
multipleSelection: [],
pagination: {
currentPage: 1,
size: 10,
total: 1000
}
}
},
beforeMount () {
this.fetchData()
},
methods: {
fetchData () {
this.tableData = []
let start = (this.pagination.currentPage - 1) * this.pagination.size
let end = this.pagination.currentPage * this.pagination.size
setTimeout(_ => {
for (let i = start; i < end; i++) {
this.tableData.push({
id: i,
name: `王${i}蘭`
})
}
}
},
handleCurrentChange () {
this.fetchData()
},
handleSelectionChange (val) {
this.multipleSelection = val
},
}
展示已選擇項
<div class="result">
已選:{{ allMultipleSelection }}
</div>
allMultipleSelection: [],
在復選事件中對所選項進行存儲
主要思路就是:
handleSelectionChange (val) {
this.multipleSelection = val
// @tip 實現分頁復選
console.log(val, 'selection')
setTimeout(_ => {
this.resolveAllSelection()
}, 50)
},
resolveAllSelection () {
let currentPageData = this.tableData.map(item => item[this.uniqueKey]) // 當前頁所有數據
let currentPageSelected = this.multipleSelection.map(item => item[this.uniqueKey]) // 當前頁已選數據
let currentPageNotSelected = currentPageData.filter(item => !currentPageSelected.includes(item)) // 當前頁未選數據
// 將當前頁已選數據放入所有已選項
currentPageSelected.forEach(item => {
if (!this.allMultipleSelection.includes(item)) {
this.allMultipleSelection.push(item)
}
})
// 將所有已選項數據中當前頁沒選擇的項移除
currentPageNotSelected.forEach(item => {
let idx = this.allMultipleSelection.indexOf(item)
if (idx > -1) {
this.allMultipleSelection.splice(idx, 1)
}
})
console.log(this.allMultipleSelection, 'all')
},
此時還需要在切換頁面時將之間選擇項進行重新選中,即遍歷當前頁所有數據如果存在于所有已選項中,則將其置為已選擇。
fetchData () {
// ...
setTimeout(_ => {
// ...
// @tip 實現分頁復選
setTimeout(_ => {
this.setSelectedRow()
}, 50)
}, 200)
},
setSelectedRow () {
// 設置當前頁已選項
this.tableData.forEach(item => {
if (this.allMultipleSelection.includes(item[this.uniqueKey])) {
this.$refs.asTable.toggleRowSelection(item, true)
console.log(item[this.uniqueKey], 'set')
}
})
},
以上實現了分頁復選功能。
所有代碼存放在 @careteen/lan-vue
查看DEMO
clone倉庫并引入依賴
git clone git@github.com:careteenL/lan-vue.git npm install npm run serve
瀏覽器打開 http://localhost:8080/#/examples/pagingCheck 即可看到效果
總結
以上所述是小編給大家介紹的在vue和element-ui的table中實現分頁復選功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。