首页IT科技antdesign vue table(保姆级教程:Ant Design Vue中 a-table 嵌套子表格)

antdesign vue table(保姆级教程:Ant Design Vue中 a-table 嵌套子表格)

时间2025-05-03 17:56:23分类IT科技浏览3724
导读:前端为Ant Design Vue 版本为1.6.2,使用的是vue2...

前端为Ant Design Vue 版本为1.6.2            ,使用的是vue2

Ant Design Vue中 a-table 嵌套子表格                ,说的可能稍微墨迹了点    ,不过重点内容都说的比较详细         ,利于新人理解                 ,高手可以自取完整代码

完成样式及完整代码展示

下图为官网图      ,会在每行最前面多一个加号      ,点击后会展开                  ,看到子表格的数据

<template> <div> <a-card> <a-table :columns="columns" :data-source="datasource" :bordered="true" :expandedRowKeys="expandedRowKeys" @expand="getInnerData" :pagination="pagination"> <a-table slot="expandedRowRender" slot-scope="text" :columns="innerColumns" :data-source="inndata" :pagination="false"></a-table> <span slot="operation" slot-scope="text, record"> <a-button type="primary" icon="unordered-list" @click="getData(record)"> 详情 </a-button> </span> </a-table> </a-card> </div> </template> <script> import { axios } from @/utils/request const columns = [ { title: 序号, dataIndex: id, key: id, },           。                。      。        。               。中间省略 { title: 操作, key: operation, width: 80px, scopedSlots: { customRender: operation }, }, ]; const innerColumns = [ { title: 子表单列, dataIndex: XXX, key: XXX, }, ]; const inndata = []; export default { data() { return { datasource: [], columns, innerColumns, inndata, expandedRowKeys:[], pagination: { //分页 showSizeChanger: true, //是否分页 curPageSizeIndex: 0, pageSize: 10, //一页显示多少条 // total: 1, //总条数 current: 1, //当前显示页面 pageSizeOptions: [10, 20, 30], showTotal: (total) => `共计 ${total}条`, //显示总数 onShowSizeChange: (current, pagesize) => { this.pagination.current = current this.pagination.pageSize = pagesize this.options.offset = (current - 1) * pagesize this.options.limit = pagesize this.refreshData() }, onChange: (current, pageSize) => { this.pagination.current = current this.options.offset = (current - 1) * pageSize this.options.limit = pageSize this.refreshData() }, }, } }, mounted() { this.refreshData() }, methods: { refreshData() { axios({ url: url, method: post, }).then((res) => { if (res.errCode > 0) { this.$notification.error({ message: 错误信息, description: res.errMsg, }) return } console.log(res, 数据) this.datasource = res }) }, getInnerData(expanded, record) { this.expandedRowKeys=[] if (expanded) { var b = b=(record.id-1).toString() this.expandedRowKeys.push(Number(b.slice(-1))) this.inndata = []; this.inndata.push(this.datasource[record.id - 1]) } }, getData(record) { console.log(record); }, }, } </script> <style scoped> </style>

子表格嵌套

子格嵌套从代码层简单理解就是一个中套了一个         ,即   ,当然                  ,这只是一个简单理解            ,并不是这样写,要在其中添加一些东西         。

抛开一些配置项               ,解释下: <template> <div> <a-card> // 这里开始是父表格组件               ,columns -> 父级列名  ,datasource -> 父级表中的数据            , @expand="getInnerData" -> 点开子表单的操作函数                ,pagination -> 分页的设置 <a-table :columns="columns" :data-source="datasource" @expand="getInnerData" :pagination="pagination"> // 这里开始是子表格组件    ,slot="expandedRowRender" 重点         ,必须有                 ,innerColumns-> 子级列名      ,inndata-> 子级表中的数据      , <a-table slot="expandedRowRender" slot-scope="text" :columns="innerColumns" :data-source="inndata" :pagination="false"></a-table>// 这里是子表格组件结尾 // 这里是父表格span                  ,不用的可以删掉 <span slot="operation" slot-scope="text, record"> <a-button type="primary" icon="unordered-list" @click="getData(record)"> 详情 </a-button> </span> </a-table>// 这里是父表格组件结尾 </a-card> </div> </template> <script> // 数据请求axios import { axios } from @/utils/request // 设置父级列名 const columns = [ { title: 序号, dataIndex: id, key: id, },       。               。            。   。               。中间省略 { title: 操作, key: operation, width: 80px, scopedSlots: { customRender: operation }, }, ]; // 设置子级列名 const innerColumns = [ { title: 子表单列, dataIndex: XXX, key: XXX, }, ]; // 设置子级数据         ,写下面data里也行 const inndata = []; export default { data() { return { datasource: [], columns, innerColumns, inndata, pagination: { //分页 showSizeChanger: true, //是否分页 curPageSizeIndex: 0, pageSize: 10, //一页显示多少条 // total: 1, //总条数 current: 1, //当前显示页面 pageSizeOptions: [10, 20, 30], showTotal: (total) => `共计 ${total}条`, //显示总数 onShowSizeChange: (current, pagesize) => { this.pagination.current = current this.pagination.pageSize = pagesize this.options.offset = (current - 1) * pagesize this.options.limit = pagesize this.refreshData() }, onChange: (current, pageSize) => { this.pagination.current = current this.options.offset = (current - 1) * pageSize this.options.limit = pageSize this.refreshData() }, }, } }, mounted() { // 进入页面直接运行的函数 this.refreshData() }, methods: { // 异步请求获取数据 refreshData() { axios({ url: url, method: post, }).then((res) => { if (res.errCode > 0) { this.$notification.error({ message: 错误信息, description: res.errMsg, }) return } console.log(res, 数据) // 将数据赋值给父级表格 this.datasource = res }) }, // 点开子级表格(就是点击那个加号)触发的函数 getInnerData(expanded, record) { // 判断是否点开 if (expanded) { // 点开后执行               。。            。 // 先将子级表格数据清空   ,否则之前点开别的行的数据也会在里面 this.inndata = []; // 再将父级点击的那一行的数据(record)给子级数据存进去                  ,具体情况根据自己需求更改 this.inndata.push(record) } }, // 点击父级表格表格span            ,也就是那个“详情          ”按钮触发的函数 getData(record) { console.log(record); }, }, } </script> <style scoped> </style>

通过上述操作会发现能够实现基本的子表格嵌套,但是会发现点开一个子表单后               ,再点一个               ,两个都会打开  ,但是里面的子表格数据是相同的            ,这就是个很大的问题                ,为了解决这个问题    ,请看下面         ,这个在官网没直接写出来                 ,API里有相关的参数      ,现在告诉你应该怎样操作

只打开一个嵌套表格

为了防止点开多个子表格内容相同      ,再结合我自己的使用场景                  ,我只需要展开一个子表格就行         ,也就是说点开新的子表格后   ,原来的子表格自动关闭

下面把需要改动的代码片段给你们:

1. 父级中的修改内容 // 添加 :expandedRowKeys="expandedRowKeys" 目的是使expandedRowKeys只有最新点开子表单的key <a-table :columns="columns" :data-source="datasource" :bordered="true" :expandedRowKeys="expandedRowKeys" @expand="getInnerData" :pagination="pagination"> <a-table slot="expandedRowRender" slot-scope="text" :columns="innerColumns" :data-source="inndata" :pagination="false"></a-table> <span slot="operation" slot-scope="text, record"> <a-button type="primary" icon="unordered-list" @click="getData(record)"> 详情 </a-button> </span> </a-table>

2. 在data里加上空的 expandedRowKeys

export default { data() { return { datasource: [], columns, innerColumns, inndata, // 添加空的expandedRowKeys expandedRowKeys:[],                  。   。          。                。      。        。

3. 修改getInnerData函数

getInnerData(expanded, record) { // 先将expandedRowKeys数据清空 this.expandedRowKeys=[] if (expanded) { // 将父级的第几条数据传到expandedRowKeys里                  ,我这用的是数据表里的id处理的            ,我的id是从1开始 // 我这是一页十条数据,要放入0-9               ,第一条是0               ,第十条是9  ,一定要是数字格式 // 再一个            ,注意下                ,假设使用的是数据中的第45条    ,record.id=45 // 但是传入 expandedRowKeys 只能是0-9         ,且是数字格式                 ,那么应该将数字4传到expandedRowKeys中 // 因为record.id=45这条数据      ,在表格中的第五行      ,应该是4 // 所以有了下面的record.id-1                  ,再转成字符串格式         ,取最后一位   ,再转成数字 // 也有人用key,id能够获取到                  ,不过我没弄成功            ,就这样吧,实现功能就行 var b=(record.id-1).toString() this.expandedRowKeys.push(Number(b.slice(-1))) // 下面两行说过               ,我就把注释直接粘过来了 // 先将子级表格数据清空               ,否则之前点开别的行的数据也会在里面 this.inndata = []; // 再将父级点击的那一行的数据(record)给子级数据存进去  ,具体情况根据自己需求更改 this.inndata.push(record) } },
声明:本站所有文章            ,如无特殊说明或标注                ,均为本站原创发布               。任何个人或组织    ,在未征得本站同意时         ,禁止复制            、盗用                、采集    、发布本站内容到任何网站         、书籍等各类媒体平台         。如若本站内容侵犯了原著者的合法权益                 ,可联系我们进行处理      。

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
网创平台(网创什么最赚-互联网上哪些赚钱平台信誉比较好?比较挣钱的网创平台选择) 如何应对百度算法对网站SEO的惩罚(探究百度算法对SEO的影响及应对策略)