element-ui-table实现分页后保留之前checkbox选中数据

element-ui 的table表格可以轻松实现多选的功能,只要在表格列中增加type="selection"的一列即可。

但是大部分情况下,表格的数据是有分页的,分页一般是要请求后台接口,这样上一页也就是上一次请求的数据的选中状态就没有了。element-ui提供了reserve-selection,可以保存数据更新前选中的值,这个属性还需要指定row-key。

<template>
	<div>
		<el-table ref="userTable" 
			:data="userList" 
			:row-key="getRowKeys"
			@selection-change="userSelectionChange">
			<el-table-column type="selection" :reserve-selection="true"></el-table-column>
			<el-table-column prop="account" label="账号"></el-table-column>
			<el-table-column prop="phone" label="手机"></el-table-column>
			<el-table-column prop="email" label="邮箱"></el-table-column>		
		</el-table>
		<el-pagination
			@size-change="handleSizeChange"
			@current-change="handleCurrentChange"
			:current-page="userPageObj.currentPage"
			:page-sizes="[10,20,30]"
			:page-size="userPageObj.pageSize"
			layout="total, sizes, prev, pager, next"
			:total="userPageObj.total">
		</el-pagination>
	</div>
</template>

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
<script>
	export default {
		data() {
			return {
				userList: [],
				userPageObj: {
					currentPage: 1,  //当前页
					pageSize: 10,  //每页条数
					total: 0,  //数据总数
				},
			}
		},
		methods: {
			//加载用户列表
			getUserList() {
				//调接口,赋值,具体细节省略
			},
			getRowKeys(row) {
				return row.account;
			},
			userSelectionChange(arr) {
				console.log(arr);
			},
		},
		mounted() {
			this.getUserList();
		},
	}
</script>

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

假如第1页选中3项,切换分页到第2页选中2项,看最后console.log(arr)的数据:

如果不设置reserve-selection=“true”,则最终console的结果是:第2页的2条数据;

如果设置reserve-selection=“true”,则最终console的结果是:第1页的3条数据 + 第2页的2条数据 = 5条数据。

VueElement UITable组件中实现分页复选功能,可以按照以下步骤进行操作: 1. 在Vue组件中引入Element UITableCheckbox组件: ```javascript import { Table, Checkbox } from 'element-ui'; ``` 2. 在data中定义需要用到的变量: ```javascript data() { return { // 表格数据 tableData: [], // 选中的行数据 selectedRows: [], // 是否全选 isAllSelected: false } } ``` 3. 在模板中使用TableCheckbox组件,并设置相应属性和事件: ```html <template> <div> <el-table :data="tableData" @row-click="handleRowClick"> <el-table-column type="selection" width="55"> <template slot-scope="scope"> <el-checkbox v-model="selectedRows" :label="scope.row" @change="handleCheckboxChange"></el-checkbox> </template> </el-table-column> <!-- 其他列定义--> </el-table> </div> </template> ``` 4. 在methods中定义事件处理函数: ```javascript methods: { // 单击行选中/取消选中 handleRowClick(row) { const index = this.selectedRows.indexOf(row); if (index > -1) { this.selectedRows.splice(index, 1); } else { this.selectedRows.push(row); } }, // 全选/取消全选 handleCheckboxChange(value) { if (value.length === this.tableData.length) { this.isAllSelected = true; } else { this.isAllSelected = false; } } } ``` 5. 在模板中使用全选Checkbox,并绑定isAllSelected属性: ```html <el-checkbox v-model="isAllSelected" @change="handleSelectAllChange">全选</el-checkbox> ``` 6. 在methods中定义全选Checkbox的 change 事件: ```javascript methods: { // 全选/取消全选 handleSelectAllChange(value) { if (value) { this.selectedRows = [...this.tableData]; } else { this.selectedRows = []; } } } ``` 以上就是在VueElement UITable实现分页复选功能的步骤。通过监听行点击事件,可以实现点击行选中/取消选中的功能;通过绑定Checkbox的v-model和change事件,可以实现全选功能;通过管理选中的行数据,可以获取用户选择的数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值