I am using react-bootstrap-table and I am trying to alternate the background color. The documentation leaves it a bit unclear what type of data in particular goes into it's implementation of the conditional rendering function so I cannot receive the correct result. What am I doing wrong?
// 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>
);
}
}
You can customize the inline styles with trStyle
instead of trClassName
. The inlined styles should also be returned in object form, not as a string.
Example
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>
);
}
}