vue3 antd table表格样式修改——ant design vue table表格的行高调整

vue3 antd项目实战——修改ant design vue table组件的默认样式(调整每行行高)

知识调用

文章中可能会用到的知识链接
vue3+ant design vue+ts实战【ant-design-vue组件库引入】
css样式穿透(/deep/ ::v-deep深度选择器)🔥🔥
vue3 antd项目实战——table表格(一文带你快速实现后台管理系统最常用的table表格)🔥🔥

场景复现

修改antd table的默认样式
ant design vue组件库固然简单好用,但是它的默认样式有时候非常鸡肋,有些是不太符合我们的需求的。比如,表格的padding过宽,又比如,更改行颜色。 这时我们需要用到 vue组件的样式穿透 来更改table表格的默认样式。

实际操作

解决a-table表格padding过宽

改变默认样式之前: 👇👇👇(表格源码附在文章末尾
在这里插入图片描述

  • 假如我们需要打印这个表格,在纸张有限的情况下,我们肯定是既希望保证美观,又希望在美观的同时最大限度的利用纸张。
  • 类比页面亦是如此,我们可以将table样式中的padding调小一点,于是这里就用到了上期文章所介绍的 css样式穿透。这里我们称为 vue的样式穿透

这里我采用的是最通用的解决方式3。 👇👇👇

解决方式3——通用的样式穿透(使用::v-deep)

<style scoped>
  ::v-deep .ant-table-thead > tr > th, .ant-table-tbody > tr > td {
      padding: 8px 8px;
      overflow-wrap: break-word;
  }
  ::v-deep .ant-table-tbody .ant-table-row td {
      padding-top: 8px;
      padding-bottom: 8px;
    }
</style>

解决方式1——sass和less的样式穿透(使用/deep/)

<style lang="scss" scoped>
  .AStockOutDetailTable{
    & /deep/ .ant-table-thead > tr > th, .ant-table-tbody > tr > td {
      padding: 8px 8px;
      overflow-wrap: break-word;
    }
    & /deep/ .ant-table-tbody .ant-table-row td {
      padding-top: 8px;
      padding-bottom: 8px;
    }
  }
</style>

解决方式2——stylus的样式穿透(使用>>>)

<style lang="stylus" scoped>
  .AStockOutDetailTable >>> .ant-table-thead > tr > th, .ant-table-tbody > tr > td {
      padding: 8px 8px;
      overflow-wrap: break-word;
  }
  .AStockOutDetailTable >>> .ant-table-tbody .ant-table-row td {
      padding-top: 8px;
      padding-bottom: 8px;
    }
</style>
  • 用另外两种方式都可以解决表格默认样式的问题。

用上述方法更改默认样式之后的样式: 👇👇👇
在这里插入图片描述


基础表格源码:【未经处理】(包含表格数据data、表格纵列目录columns)

<template>
    <a-table 
        :columns="columns" 
        :data-source="data"
        class="AStockOutDetailTable"
        >
        <template #bodyCell="{ column, text }">
            <template v-if="column.dataIndex === 'name'">
                <a>{{ text }}</a>
            </template>
        </template>
    </a-table>
</template>
<script lang="ts" setup>
import { defineComponent } from 'vue';
const columns = [
    {
        title: 'Name',
        dataIndex: 'name',
        key: 'name',
    },
    {
        title: 'Age',
        dataIndex: 'age',
        key: 'age',
        width: 80,
    },
    {
        title: 'Address',
        dataIndex: 'address',
        key: 'address 1',
        ellipsis: true,
    },
    {
        title: 'Long Column Long Column Long Column',
        dataIndex: 'address',
        key: 'address 2',
        ellipsis: true,
    },
    {
        title: 'Long Column Long Column',
        dataIndex: 'address',
        key: 'address 3',
        ellipsis: true,
    },
    {
        title: 'Long Column',
        dataIndex: 'address',
        key: 'address 4',
        ellipsis: true,
    },
];

const data = [
    {
        key: '1',
        name: 'John Brown',
        age: 32,
        address: 'New York No. 1 Lake Park, New York No. 1 Lake Park',
        tags: ['nice', 'developer'],
    },
    {
        key: '2',
        name: 'Jim Green',
        age: 42,
        address: 'London No. 2 Lake Park, London No. 2 Lake Park',
        tags: ['loser'],
    },
    {
        key: '3',
        name: 'Joe Black',
        age: 32,
        address: 'Sidney No. 1 Lake Park, Sidney No. 1 Lake Park',
        tags: ['cool', 'teacher'],
    },
];
</script>

下期文章将介绍如何修改antd table表格的行样式

觉得这篇文章有用的小伙伴们🔥
可以点赞➕收藏➕关注哦~🔥
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
vue3 antdvue 中,要对 a-table 中的行进行合计,可以利用 a-table 中的 footer 属性和 scoped slot 的方式来实现。 为了得到每一行中需要合计的值,需要先将数据进行处理。 首先,需要在 a-table设置 footer 属性为 true,这样就会生成表格的 footer 部分。 然后,可以利用 scoped slot 来自定义 footer 行的内容。在 slot-scope 中,可以获取到表格中的所有数据,包括每一行中的数据。可以遍历每一行的数据,计算需要合计的值,并将结果显示在 footer 中。 下面是实现的代码示例: ``` <template> <a-table :columns="columns" :data-source="data" :footer="true"> <template v-slot:footer> <a-table-tfoot> <a-table-cell v-for="col in columns" :key="col.key"> <template v-if="col.key === 'total'"> {{ getFooterTotal(col.key) }} </template> <template v-else> {{ col.title }} </template> </a-table-cell> </a-table-tfoot> </template> </a-table> </template> <script> export default { data() { return { columns: [ { title: 'Name', dataIndex: 'name', key: 'name' }, { title: 'Age', dataIndex: 'age', key: 'age' }, { title: 'Address', dataIndex: 'address', key: 'address' }, { title: 'Total', key: 'total' }, ], data: [ { name: 'John', age: 28, address: 'New York', total: 100 }, { name: 'Jane', age: 32, address: 'London', total: 200 }, { name: 'Bob', age: 42, address: 'Paris', total: 300 }, ], }; }, methods: { getFooterTotal(key) { let total = 0; this.data.forEach((item) => { total += item[key]; }); return total; }, }, }; </script> ``` 在这个示例中,我们定义了一个 a-table ,包含了四列数据:Name、Age、Address 和 Total。其中,Total 列需要对其下面的每一行进行合计。 我们通过设置 footer 属性为 true,确保能够生成表格的 footer 部分。接着,我们可以利用 scoped slot 来自定义 footer 的内容。 在 footer 模板中,我们首先定义了一个 a-table-tfoot 元素,表示 footer 中要显示的行。 然后,我们遍历每一列数据,如果是 Total 列,就调用 getFooterTotal 方法来计算合计值。这个方法遍历每一行数据,将 Total 列的值累加到 total 中。 最后,我们将结果返回给 footer 模板,以显示在表格的 footer 中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Dorable_Wander

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

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值