material-table-core / core

Datatable for React based on material-ui's table with additional features. Support us at https://opencollective.com/material-table-core
https://material-table-core.github.io
MIT License
296 stars 146 forks source link

How update table styling after sorting? #781

Closed gabrielizalo closed 1 year ago

gabrielizalo commented 1 year ago

I created a Zebra Table with sort columns:

CleanShot 2023-07-26 at 11 21 12@2x

The did setup the zebra style in this way:


<MaterialTable
  data={data}
  columns={columns}
  ...
  options={{
    ...
    rowStyle: (rowData) => ({
      backgroundColor:
        rowData.tableData.id % 2 === 0
          ? "var(--white)"
          : "var(--wild-sand)",
    }),
  }}
/>

After sorting, the background colors remains in the original rows, so the table is not a zebra table anymore:

CleanShot 2023-07-26 at 11 24 17@2x

How I can update the "zebra styling" of the table after sorting?

Thanks

Domino987 commented 1 year ago

Can you add a sandbox as requested? Thank you.``

gabrielizalo commented 1 year ago

@Domino987 Thanks.. I created this SandBox: https://codesandbox.io/s/material-table-zebra-with-order-pk4zvt?file=/src/index.js

If you click in any of the header columns rows will be sorted and zebra style is damaged.

Domino987 commented 1 year ago

You need to use the index, not the id

rowStyle: (rowData, index) => {console.log(index, rowData);return { background: index % 2 === 0 ? "White" : "Gray" }},

Domino987 commented 1 year ago

You need to use the index, not the id

rowStyle: (rowData, index) => {console.log(index, rowData);return { background: index % 2 === 0 ? "White" : "Gray" }},

gabrielizalo commented 1 year ago

It worked perfectly. Thanks @Domino987 !