Material-table: Sorting doesn't work when fields shown are null

Created on 21 Mar 2019  路  4Comments  路  Source: mbrn/material-table

Describe the bug
I have a column that has lots of null values. When only null values are shown on page, and you try to sort, nothing happens. If you go to a page where a value is shown, and you click to sort, it goes back to page 1 and it still doesn't sort. No error appears on console.

Column definition:

{
  title: '脷lt. Venda',
  field: 'dataVenda',
  render: (rowData) => rowData.dataVenda ? moment(rowData.dataVenda).format('DD/MM/YYYY') : 'Sem dados',
}

Expected behavior
Sorting should work fine

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari] Firefox, Chrome
  • Version [e.g. 22] 67.0b3, 74.0.3729.22
bug

All 4 comments

Hi @TatiTheFreaK ,

I will take a look at this case asap.

I also face this issue, I think its possible to get around it until the fix lands though. In the docs there is customSort under columns props that could be implemented by you to handle null values for each of your columns.

Good thinking. Something like this function works as a quick fix.

const sortNullFirst = (a, b, property) => a[property] < b[property] || a[property] === null ? -1 : 1

Implementation:

columns={
  [
    ...,
    {
      title: 'Some Column',
      field: 'correspondingProperty',
      customSort: (a, b) => sortNullFirst(a, b, 'correspondingProperty')
    },
    ...,
]
}

Perhaps the more sustainable solution is to convert nulls to empty strings, say, when the data is stored or when it's fetched?

This is done with version 1.31.0

Was this page helpful?
0 / 5 - 0 ratings