背景
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的慶元網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
后臺(tái)管理系統(tǒng)中,使用表格展示數(shù)據(jù)時(shí),可能的需求是多項(xiàng)選擇然后進(jìn)行批量操作,也期望能翻頁(yè)多選。
實(shí)現(xiàn)
頁(yè)面結(jié)構(gòu)如下
<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>
模擬數(shù)據(jù)實(shí)現(xiàn)分頁(yè)
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 }, }
展示已選擇項(xiàng)
<div class="result"> 已選:{{ allMultipleSelection }} </div> allMultipleSelection: [],
在復(fù)選事件中對(duì)所選項(xiàng)進(jìn)行存儲(chǔ)
主要思路就是:
handleSelectionChange (val) { this.multipleSelection = val // @tip 實(shí)現(xiàn)分頁(yè)復(fù)選 console.log(val, 'selection') setTimeout(_ => { this.resolveAllSelection() }, 50) }, resolveAllSelection () { let currentPageData = this.tableData.map(item => item[this.uniqueKey]) // 當(dāng)前頁(yè)所有數(shù)據(jù) let currentPageSelected = this.multipleSelection.map(item => item[this.uniqueKey]) // 當(dāng)前頁(yè)已選數(shù)據(jù) let currentPageNotSelected = currentPageData.filter(item => !currentPageSelected.includes(item)) // 當(dāng)前頁(yè)未選數(shù)據(jù) // 將當(dāng)前頁(yè)已選數(shù)據(jù)放入所有已選項(xiàng) currentPageSelected.forEach(item => { if (!this.allMultipleSelection.includes(item)) { this.allMultipleSelection.push(item) } }) // 將所有已選項(xiàng)數(shù)據(jù)中當(dāng)前頁(yè)沒(méi)選擇的項(xiàng)移除 currentPageNotSelected.forEach(item => { let idx = this.allMultipleSelection.indexOf(item) if (idx > -1) { this.allMultipleSelection.splice(idx, 1) } }) console.log(this.allMultipleSelection, 'all') },
此時(shí)還需要在切換頁(yè)面時(shí)將之間選擇項(xiàng)進(jìn)行重新選中,即遍歷當(dāng)前頁(yè)所有數(shù)據(jù)如果存在于所有已選項(xiàng)中,則將其置為已選擇。
fetchData () { // ... setTimeout(_ => { // ... // @tip 實(shí)現(xiàn)分頁(yè)復(fù)選 setTimeout(_ => { this.setSelectedRow() }, 50) }, 200) }, setSelectedRow () { // 設(shè)置當(dāng)前頁(yè)已選項(xiàng) this.tableData.forEach(item => { if (this.allMultipleSelection.includes(item[this.uniqueKey])) { this.$refs.asTable.toggleRowSelection(item, true) console.log(item[this.uniqueKey], 'set') } }) },
以上實(shí)現(xiàn)了分頁(yè)復(fù)選功能。
所有代碼存放在 @careteen/lan-vue
查看DEMO
clone倉(cāng)庫(kù)并引入依賴
git clone git@github.com:careteenL/lan-vue.git npm install npm run serve
瀏覽器打開(kāi) http://localhost:8080/#/examples/pagingCheck 即可看到效果
總結(jié)
以上所述是小編給大家介紹的在vue和element-ui的table中實(shí)現(xiàn)分頁(yè)復(fù)選功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
本文標(biāo)題:在vue和element-ui的table中實(shí)現(xiàn)分頁(yè)復(fù)選功能
文章路徑:http://chinadenli.net/article4/gissie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)站改版、做網(wǎng)站、靜態(tài)網(wǎng)站、ChatGPT、網(wǎng)頁(yè)設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)