Ant Design Vue <a-table>表格行根据条件改变背景色

//组件

<a-table> 属性中加入属性:

        :rowClassName="setRowClassName"

//js

//setRowClassName为自定义返回行的样式名称,record,index参数为行数据和行索引

setRowClassName(record,index){

        //判断并返回行的样式名称

        if(record.xxx == '1'){

                return "bg-blue"

        }else if(record.xxx == '2'){

                return "bg-yellow"

        }

}

//css

::v-deep .bg-blue{

        background-color:blue;

}

::v-deep .bg-yellow{

        background-color:yellow;

}

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在Ant-Design-Vue表格设置单元格的背景色,可以使用`customRender`属性来自定义单元格的渲染方式,具体可以参考以下代码: ```html <template> <a-table :columns="columns" :data-source="data"> <template slot="name" slot-scope="text, record"> <span :style="{backgroundColor: record.color}">{{text}}</span> </template> </a-table> </template> <script> export default { data() { return { columns: [ { title: "Name", dataIndex: "name", key: "name", customRender: (text, record, index) => { if (index > 2) { return <span :style="{backgroundColor: record.color}">{text}</span>; } else { return text; } }, }, { title: "Age", dataIndex: "age", key: "age", }, { title: "Address", dataIndex: "address", key: "address", }, ], data: [ { key: 1, name: "John Brown", age: 32, address: "New York No. 1 Lake Park", color: "#f0f0f0", }, { key: 2, name: "Jim Green", age: 42, address: "London No. 1 Lake Park", color: "#f0f0f0", }, { key: 3, name: "Joe Black", age: 32, address: "Sidney No. 1 Lake Park", color: "#f0f0f0", }, { key: 4, name: "Jim Red", age: 32, address: "London No. 2 Lake Park", color: "#f0f0f0", }, { key: 5, name: "Jake White", age: 32, address: "Dublin No. 2 Lake Park", color: "#f0f0f0", }, ], }; }, }; </script> ``` 在上述代码中,我们通过`customRender`方法来自定义单元格的渲染方式,并且在前三列不设置单元格的背景色。具体实现中,我们在`customRender`方法中判断当前单元格的索引是否大于2,如果是,则渲染带有背景色的`span`标签,否则直接返回文本。同时,我们在模板中使用`slot`以及`slot-scope`属性来渲染自定义单元格。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值