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表格的行样式

觉得这篇文章有用的小伙伴们🔥
可以点赞➕收藏➕关注哦~🔥
  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Dorable_Wander

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

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

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

打赏作者

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

抵扣说明:

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

余额充值