在vue和element-ui的table中实现分页复选功能

在vue和element-ui的table中实现分页复选功能

这篇文章主要介绍了在vue和element-ui的table中实现分页复选功能,本文代码结合图文的形式给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下。


页面结构如下:

<el-table
 class="table"
 :data="tableData"
 border
 style="width: 100%"
 @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) //50ms延时器中(要等上一次点击操作完成之后在进入下一次)
},
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')
  }
 })
},

总结

element-ui中selection-change方法,得出当前页已选中的数据集合 list1,和当前页未选中的集合 list2,声明一个数据all用于存储所有已选中的数据集合,遍历list1将all中没有的项加入到all中,再遍历list2将all中有的项从all中删除,封装成一个方法后在selection-change方法中调用时放在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值