1.antd官网表格组件:
https://ant.design/components/table-cn/#components-table-demo-dynamic-settings
2.引用antd的表格组件,即:
import {Table} from ‘antd’;
渲染表格组件:
说明:
(1)rowClassName属性是添加表格行样式属性,此时的this.setClassName方法为点击行之后为其添加行高亮样式。
(2)onRow是给表格行添加点击onClick事件,也可在其内部添加其他事件,如鼠标移入、移出等。
(3)下面为实现代码:(本人对代码均做了注释说明,大家可参考)
//antd表格组件
<Table
pagination={{ // 分页
current: this.state.Current,
total: this.state.total,
pageSize:this.props.pageSize,
onChange: this.antdPage,
}}
dataSource={this.state.dataSource} //表格数据渲染
scroll={{ x: this.props.scrollX }} //表格内弄的横向滚动
className={`${style['l-table-td']}`} //给表格列添加不换行样式
rowClassName={this.setClassName} //表格行点击高亮
onRow={(record) => {//表格行点击事件
return {
onClick: this.clickRow.bind(this,record.no)
};
}}
/>
clickRow(num){
this.setState({
activeIndex:(num-1)//获取点击行的索引
})
}
setClassName=(record,index)=>{//record代表表格行的内容,index代表行索引
//判断索引相等时添加行的高亮样式
return index === this.state.activeIndex ? `${style['l-table-row-active']}` : "";
}
参考:
https://blog.youkuaiyun.com/weixin_41652466/article/details/80697463
https://ant.design/components/table-cn/#components-table-demo-dynamic-settings