el-table多选框数据回显

必要属性:1.row-key; 2. reserve-selection ;  3.表格设置ref属性
 

row-key的官网解释:

三个必要属性的使用:

上代码:

getRecruitmentPostList (value) { // 这个value是我在某次调用时传来的参,主要是为了判断调用接口时是否回显多选框
			this.recruitmentPostLoading = true;
			API.getJobList(this.recruitmentPostQueryParams).then(res => {
				if (res.code == '200') {
					this.recruitmentPostTableList = res.rows;
					this.recruitmentPostTotal = res.total;
					this.recruitmentPostLoading = false;
				} else {
					this.recruitmentPostTableList = [];
					this.recruitmentPostTotal = 0;
					this.recruitmentPostLoading = false;
				}
				if (value) {
					this.$refs.recruitmentPostTable.clearSelection() // 我如果没加这一句,会回显失败
					this.jobTableList.forEach(key1 => { // 根据这个数组决定要回显的多选框
						this.$nextTick(() => {
							this.recruitmentPostTableList.forEach(key2 => { // 这个数组是要回显多选框的那个表格数据
								console.log(key1.id == key2.id)
								if (key1.id == key2.id) {
									this.$refs.recruitmentPostTable.toggleRowSelection(key2, true); //这里选择的key2值才有效,如果要是无效可以选择key1值
								}
							})
						})
					})
				}

			});
		},

结束啦 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了将el-table多选框变为单选,可以按照以下步骤进行操作: 1.在el-table中添加一个selection-change事件,用来监听选中项的变化。 2.在事件处理程序中,判断当前选中的行数是否为1,如果不是,则使用table的clearSelection方法清除所有选中的数据,然后使用table的toggleRowSelection方法选中当前行。 3.为了解决数据回显的问题,需要在选中行时将选中的行数据存储到一个变量中,然后在下一次渲染表格时,将该变量中的数据传递给table的selected-rows属性即可。 下面是一个示例代码,可以将其添加到el-table的模板中: ```html <template> <el-table :data="tableData" :row-key="row => row.id" @selection-change="handleSelectionChange" :selected-rows="selectedRows" ref="table" > <el-table-column type="selection" width="55"></el-table-column> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { id: 1, name: '张三', age: 18, address: '北京市' }, { id: 2, name: '李四', age: 20, address: '上海市' }, { id: 3, name: '王五', age: 22, address: '广州市' }, { id: 4, name: '赵六', age: 24, address: '深圳市' } ], selectedRows: [] } }, methods: { handleSelectionChange(val) { if (val.length !== 1) { this.$refs.table.clearSelection() this.$refs.table.toggleRowSelection(val[0]) } else { this.selectedRows = val } } } } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值