使用ant组件中的表格,嵌套表格展开时数据污染,只展开当前行

最近遇到一个棘手的问题,Ant的嵌套表格,点击一行显示子表格的相关数据,展开其他行,共享数据源,便会使所有展开的行显示相同数据!
在这里插入图片描述

在上网找了很多解决办法,试过都不行,Ant也没有仅展开当前行的例子,改变数据源有点复杂,所以我采取的是点击哪行就只展开该行,关闭其他行,视图上就不会影响用户体验!
话不多说,直接上代码:
查阅了Ant的表格API,expandedRowKeys展开的行,控制属性
在这里插入图片描述

第一步:在表格中加上expandedRowKeys控制展开行的数据

<a-table ref='saleTable' :columns="columns" expandRowByClick(点击行展开)  :expandedRowKeys="openRowKeys"(展开哪一行)  rowKey="SaleId"(row的唯一标识,这个很关键) @expand="expand"(点击图标展开)>
</a-table>

第二步:在data中定义openRowKeys

data(){
	 return{
	 	openRowKeys:"[]",
	}
}

第三步:在data中定义openRowKeys

//点击行
expand(expanded, record) {
	this.openRowKeys="[]"; //关闭其他行
	if (expanded) {
		//去发送接口,展开items
		this.openRowKeys=`"[${record.SaleId}]"`;
		this.$api.saleitems(record.SaleId).then(res => {
			调axios接口,渲染子表格数据
		});
	}
}

问题就解决啦,就可以只展开一行,实现手风琴效果啦~~~

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以通过在表格使用 expandable 和 rowSelection 属性来实现表格嵌套表格,支持展开和全选。以下是一个基本的示例: ```html <template> <a-table :columns="columns" :data-source="data" :expandable="expandable" :row-selection="rowSelection"> <template #expanded-row="{record}"> <a-table :columns="innerColumns" :data-source="record.children" /> </template> </a-table> </template> <script> import { Table } from 'ant-design-vue'; export default { components: { Table }, data() { return { columns: [ { title: '姓名', dataIndex: 'name' }, { title: '年龄', dataIndex: 'age' }, { title: '性别', dataIndex: 'gender' }, ], innerColumns: [ { title: '学科', dataIndex: 'subject' }, { title: '成绩', dataIndex: 'score' }, ], data: [ { key: 1, name: '张三', age: 18, gender: '男', children: [ { key: 11, subject: '语文', score: 85 }, { key: 12, subject: '数学', score: 90 }, ]}, { key: 2, name: '李四', age: 20, gender: '女', children: [ { key: 21, subject: '语文', score: 80 }, { key: 22, subject: '数学', score: 95 }, ]}, ], expandable: { defaultExpandAllRows: true, }, rowSelection: { type: 'checkbox', onSelectAll: (selected, selectedRows, changeRows) => { console.log(selected, selectedRows, changeRows); }, }, }; }, }; </script> ``` 在上面的示例,我们在外层表格设置了 expandable 属性为 `{ defaultExpandAllRows: true }`,使得表格默认展开全部,同在内层表格使用了 a-table 组件来展示子表格。 我们还设置了 rowSelection 属性,使得表格支持全选功能,当用户选或取消选,会触发 onSelectAll 回调函数,可以在该函数处理选数据

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值