vue3 antd table表格样式修改——使用rowClassName更改某行数据的样式

vue3 antd项目实战——修改ant design vue组件中table表格的默认样式(二)

知识调用

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

场景复现

上期文章我们介绍了如何修改 ant design vue 表格的默认样式,并以 修改table表格的padding(调整表格行row的高度) 为例,以vue组件的css样式穿透为方法,详细介绍了解决方案。

本期文章将继续介绍table表格的默认样式修改。

  • 主要内容利用rowClassName给table添加行样式
  • 实际例子修改table表格的颜色
  • 解决方法rowClassName+ pointer-events: none;

修改table表格的行样式

修改默认行样式之前: 👇👇👇(表格源码附在文章最后
在这里插入图片描述

表格默认样式:

修改后的样式需求:

  • 取消斑马纹
  • 更改表格行样式(颜色)

解决方案:

  • 修改行样式(颜色)——样式穿透+rowClassName添加行样式
  • 表格行不可控(鼠标放在表格行上时无变色效果)——css不可控操作

一、rowClassName添加行样式

rowClassNameant design vue组件库内置的一个API属性,通过 绑定对应的行样式 即可对table表格的默认行样式进行修改。

点击可移步官方文档具体查看

在这里插入图片描述
template部分
绑定rowClassName属性,调用newStyle函数
在这里插入图片描述

源代码:

<template>
    <a-table 
        :columns="columns" 
        :data-source="data"
        :rowClassName="newStyle"
        >
        <template #bodyCell="{ column, text }">
            <template v-if="column.dataIndex === 'name'">
                <a>{{ text }}</a>
            </template>
        </template>
    </a-table>
</template>

script部分:
编写newStyle函数

// 新样式函数
const newStyle = () => {
    return 'newRowStyle' // 返回到行样式
}

在这里插入图片描述
注意: 各变量名、函数名需要保持一致

style部分:

<style scoped>
.newRowStyle {
    background-color: orange;
}
</style>

此时我们来看看效果。👇👇👇
在这里插入图片描述
我们发现,表格的样式并未发生改变。

对应的有两种解决方案

  • 删除scoped(较方便)
  • 样式穿透

我们尝试删除scoped之后,显示效果如下 👇👇👇
在这里插入图片描述
到目前为止,解决方案第一步实现。可以看到表格的斑马纹依然存在
这时就需要通过不可控操作来去除斑马纹。

斑马纹效果如下图:👇👇👇
在这里插入图片描述

二、表格的不可控操作

style部分:
在新的行样式中添加禁用操作(这也意味着表格自带的触控样式会全部消失)
在这里插入图片描述

.newRowStyle {
    background-color: orange; /* 修改颜色*/
    pointer-events: none;  /* 取消斑马纹*/
}

此时我们来看看实现效果如何 👇👇👇
在这里插入图片描述
很显然,到目前为止我们完成了解决方案的所有步骤。更改了表格行颜色,同时也取消了斑马纹,需求得以实现。

基础表格源码【未经处理】:(包含表格数据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>

写在最后

ant design vue组件库中table表格学问很多,有很多功能值得我们去研究探索。下期文章将以本文为基础进行延伸,介绍 如何根据数据属性更改行样式


后续将不定期持续更新相关内容~🔥
觉得这篇文章有用的小伙伴们🔥
可以点赞➕收藏➕关注哦~🔥
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
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 中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

XSL_HR

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

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

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

打赏作者

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

抵扣说明:

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

余额充值