vue antd table嵌套表格 左侧展开图标动态控制显示隐藏

本文介绍了如何在AntDesign4版本的表格组件中,通过自定义模板实现当有子级数据时显示展开图标,无子级时隐藏图标的功能,以及相关的代码示例。
摘要由CSDN通过智能技术生成

antd a-table想要实现如以下效果,有子级就显示展开图标,没有就不显示图标:

话不多说,直接上代码:

<template>
  <a-table :columns="columns" :data-source="dataSource">
    <template #bodyCell="{ column }">
      <template v-if="column.key === 'operation'">
        <a>Publish</a>
      </template>
    </template>

    <template #expandIcon="{ expanded, onExpand, record }">
      <button @click="(e) => onExpand(record, e)" v-if="record && record.more.length"
        :class="`ant-table-row-expand-icon ${expanded ? 'ant-table-row-expand-icon-expanded' : 'ant-table-row-expand-icon-collapsed'}`"></button>
    </template>

    <template #expandedRowRender="{ record }">
      <a-table :columns="innerColumns" :data-source="record.more" :pagination="false">
        <template #bodyCell="{ column }">

          <template v-if="column.key === 'state'">
            <span>
              <a-badge status="success" />
              Finished
            </span>
          </template>

        </template>
      </a-table>
    </template>
  </a-table>
</template>
<script setup>

const columns = [
  { title: 'Name', dataIndex: 'name', key: 'name' },
  { title: 'Version', dataIndex: 'version', key: 'version' },
  { title: 'Upgraded', dataIndex: 'upgradeNum', key: 'upgradeNum' },
  { title: 'Date', dataIndex: 'createdAt', key: 'createdAt' },
  { title: 'Action', key: 'operation' },
];



const dataSource = [{
  key: 0,
  name: "Screem 1",
  version: "10.3.4.5654",
  upgradeNum: 500,
  createdAt: "2014-12-24 23:12:00",
  more: [
    {
      key: 6,
      date: "2014-12-24 23:12:00",
      name: "This is production name 1",
      upgradeNum: "Upgraded: 56"
    }, {
      key: 7,
      date: "2014-12-24 23:12:00",
      name: "This is production name 2",
      upgradeNum: "Upgraded: 56"
    }, {
      key: 8,
      date: "2014-12-24 23:12:00",
      name: "This is production name 3",
      upgradeNum: "Upgraded: 56"
    }
  ]
}, {
  key: 1,
  name: "Screem 2",
  version: "10.3.4.5654",
  upgradeNum: 700,
  createdAt: "2014-12-24 23:12:00",
  more: []
}, {
  key: 2,
  name: "Screem 3",
  version: "10.3.4.5654",
  upgradeNum: 300,
  createdAt: "2014-12-24 23:12:00",
  more: [
    {
      key: 9,
      date: "2014-12-24 23:12:00",
      name: "This is production name 1",
      upgradeNum: "Upgraded: 56"
    }
  ]
}];


const innerColumns = [
  { title: 'Date', dataIndex: 'date', key: 'date' },
  { title: 'Name', dataIndex: 'name', key: 'name' },
  { title: 'Upgrade Status', dataIndex: 'upgradeNum', key: 'upgradeNum' },
];


</script>

关键在于#expandIcon设置自定义图标,效果跟默认的效果一毛一样。

要注意:

我使用的是antd4,所以图标的类名是

ant-table-row-expand-icon-expanded、ant-table-row-expand-icon-collapsed。

大家如果用的版本不一样,可以控制台看下它默认渲染的图标类名是什么,直接复制过来替换掉就可以了,完美!

    <template #expandIcon="{ expanded, onExpand, record }">
      <button @click="(e) => onExpand(record, e)" v-if="record && record.more.length"
        :class="`ant-table-row-expand-icon ${expanded ? 'ant-table-row-expand-icon-expanded' : 'ant-table-row-expand-icon-collapsed'}`"></button>
    </template>

  • 13
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要使用 Ant Design Vue表格树组件,您需要先安装和导入 Table 和 TreeSelect 组件。 在表格中,您需要使用 `customExpandIcon` 属性来自定义展开/折叠图标,并使用 `customRow` 属性来自定义行的渲染。 以下是一个示例代码: ```vue <template> <a-table :columns="columns" :data-source="data" :custom-row="customRow" :custom-expand-icon="customExpandIcon"> <template slot="name" slot-scope="{ record }"> {{ record.name }} </template> </a-table> </template> <script> import { Table, TreeSelect } from 'ant-design-vue'; export default { components: { Table, TreeSelect, }, data() { return { columns: [ { title: 'Name', dataIndex: 'name', key: 'name', scopedSlots: { customRender: 'name' }, }, { title: 'Age', dataIndex: 'age', key: 'age', }, { title: 'Address', dataIndex: 'address', key: 'address', }, ], data: [ { key: '1', name: 'Parent 1', age: 30, address: 'New York No. 1 Lake Park', children: [ { key: '1-1', name: 'Child 1-1', age: 12, address: 'New York No. 2 Lake Park', }, { key: '1-2', name: 'Child 1-2', age: 13, address: 'New York No. 3 Lake Park', }, ], }, { key: '2', name: 'Parent 2', age: 32, address: 'London No. 1 Lake Park', children: [ { key: '2-1', name: 'Child 2-1', age: 11, address: 'London No. 2 Lake Park', }, { key: '2-2', name: 'Child 2-2', age: 16, address: 'London No. 3 Lake Park', }, ], }, ], }; }, methods: { customExpandIcon({ record, onExpand }) { if (record.children) { return ( <a onClick={onExpand}> {record._expanded ? <a-icon type="minus-square" /> : <a-icon type="plus-square" />} </a> ); } return null; }, customRow(record, index, indent, expanded) { return { class: `custom-row-${record.key}`, on: { click: () => { console.log(record); }, }, }; }, }, }; </script> ``` 在上面的代码中,您可以看到我们自定义了展开/折叠图标和行的渲染,以及使用了插槽 `scopedSlots` 来渲染名称列。您还可以根据您的需求来添加其他自定义的组件或插槽。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

caicaicai404

对作者使用钞能力,欢迎点这里

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值