ant vue中给table列自定义样式

效果图

在这里插入图片描述

代码
columns: [
	{ title: '序号', dataIndex: 'index', align: 'center', width: '5%', scopedSlots: { customRender: 'index' } },
	{
		title: '版本名称',
		dataIndex: 'versionName',
		align: 'center',
		ellipsis: true,
		customCell: (record) => {
			//条件成立才显示自定义样式
			if(record.isDefault){
				return {
					style: {
						backgroundImage: "url(" + require("@/assets/icons/default.svg") + ")",
						backgroundRepeat: "no-repeat",
						backgroundSize: "30%",
						backgroundPosition: '105% -7px',
					}
				}
			}
		},
	},
	{ title: '版本号', dataIndex: 'versionNo', align: 'center' , ellipsis: true },
],
vue3写法
columns.splice(1, 0, {
    title: typeName.value + '单编号', dataIndex: 'formNo', align: 'center', width: '160px',
    customCell: (record) => {
      if(record.formLevel === '2'){
        return {
          style: {
            backgroundImage: "url("+ _this.$myUtil.getAssetsFile('icons', 'urgent.svg') + ")",
            backgroundRepeat: "no-repeat",
            backgroundSize: "25%",
            backgroundPosition: '100%',
          }
        }
      }
    },
  });

 /**
  * 获取assets目录下的文件
  * @param url
  * @param fileName
  * @returns {string}
  */
 getAssetsFile(url, fileName) {
     return new URL(`../assets/${url}/${fileName}`, import.meta.url).href;
 },

差别不大,只是 require 在 vite + vue3 中不支持,改成使用 new URL() 获取地址
更新于2024-01-25

### 使用 `customRow` 实现 Ant Design Vue 表格组件的自定义Ant Design VueTable 组件中,`customRow` 属性提供了一种方法来定制每行的行为和样式。这使得开发人员能够根据特定的需求调整表格的功能。 #### 定制行点击事件 为了响应用户的交互行为,比如当用户单击某一行时触发某些操作,可以通过设置 `customRow` 来监听这些动作: ```javascript const columns = [ { title: 'Name', dataIndex: 'name' }, // ...其他配置... ]; const data = [ { key: '1', name: 'John Brown' }, // ...更多数据条目... ]; // 自定义行属性 const rowSelection = { customRow: record => ({ onClick: event => console.log(`Clicked on ${record.name}`, event), }) }; ``` 上述代码展示了如何利用 `customRow` 函数返回的对象中的 `onClick` 方法处理行级别的点击事件[^2]。 #### 动态应用样式到指定行 除了添加交互逻辑外,还可以依据业务规则动态改变行的颜色或其他CSS属性。例如,对于满足一定条件的数据记录给予视觉上的突出显示: ```vue <template> <a-table :columns="columns" :data-source="data"> <!-- 这里省略了其他的 prop --> </a-table> </template> <script setup lang="ts"> import { ref } from 'vue'; const columns = [...]; // 定义同上 let data = ref([ { key: '1', name: 'Jack', age: 32, address: 'New York No. 1 Lake Park'}, { key: '2', name: 'Lucy', age: 24, address: 'London No. 1 Lake Park'} ]); function highlight(row){ return{ style:{ backgroundColor:(row.age>30)?'#f8d7da':'white' } }; } const rowProps={ customRow:highlight, } </script> ``` 在这个例子中,如果一个人的年龄超过30岁,则该行背景会变成浅红色;否则保持默认白色背景颜色。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值