当前位置:   article > 正文

antd table如何默认选中第一行,并添加背景色_antd table第一行设置背景样式

antd table第一行设置背景样式
table配置
<Table
   columns={this.state.columns}
     dataSource={this.state.tableData}
     bordered
     // loading
     rowClassName='table-box-detail'
     pagination={false}
     // scroll={{y: 500 }}
     rowClassName={this.setTableRowClassName}
     // 对点击的列表行添加背景色
     onRow = {this.onClickTableRow}
     scroll={{x: 500, y: this.state.scrollHeight }}
     rowKey={this.state.tableData.ipcId}
              />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

在这里插入图片描述

添加类名的方法
 setTableRowClassName = (record) => {
    return record.iiId === this.state.rowId ? 'clickRowSty1' : '';
  }
  • 1
  • 2
  • 3
添加点击每一行事件的方法
 onClickTableRow = (record) => {
    console.log(record);
    return {
      onClick: () => {
        this.setState({
          rowId: record.iiId,
          selectRow: record
        })
      }
    }
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

在这里插入图片描述

clickRowSty1 类名 设置样式放在公共样式或者任意一个地方,给个背景色

// 给表格添加背景色
.clickRowSty1 {
  background-color: #e6f7ff;
}
  • 1
  • 2
  • 3
  • 4

state设置rowId

  this.state = {
	rowId: ''
}
  • 1
  • 2
  • 3
getList方法 从后台获取列表内容或者自己模拟假数据都行,默认取第一条数据
 getList = (data) => {
    let params = { }
 
    window.$API.GetIllegalList(params)
        .then( res => {
          if(res.data.code === 0) {
            let pageNav = this.state.pageNav;
            pageNav.total = res.data.data.total;
            this.setState({
              tableData: res.data.data.items,
              pageNav
            })
            // 当获取数据长度不为0,再一条
            if(res.data.data.items.length !== 0) {
                this.setState({
                  selectRow: res.data.data.items[0],
                  rowId: res.data.data.items[0].iiId
                },
                () => {
                  this.onClickTableRow(res.data.data.items[0]);
                  this.setTableRowClassName(res.data.data.items[0]);
                });
            }
          } else {
            message.error(res.data.msg);
          }
        })
        .catch( err => {
          console.log(err);
        })
  };
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

在这里插入图片描述

实现效果 默认选中第一条

在这里插入图片描述

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/459282
推荐阅读
相关标签
  

闽ICP备14008679号