Ant中修改Table的标题title实现不同自定义背景颜色

Table标题颜色调整:解决AntDesignTable中CSS冲突与内边距问题
本文讲述了如何在AntDesignTable的标题(title)中实现不同颜色,并解决因内边距导致的样式问题。作者通过修改columns中的title样式并利用全局CSS覆盖解决了背景色显示和padding影响的问题,最终实现了预期的标题颜色效果。

需求背景 需要在Table表格的标题title中实现不同颜色的标题 

实现思路:通过在 columns中 修改title的颜色

let columnsYX = [{
    title: '类型',
    dataIndex: 'category',
    width: 80,
  }, {
    title: <div style={{backgroundColor:'red'}}>'唯一识别码'</div>,
    dataIndex: 'visitNo',
    width: 120,
  }, {
    title: '病案号1',
    dataIndex: 'chartNo',
    width: 120,
  },]

效果:

这时候出现问题  在title的设置后  外层还是原组件颜色

这时候我们在控制台,盒子模型中发现有一个padding:16的内边距

这时候解决办法:通过 :global 使用css将元素定位并改变样式

.zrjCSS3 {
  :global {
    .ant-table-bordered .ant-table-thead>tr>th {
      background-color: #4874CB;
      color: #fff;
      padding: 0px;
      text-align: center;
      line-height: 50px;
    }
    .ant-table-thead > tr > th .ant-table-header-column{
      display: block;
    }

  }
}
<div className={zjrCSS3}>
  <Table columns={columns} dataSource={dataSource} bordered
    loading={loadingVis}
  />
</div>

最终效果:

在Vue 3中使用Ant Design Vue的a-table组件时,自定义表头和表格内容颜色可采用以下方法: ### 自定义表头颜色 可借助CSS选择器修改表头背景色、文字颜色等样式,也能使用插槽自定义表头内容并设置样式。 #### 方法一:使用CSS选择器 通过CSS选择器修改表头样式,示例代码如下: ```vue <template> <a-table :columns="columns" :data-source="dataSource"></a-table> </template> <style scoped> /* 标题背景色用important */ ::v-deep .ant-table-thead > tr > th { background: #yourColor !important; color: #yourTextColor; font-size: yourFontSize; padding-left: yourPaddingLeft; } </style> <script setup> import { ref } from 'vue'; const columns = ref([ { title: 'Name', dataIndex: 'name', key: 'name', }, // 其他列配置 ]); const dataSource = ref([ { key: '1', name: 'John Brown', }, // 其他数据 ]); </script> ``` #### 方法二:使用插槽自定义表头内容和样式 利用插槽自定义表头内容并设置样式,示例代码如下: ```vue <template> <a-table :columns="columns" :data-source="dataSource"> <!-- 表头自定义 --> <span slot="customName" style="color: #ff6b81"><a-icon type="smile-o" /> 订单状态配载状态</span> </a-table> </template> <script setup> import { ref } from 'vue'; const columns = ref([ { dataIndex: 'no', key: 'no', title: '客户名称', scopedSlots: { customRender: 'time' }, }, { dataIndex: 'time', key: '配载单状态', // 表头自定义 slots: { title: 'customName' }, scopedSlots: { customRender: 'time' }, }, // 其他列配置 ]); const dataSource = ref([ { key: '1', no: '123', time: '2024-01-01', }, // 其他数据 ]); </script> ``` ### 自定义表格内容颜色 可通过CSS选择器设置隔行变色,也能根据数据动态判断设置颜色。 #### 方法一:使用CSS选择器设置隔行变色 通过CSS选择器设置表格行的背景色实现隔行变色效果,示例代码如下: ```vue <template> <a-table :columns="columns" :data-source="dataSource"></a-table> </template> <style scoped> /* 隔行变色 */ ::v-deep .ant-table-tbody .ant-table-row:nth-child(odd) { background-color: #yourOddColor; } ::v-deep .ant-table-tbody .ant-table-row:nth-child(even) { background: #yourEvenColor; } </style> <script setup> import { ref } from 'vue'; const columns = ref([ { title: 'Name', dataIndex: 'name', key: 'name', }, // 其他列配置 ]); const dataSource = ref([ { key: '1', name: 'John Brown', }, // 其他数据 ]); </script> ``` #### 方法二:根据数据动态判断设置颜色 根据数据动态判断单元格颜色,可通过方法返回样式对象,示例代码如下: ```vue <template> <a-table :columns="columns" :data-source="dataSource"> <template #customRender="{ record }"> <span :style="getStyle(record)"> {{ record.name }} </span> </template> </a-table> </template> <script setup> import { ref } from 'vue'; const columns = ref([ { title: 'Name', dataIndex: 'name', key: 'name', scopedSlots: { customRender: 'customRender' }, }, // 其他列配置 ]); const dataSource = ref([ { key: '1', name: 'John Brown', value: 8, }, // 其他数据 ]); const getStyle = (record) => { return { backgroundColor: record.value >= 5 ? 'red' : '', color: record.value >= 5 ? '#fff' : '', }; }; </script> ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值