Ant Design Vue: a-table 组件来创建表格,在表头中使用 slot 插槽来自定义表头的列

27 篇文章 0 订阅
5 篇文章 0 订阅
本文展示了如何在AntDesignVue的a-table组件中使用expand-row属性和slot插槽来自定义表格的展开行功能。通过在表头中添加自定义的‘+’按钮,可以实现行的展开和折叠。同时,根据不同展开状态,动态显示编辑和删除按钮或关闭按钮。
摘要由CSDN通过智能技术生成

在 Ant Design Vue 中,可以使用 a-table 组件来创建表格,并且可以使用 expand-row 属性来自定义展开/折叠行。为了在表头中添加一个自定义的“+”按钮,可以在表头中使用 slot 插槽来自定义表头的列。

以下是一个示例代码,演示如何在 Ant Design Vue 中自定义表头为“+”按钮:

<template>  
  <a-table :columns="columns" :data-source="data" :expand-row-keys="expandRowKeys" :default-expand-all="true">  
    <template slot="name" slot-scope="scope">  
      <span v-if="!scope.row.expanded">{{ scope.row.index + 1 }}</span>  
      <span v-else>  
        <i class="anticon anticon-down" @click="toggleExpand(scope.row)"></i>  
        {{ scope.row.index + 1 }}  
      </span>  
    </template>  
    <template slot="operation" slot-scope="scope">  
      <span v-if="!scope.row.expanded">  
        <i class="anticon anticon-edit" @click="edit(scope.row)"></i>  
        <i class="anticon anticon-delete" @click="remove(scope.row)"></i>  
      </span>  
      <span v-else>  
        <i class="anticon anticon-close" @click="toggleExpand(scope.row)"></i>  
      </span>  
    </template>  
  </a-table>  
</template>  
  
<script>  
export default {  
  data() {  
    return {  
      columns: [  
        {  
          title: 'Name',  
          dataIndex: 'name',  
          key: 'name',  
        },  
        {  
          title: 'Operation',  
          dataIndex: 'operation',  
          key: 'operation',  
          scopedSlots: { default: 'operation' },  
        },  
      ],  
      data: [  
        {  
          id: 1,  
          name: 'John Doe',  
          operation: 'Edit',  
        },  
        {  
          id: 2,  
          name: 'Jane Smith',  
          operation: 'Delete',  
        },  
      ],  
      expandRowKeys: [],  
    };  
  },  
  methods: {  
    toggleExpand(row) {  
      const index = this.expandRowKeys.indexOf(row.id);  
      if (index === -1) {  
        this.expandRowKeys.push(row.id);  
      } else {  
        this.expandRowKeys.splice(index, 1);  
      }  
    },  
    edit(row) {  
      alert(`Edit ${row.name}`);  
    },  
    remove(row) {  
      alert(`Delete ${row.name}`);  
    },  
  },  
};  
</script>

在这个示例代码中,我们在表头中定义了两列:姓名和操作。对于“操作”列,我们使用了 scopedSlots 来定义一个名为 operation 的插槽,用于自定义该列的内容。在插槽中,我们根据当前行的展开状态来显示不同的按钮。如果当前行未展开,则显示“Edit”和“Delete”按钮;如果当前行已展开,则显示“Close”按钮。我们还定义了一个 toggleExpand 方法来切换行的展开状态。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

浪潮行舟

你的鼓励是我创作的最大动力。

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

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

打赏作者

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

抵扣说明:

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

余额充值