vxe-table 可单选,可多选, 以及分页刷新后选中消失问题

<vxe-table
	ref="xTable"
	:data="tableObj.data"
	:loading="tableObj.loading"
	border
	resizable
	auto-resize
	height="100%"
	size="small"
	highlight-current-row
	highlight-hover-row
	show-overflow="tooltip"
	show-header-overflow="tooltip"
	row-id="id"
	:row-config="{ keyField: 'id', isCurrent: true, isHover: true }"
	:radio-config="{ reserve: true, trigger: 'row' }"
	:checkbox-config="{ reserve: true, trigger: 'row' }"
	@radio-change="handleRadioChange"
	@checkbox-all="checkboxAll"
	@checkbox-change="changeCheckBox"
>
	<vxe-column type="radio" width="80" align="center" :visible="!multiple">
		<template #header>
			<vxe-button type="text" @click="clearRadioRow">取消选中</vxe-button>
		</template>
	</vxe-column>
	<vxe-column type="checkbox" width="80" align="center" :visible="multiple"></vxe-column>
	<vxe-table-column field="sname" title="姓名" min-width="150" align="left"></vxe-table-column>
</vxe-table>
// 打开弹框,传入回显的数据
open(data) {
	if (!this.multiple) {
		this.selectObj = this.$XEUtils.clone(data, true)
	} else {
		this.selectArr = this.$XEUtils.clone(data, true)
	}
	this.dialogObj.show = true
	this.getTableData()
},

// 获取表格
getTableData() {
	this.tableObj.data = res.data
	this.copyData = res.data // 当前端过滤查询条件,需要复制一份
	
	// 单选且已经选过
	if (!this.multiple && this.selectObj.id) {
		const find = this.tableObj.data.find((i) => i.id === this.selectObj.id)
		this.$refs.xTable.setRadioRow(find)
	}
	// 多选且已选过
	if (this.multiple && this.selectArr.length > 0) {
		this.$refs.xTable.setCheckboxRow(this.selectArr, true)
	}

	//  当数据量多时       :checkbox-config="{ checkField: 'checked', reserve: true, trigger: 'row' }"
	// this.tableObj.data = res.data.map((i) => {
	// 	return { ...i, checked: false }
	// })
	// // 单选且已经选过
	// if (!this.multiple && this.selectObj.id) {
	// 	const find = this.tableObj.data.find((i) => i.id === this.selectObj.id)
	// 	this.$refs.xTable.setRadioRow(find)
	// }
	// // 多选且已选过
	// if (this.multiple && this.selectArr.length > 0) {
	// 	this.$refs.xTable.setCheckboxRow(this.selectArr, true)
	// 	this.tableObj.data = res.data.map((i) => {
	// 		return {
	// 			...i,
	// 			checked: this.selectArr.find((item) => item.id === i.id)
	// 		}
	// 	})
	// }
	setTimeout(() => {
		this.$refs.xTable && this.$refs.xTable.refreshColumn()
	}, 200)
},

// 名称input查询
doSearch() {
	// 第一种 前端过滤
	const str = this.searchStr.trim()
	if (!!str) {
		this.tableObj.data = this.copyData.filter((i) => i.name.includes(str))
	} else {
		this.tableObj.data = this.copyData
	}
	setTimeout(() => {
		this.$refs.checkTable.setCheckboxRow(this.selectArr, true)
	}, 200)
	
	// 第二种 接口查询 this.getTableData()
},

// 单选change
handleRadioChange() {
	this.selectObj = this.$refs.xTable.getRadioRecord()
},

// 取消单选选中
clearRadioRow() {
	this.$refs.xTable.clearRadioRow()
	this.selectObj = {}
},

// 复选框change
changeCheckBox({ checked, row }) {
	const findIndex = this.selectArr.findIndex((i) => i.id === row.id)
	// 当前行已勾选,现取消勾选,从数组中删除
	if (findIndex > -1 && !checked) {
		this.selectArr.splice(findIndex, 1)
		// 没有全选的情况下,限制选中个数
		// if (this.selectArr.length <= 1) {
		// 	 this.$message.warning('至少选1个!')
		// 	 this.$refs.checkTable.toggleCheckboxRow(row)
		// } else {
		// 	 this.selectArr.splice(findIndex, 1)
		// }
	}
	// 当前行未勾选,现勾选,加入数组
	if (findIndex <= -1 && checked) {
		this.selectArr.push(row)
		// 没有全选的情况下,限制选中个数
		// if (this.selectArr.length >= 5) {
		// 	 this.$message.warning('最多选5个!')
		// 	 this.$refs.checkTable.toggleCheckboxRow(row)
		// } else {
		// 	 this.selectArr.push(row)
		// }
	}
},

// 复选框全选
checkboxAll({ checked }) {
	this.tableObj.data.map((item) => {
		const findIndex = this.selectArr.findIndex((i) => i.id === item.id)
		if (findIndex > -1 && !checked) {
			this.selectArr.splice(findIndex, 1)
		}
		if (findIndex <= -1 && checked) {
			this.selectArr.push(item)
		}
	})
},
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vxe-table是一个基于vue的表格组件,支持增删改查、虚拟滚动、懒加载、快捷菜单、数据校验、树形结构、打印导出、表单渲染、数据分页、模态窗口、自定义模板、灵活的配置项、丰富的扩展插件等... 设计理念: 面向现代浏览器,高效的简洁 API 设计 模块化表格、按需加载、插件化扩展 为单行编辑表格而设计,支持增删改查及更多扩展,强大的功能的同时兼具性能 功能: Basic table (基础表格) Grid (高级表格) Size (尺寸) Striped (斑马线条纹) Table with border (带边框) Cell style (单元格样式) Column resizable (列宽拖动) Maximum table height (最大高度) Resize height and width (响应式宽高) Fixed column (固定列) Grouping table head (表头分组) Highlight row and column (高亮行、列) Table sequence (序号) Radio (单选) Checkbox (多选) Sorting (排序) Filter (筛选) Rowspan and colspan (合并行或列) Footer summary (表尾合计) Import (导入) Export (导出) Print (打印) Show/Hide column (显示/隐藏列) Loading (加载中) Formatted content (格式化内容) Custom template (自定义模板) Context menu(快捷菜单) Virtual Scroller(虚拟滚动) Expandable row (展开行) Pager(分页) Form(表单) Toolbar(工具栏) Tree table (树形表格) Editable CRUD(增删改查) Validate(数据校验) Data Proxy(数据代理) Keyboard navigation(键盘导航) Modal window(模态窗口) Charts(图表工具) 更新日志: v4.0.20 table 修改单选框、复选框获取值错误问题 grid 修复 KeepAlive 中报错问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值