question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set class for css styles on a cell or row based on data

See original GitHub issue

I would like to be able to do something like this.

const data = [ 
    { x: 1, y: 2},
    { x: 2, y: 3}
];

const getClass = (cell, row) {
    if (row.x == 1) { 
       return "bg-info";
    } else {
       return "bg-warning";
    }
}

const MyTable = () => (
    <BootstrapTable data={data} keyField="x">
        <TableHeaderColumn dataField="y" **dataClass={getClass}** />
    </BootstrapTable>
);

When rendered, MyTable would have the bg-info class on the first row and bg-warning on the second row. This would result in the background of the cells being colored appropriately.

I tried setting dataFormat={cellFormat} on the columns, where cellFormat returns <span bsClass="{style}"> {cell} </span> with style being the style name, but that did not work. Setting class instead of bsClass also did not work.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
patrickgordoncommented, Sep 7, 2016

hey @AllenFang here’s what I did:

  trClassFormat(rowData, rIndex) {
    const {noShiftDataRowIds} = this.props
    if (find(noShiftDataRowIds, (item) => {
        return item === rIndex
      })) {
      return 'danger'
    }
  }

I think the issue is actually that noShiftDataRowIds is an array of the index’s for my data and that is not changing because it has no reason to. What I will do is instead of it being an array of index I will make it an array of the primary key instead and that should work. 😃

👍 thanks for help

0reactions
patrickgordoncommented, Sep 9, 2016

Yep all sorted @AllenFang !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use CSS to change the style of each row depending on ...
Use CSS to change the style of each row depending on the content · 1. Display element B if element A is empty...
Read more >
Apply css style to a row based on a value of its data
I'm able to style the single cells with an external script that read data from the data-style attribute and append to the <td>...
Read more >
Is it possible to select a set of table cells for CSS styling by ...
The only solution was to add a class to every cell in the relevant columns, and select on tr.rowclass td.colclass .
Read more >
Adding a css class to row based on condition; GridModel ...
Class will be added based on data from a column in the row. I few things about my grid; I'm using GridModel approach...
Read more >
Cell Styles: Styling & Appearance Feature of our Datagrid
Use CSS rules to define Cell Style based on data content, e.g. put a red ... cellClass: 'my-class' } // return same array...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found