[英]Alternating Background Color react-bootstrap-table
我正在使用react-bootstrap-table,并且尝试替换背景颜色。 该文档让它有点不清楚,具体是什么类型的数据进入了条件呈现功能的实现,因此我无法收到正确的结果。 我究竟做错了什么?
// Customization Function
function rowClassNameFormat(row, rowIdx) {
// row is whole row object
// rowIdx is index of row
return rowIdx % 2 === 0 ? 'backgroundColor: red' : 'backgroundColor: blue';
}
// Data
var products = [
{
id: '1',
name: 'P1',
price: '42'
},
{
id: '2',
name: 'P2',
price: '42'
},
{
id: '3',
name: 'P3',
price: '42'
},
];
// Component
class TrClassStringTable extends React.Component {
render() {
return (
<BootstrapTable data={ products } trClassName={this.rowClassNameFormat}>
<TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn>
<TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn>
<TableHeaderColumn dataField='price'>Product Price</TableHeaderColumn>
</BootstrapTable>
);
}
}
您可以使用trStyle
而不是trClassName
定制内联样式。 内联样式还应该以对象形式而不是字符串形式返回。
例
function rowStyleFormat(row, rowIdx) {
return { backgroundColor: rowIdx % 2 === 0 ? 'red' : 'blue' };
}
class TrClassStringTable extends React.Component {
render() {
return (
<BootstrapTable data={ products } trStyle={rowStyleFormat}>
<TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn>
<TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn>
<TableHeaderColumn dataField='price'>Product Price</TableHeaderColumn>
</BootstrapTable>
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.