React-table: Filter is case-sensitive, how to change to not case sensitive??

Created on 2 May 2017  路  4Comments  路  Source: tannerlinsley/react-table

Hi,

The filters in the table is case sensitive. I've gone through the documentation but couldn't find anything on how to change it to not being case sensitive. Any suggestions??

Thanks in advance.

Most helpful comment

@yolohello12 yolohello12
https://github.com/tannerlinsley/react-table/blob/bc7005db9993df2a38c3c04024c9c49638dc5125/src/defaultProps.js#L25:

  defaultFilterMethod: (filter, row, column) => {
    const id = filter.pivotId || filter.id
    return row[id] !== undefined ? String(row[id]).startsWith(filter.value) : true
  },

```es6
filterMethod = (filter, row, column) => {
const id = filter.pivotId || filter.id
return row[id] !== undefined ? String(row[id].toLowerCase()).startsWith(filter.value.toLowerCase()) : true
},

```jsx
    <Table
      defaultFilterMethod={this.filterMethod}

All 4 comments

Have you taken a look at the Filtering section of the Readme?

If you want to set the filters on the whole table you can use the defaultFilterMethod or you can set each column individually with the filterMethod. The Custom Filtering example is a good reference as well.

@yolohello12 yolohello12
https://github.com/tannerlinsley/react-table/blob/bc7005db9993df2a38c3c04024c9c49638dc5125/src/defaultProps.js#L25:

  defaultFilterMethod: (filter, row, column) => {
    const id = filter.pivotId || filter.id
    return row[id] !== undefined ? String(row[id]).startsWith(filter.value) : true
  },

```es6
filterMethod = (filter, row, column) => {
const id = filter.pivotId || filter.id
return row[id] !== undefined ? String(row[id].toLowerCase()).startsWith(filter.value.toLowerCase()) : true
},

```jsx
    <Table
      defaultFilterMethod={this.filterMethod}

@a-x- That did the trick, thanks a lot.

filterMethod = (filter, row, column) => {
    const id = filter.pivotId || filter.id
    return row[id] !== undefined ? String(row[id].toLowerCase()).startsWith(filter.value.toLowerCase()) : true
  }

I suggest applying .toLowerCase() after String(row[id])
as in String(row[id]).toLowerCase().startsWith, to make sure data in row[id] is actually a string, and that toLowerCase() exists as well.

Becomes:

filterMethod = (filter, row, column) => {
    const id = filter.pivotId || filter.id
    return row[id] !== undefined ? String(row[id].toLowerCase()).startsWith(filter.value.toLowerCase()) : true
  }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ocalde picture ocalde  路  3Comments

mlajszczak picture mlajszczak  路  3Comments

kieronsutton00 picture kieronsutton00  路  3Comments

mellis481 picture mellis481  路  3Comments

dwjft picture dwjft  路  3Comments