antd table表格选中每行设置背景色

4 篇文章 0 订阅

1.文档显示,table表格中有rowClassName属性
Table
参数 --------------------------------- 说明--------------------------------------------------------- 类型
在这里插入图片描述
2.vue页面使用

//template    rowClassName
<a-table ref="table" :columns="columns" :dataSource="dataSource" :loading="loading" 
	:rowClassName='getRowBackGround' :pagination="ipagination" 
	:scroll="{x:true}" bordered size="middle" @change="handleTableChange" >
</a-table>

//script
<script>
	export default {
		data(){
			return{
				//表头
				columns:[
					{
						title: "告警类型",
						align: "center",
						dataIndex: "typename",
						scopedSlots: { customRender: "alarmType" }
					}
				]
			}
		},
		methods:{
			//根据条件table中customCell改变单元格样式
		getRowBackGround(recordAlarm) {
			// console.log("customCell改变单元格样式" + JSON.stringify(recordAlarm))
			console.log("levelid------" + recordAlarm.levelid)
			switch (recordAlarm.levelid) {//这里根据条件改变表格背景色
				case "0":
					return "table-color-default"
				case "1":
					return  "table-color-success"
				case "2":
					return "table-color-processing"
				case "3":
					return "table-color-warning"
				case "4":
					return  "table-color-error"
				default:
					return "table-color-default"
			}
		},
		}
	}
</script>
<style>
.table-color-success {
	color: #ffffff;
	background-color: #0c8fcf;
}

.table-color-processing {
	background-color: #FFFF00;
}

.table-color-warning {
	color: #ffffff;
	background-color: #FFA500;
}

.table-color-error {
	color: #ffffff;
	background-color: #FF0000;
}

.table-color-default {
	color: #ffffff;
	background-color: #808080;
}
</style>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你使用的是 antdTable 组件,可以通过以下步骤来实现在分页切换时保留之前的选中项: 1. 在 Table 组件中,设置 rowSelection 属性,该属性用于控制行选择功能。 2. 在 rowSelection 中,设置 selectedRowKeys 属性,该属性用于存储当前选中的行的 key 值。 3. 在 onChange 回调函数中,通过参数 pagination 获取当前的分页信息,并将 selectedRowKeys 存储到 state 中。 4. 在 render 中,通过 state 中的 selectedRowKeys 属性,将之前选中的行重新选中。 下面是一个示例代码: ``` class MyTable extends React.Component { state = { selectedRowKeys: [], // 存储当前选中的行的 key 值 }; onSelectChange = selectedRowKeys => { // 更新选中的行 this.setState({ selectedRowKeys }); }; render() { const { selectedRowKeys } = this.state; const rowSelection = { selectedRowKeys, onChange: this.onSelectChange, }; const dataSource = [ { key: '1', name: 'Mike' }, { key: '2', name: 'John' }, { key: '3', name: 'Lucy' }, { key: '4', name: 'Tom' }, ]; const columns = [ { title: 'Name', dataIndex: 'name', key: 'name' }, ]; return ( <Table rowSelection={rowSelection} dataSource={dataSource} columns={columns} pagination={{ pageSize: 2, onChange: (page, pageSize) => { // 获取当前分页的选中行 const { selectedRowKeys } = this.state; // 更新分页信息 this.setState({ selectedRowKeys: [], }); }, }} /> ); } } ``` 在上面的代码中,我们定义了一个 Table 组件,该组件包含一个 rowSelection 属性,用于控制行选择功能。在 onChange 回调函数中,我们获取当前的分页信息,并将之前选中的行存储到 state 中。在 render 中,我们通过 state 中的 selectedRowKeys 属性,将之前选中的行重新选中

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值