table icon indicating copy to clipboard operation
table copied to clipboard

filterMethod with react-dates

Open mofbe opened this issue 9 years ago • 17 comments

Problem Description

Hi,

I'm trying to add a <DateRangePicker /> component from react-dates in filterRender and then check if the values in the grid are in the given date range.

The problem is that the filterMethod seems to to works, the function is called, but the data in the grid is not filtered. Even if a return false; at the top of the filterMethod, nothing changes.

Here is my code:

filterRender: ({filter, onFilterChange}) => (
  <DateRangePicker
    startDate={this.state.dobStartDate}
    endDate={this.state.dobEndDate}
    onDatesChange={({ startDate, endDate }) => {this.setState({ dobStartDate: startDate, dobEndDate: endDate }); onFilterChange({startDate, endDate});}}
    focusedInput={this.state.focusedInput}
    onFocusChange={focusedInput => this.setState({ focusedInput })}
    isOutsideRange={() => false}
    withPortal={true}
    showClearDates={true}
  />
),
filterMethod: (filter, row) => {
  if (filter.value.startDate === null || filter.value.endDate === null) {
    // Incomplet or cleared date picker
    return true
  }

  if (moment(row[filter.id]).isBetween(filter.value.startDate, filter.value.endDate)) {
    // Found row matching filter
    return true
  }
}

I have also added an headerClassName to the column. The cssClass is setting the overflow to visible. Without this, the <DateRangePicker /> isn't displayed.

I don't know if I'm missing something or if this is a bug with react-table or react-dates.

Thanks for your help!

mofbe avatar Apr 06 '17 15:04 mofbe

It looks good at first glance. Can you console.log(filter,row) and see if anything looks strange?

aaronschwartz avatar Apr 06 '17 15:04 aaronschwartz

Everything is good.

If I do this in filterMethod, the console.log is right.

if (filter.value.startDate === null || filter.value.endDate === null) {
  // Incomplet or cleared date picker
  console.log("Incomplet or cleared date picker")
  return true
}

if (momentCustom(row[filter.id]).isBetween(filter.value.startDate, filter.value.endDate)) {
  // Found row matching filter
  console.log("Found row matching filter")
  return true
}

mofbe avatar Apr 06 '17 15:04 mofbe

So even if you put a return false at the top of filterMethod, you can see that it is getting called and it's not filtering the table at all? I'll be on the react-chat slack channel today if you want to try and work it out.

aaronschwartz avatar Apr 06 '17 16:04 aaronschwartz

I have made some tests and I found something.

Thit was my <ReactTable />:

<ReactTable
  data={this.gridData}
  columns={this.gridColumns}
  defaultPageSize={10}
  className='-striped -highlight'
  previousText='Précedente'
  nextText='Suivante'
  loadingText='Chargement...'
  noDataText="Aucun contact n'a été trouvé" 
  pageText='Page'
  ofText='sur'
  rowsText='lignes'
  showFilters="true"
  resizable="true"
  defaultSorting={[{
    id: 'lastname',
    desc: false
  }]}
  defaultFilterMethod={(filter, row) => (String(S(row[filter.id].toLowerCase()).latinise().s).includes(S(filter.value.toLowerCase()).latinise().s))}
/>

If I remove defaultSorting, the data is filtered! Even if I manually sort the column.

I will do without defaultSorting for now but there is maybe an issue with this?

mofbe avatar Apr 06 '17 21:04 mofbe

Hmm I'm still curious. I tested the defaultSorting theory, and I could not reproduce what you're seeing. I am able to add a defaultSorting and still have filtering working normally. If you have time and could narrow it down to an extremely basic example and reproduce it, that would be great.

aaronschwartz avatar Apr 06 '17 21:04 aaronschwartz

There is a repository with an example

I let you try with and without defaultSorting.

Thanks

mofbe avatar Apr 06 '17 22:04 mofbe

Thank you. I'll take a look.

aaronschwartz avatar Apr 06 '17 22:04 aaronschwartz

I have a workaround but I still have to wrap my head around what kind of bug it is.

onDatesChange={({startDate, endDate}) => {
    this.setState({dobStartDate: startDate, dobEndDate: endDate},()=>{
        onFilterChange({startDate, endDate});
    });
}}

The problems is that the setState is causing a rerender of the table, but since setState is asynchronous it's getting queued after the filter has been added, so its overwriting the filter with the previous state which was an empty filter because of the way react-table handles state and prop changes.

The workaround just waits for the state change and rerender to happen and then adds the state change to add the filter.

aaronschwartz avatar Apr 06 '17 23:04 aaronschwartz

Perfect! Thank you for your help!

mofbe avatar Apr 07 '17 08:04 mofbe

I get me error, TypeError: onFilteredChange is not a function[Saber más]

soyJSus avatar Oct 20 '17 23:10 soyJSus

If this is not related to the problem originally specified, please open a separate issue and provide a codesandbox.io example of the problem so it can be reviewed.

gary-menzel avatar Oct 21 '17 03:10 gary-menzel

hello gary, is related, does not filter me and gives me that error, I get me error, TypeError: onFilterChange is not a function

https://pastebin.com/XztBeRhT

soyJSus avatar Oct 21 '17 13:10 soyJSus

The problem is in how you are accessing the parameters to Filter. They are actually called: column,filter, and onChange. If you access them without using the variable desructuring (e.g. (theColumn,theFilter,onFilterChange) => {} ) then you can pick any names you like (but they must be in that order). If you choose to use destructuring, they must be the same name as they are in the object passed in - so, ({column,onChange})=>{} (and filter of course).

filter is working - because that is what it is called in the object. onFilterChange is undefined because the member is called onChange.

The doco is probably in need of some updating but always check the samples and (if in doubt) the source code.

Also, pastebin is not very useful for doing debugging work. Best to use codesandbox.io (in the same way the samples do). Fork a sample into your own codesanbox.io, add in any dependencies for what you want to test (e.g. the Date Picker) and then reproduce the issue. Then someone can pick it up and do the same to play with it. codesandbox.io is the preferred method for providing code for issues regarding react-table.

You should probably consider joining the #react-table Slack channel. There is a badge at the top of the react-table doco.

gary-menzel avatar Oct 21 '17 13:10 gary-menzel

sorry for the inconvenience, following the example above, as would be with the new version of react, react- table and react-date? I just do not understand very well

soyJSus avatar Oct 21 '17 14:10 soyJSus

destructing is an ES6 Javascript thing - nothing to do with React, React Table or React Date.

In simple terms, onFilterChange did not exist because of the way you are getting the parameter. If you change it to onChange then it should work.

Check this story in the doco.....

https://react-table.js.org/#/story/custom-filtering

And just use the same name for the onChange event.

gary-menzel avatar Oct 21 '17 14:10 gary-menzel

@gary-menzel thanks

soyJSus avatar Oct 21 '17 15:10 soyJSus

I have a workaround but I still have to wrap my head around what kind of bug it is.

onDatesChange={({startDate, endDate}) => {
    this.setState({dobStartDate: startDate, dobEndDate: endDate},()=>{
        onFilterChange({startDate, endDate});
    });
}}

The problems is that the setState is causing a rerender of the table, but since setState is asynchronous it's getting queued after the filter has been added, so its overwriting the filter with the previous state which was an empty filter because of the way react-table handles state and prop changes.

The workaround just waits for the state change and rerender to happen and then adds the state change to add the filter.

i tried that but when i check end date the table be empty !

amrrizk95 avatar Apr 30 '20 13:04 amrrizk95