当前位置: 首页 > article >正文

ant-design-vue中table组件复选框分页切换保留之前选中数据

ant-design-vue中table组件复选框分页切换保留之前选中数据

1、vue2的写法

(1)template片段

<a-table
   :dataSource="dataSource"
   :loading="loading"
   :columns="columns"
   :pagination="false"
   :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
   :row-key="(record) => record.orgCode"
  >
</a-table>
<a-pagination
   align="center"
   v-model:current="pagination.current"
   v-model:page-size="pagination.pageSize"
   :total="pagination.total"
   @change="handlePageChange"
   :show-total="(total) => `共 ${total} 条`"/>

(2)script片段

<script>
import { getOrgList } from '@/api/spotCheck'

export default {
	data() {
		return {
			dataSource: [],
			allDataSource: [],
			loading: false,
			columns: [
				{
					title: '序号',
					dataIndex: 'rowIndex',
					key: 'rowIndex',
					align: 'center'
				},
				{
					title: '机构名称',
					dataIndex: 'orgName',
					key: 'orgName',
					align: 'center'
				},
				{
					title: '机构等级',
					dataIndex: 'regionalLevelName',
					key: 'regionalLevelName',
					align: 'center'
				},
				{
					title: '机构性质',
					dataIndex: 'orgAtt',
					key: 'orgAtt',
					align: 'center'
				}
			],
			selectedRowKeys: [],
			allSelectedRows: { 0: [] },
			pagination: {
				current: 1,
				pageSize: 10,
				total: 0
			},
		}
	},
	methods: {
		reqOrgList() {
			this.loading = true
			getOrgList()
				.then((res) => {
					const {
						data: { records, total }
					} = res
					this.allDataSource = records
					this.pagination.total = total
					if (this.pagination.total > this.pagination.pageSize) {
						this.dataSource = result.slice(0, this.pagination.pageSize)
					} else {
						this.dataSource = result
					}
				})
				.finally(() => {
					this.loading = false
				})
		},
		// 复选框操作
		onSelectChange(selectedRowKeys, selectedRows) {
			this.selectedRowKeys = selectedRowKeys
			this.allSelectedRows[this.pagination.current - 1] = selectedRows
		},
		// 分页操作
		handlePageChange(event) {
			this.dataSource = this.allDataSource?.slice(
				this.pagination.pageSize * (event - 1),
				this.pagination.pageSize * event
			)
			let allSelectRowsArr = []
			for (let key in this.allSelectedRows) {
				allSelectRowsArr.push(...this.allSelectedRows[key])
			}
			const allSelectKeys = allSelectRowsArr.map((item) => item.orgCode)
			if (allSelectKeys.length) {
				this.selectedRowKeys = this.dataSource
					?.filter((item) => allSelectKeys.includes(item.orgCode))
					?.map((it) => it.orgCode)
			}
		},
	}
}
</script>

2、vue3写法

(1)template片段

<a-table
   class="table-margin"
   :dataSource="dataSource"
   :columns="columns"
   :pagination="false"
   :row-selection="{ selectedRowKeys: state.selectedRowKeys, onChange }"
   :row-key="(record) => record.orgCode"
   :loading="tableLoading">
</a-table>
<a-pagination
    style="margin-top: 24px"
    align="center"
    v-model:current="pagination.current"
    v-model:page-size="pagination.pageSize"
    :showSizeChanger="false"
    :total="pagination.total"
    :show-total="(total) => `共 ${total} 条`"
    @change="getOrgInfoList"
/>

(2)script片段

<script setup>
import { onMounted, reactive, ref } from 'vue'
import { get_userinfo} from '@/api/qualityControlManage/organization/organization'
const dataSource = ref([])
const columns = ref([
	{
		title: '序号',
		dataIndex: 'rowKey',
		key: 'rowKey',
		align: 'center'
	},
	{
		title: '机构名称',
		dataIndex: 'orgName',
		key: 'orgName',
		align: 'center'
	},
	{
		title: '机构编码',
		dataIndex: 'orgCode',
		key: 'orgCode',
		align: 'center'
	},
	{
		title: '机构等级',
		dataIndex: 'orgLevelName',
		key: 'orgLevelName',
		align: 'center'
	},
	{
		title: '机构性质',
		dataIndex: 'orgAtt',
		key: 'orgAtt',
		align: 'center'
	},
	{
		title: '机构管理员',
		dataIndex: 'orgAdmin',
		key: 'orgAdmin',
		align: 'center'
	},
	{
		title: '操作',
		dataIndex: 'operate',
		key: 'operate',
		align: 'center'
	}
])

let pagination = reactive({
	current: 1,
	pageSize: 10,
	total: 0
})
let tableLoading = ref(false)

const state = reactive({
	selectedRowKeys: [],
	allSelectedRows: {}
})

const onChange = (selectedRowKeys, selectedRows) => {
	state.selectedRowKeys = selectedRowKeys
	state.allSelectedRows[pagination.current] = selectedRows
}

onMounted(() => {
	getOrgInfoList()
})

// 获取机构信息列表
const getOrgInfoList = () => {
	tableLoading.value = true
	const params = {
		size: pagination.pageSize,
		current: pagination.current,
		orgName: orgNameValue.value
	}
	get_userinfo(params)
		.then((res) => {
			const {
				data: { records, total }
			} = res
			dataSource.value = records
			pagination.total = total
            // 当前界面数据复选框是否选中
			let allSelectRowsArr = []
			for (let key in state.allSelectedRows) {
				allSelectRowsArr.push(...state.allSelectedRows[key])
			}
			const allSelectKeys = allSelectRowsArr.map((item) => item.orgCode)
			if (allSelectKeys.length) {
				state.selectedRowKeys = dataSource.value
					?.filter((item) => allSelectKeys.includes(item.orgCode))
					?.map((it) => it.orgCode)
			}
		})
		.finally(() => {
			tableLoading.value = false
		})
}
</script>

http://www.kler.cn/news/326374.html

相关文章:

  • 网络工程和信息安全专业应该考哪些证书?
  • Python每次for循环向list中添加多个元素
  • 【echarts】报错series.render is required.
  • 【ZYNQ 开发】填坑!双核数据采集系统LWIP TCP发送,运行一段时间不再发送且无法ping通的问题解决
  • You are not allowed to push code to this project
  • Docker 安装 ClickHouse 教程
  • Composition API 与 React Hook 的区别
  • Java LeetCode每日一题(2024.9.26)
  • Unity开发绘画板——04.笔刷大小调节
  • 智能AI对话绘画二合一网站源码系统 带完整的安装代码包以及搭建部署教程
  • XPath入门
  • 65.【C语言】联合体
  • Python | Leetcode Python题解之第442题数组中重复的数据
  • plt注解相关介绍及应用
  • 封装提示词翻译组件
  • K8S:开源容器编排平台,助力高效稳定的容器化应用管理
  • 开放词汇目标检测
  • Unity实战案例全解析:RTS游戏的框选和阵型功能(4)阵型功能
  • 【单元测试】任务1:白盒测试1
  • 完成UI界面的绘制
  • DRF实操学习——购物车及订单生成
  • 【Redis 源码】1下载与源码编译
  • 使用CAPTCHA对反爬虫有优势吗
  • java 解析excel (网络资源)
  • Matlab|计及需求响应消纳风电的电热综合能源系统经济调度
  • 防火墙的区域划分+来自公网、内网的ip欺骗攻击+防御
  • 24.9.25学习笔记
  • 语音识别控制(软件、硬件)
  • 【Pytorch图像+序列双输入网络源代码】
  • mac 触控板 三指拖动