简体   繁体   English

使用React Date Range选择器过滤React表

[英]Using React Date Range picker to filter a React table

I have a react table which gets populated from an API. 我有一个从API填充的反应表。 I wanted to filter a column based on a Date range picker. 我想根据日期范围选择器过滤列。 Problem is that the code does not reach the FilterMethod. 问题是代码没有到达FilterMethod。 What could I be doing wrong?. 我能做错什么? Here is my code 这是我的代码

const columnsvisit = [{
  id: 'date',
  Header: 'Date',
  accessor: rowProps => rowProps.date,

  Filter: ({ filter, onChange }) =>
       <div>
        <DateRangePicker  startDate={this.state.startDate}
                          endDate={this.state.endDate} ranges={this.state.ranges} 
                          onEvent={this.handleEvent} 
                          >

        <Button className="selected-date-range-btn" style={{width:'100%'}}>


            <span >
            <input type="text" name="labrl" value={label }
            onChange={event => onChange(event.target.value)} readOnly 
            />
            </span>
            <span className="caret"></span>

        </Button>
      </DateRangePicker>
      </div>,
 filterMethod: (filter, row) => {
  console.log(filter.value)

},

We ran into the same problem and @CrustyRatFink discovered that the issue was that we were using component state to manage the value of the DateRangePicker so when you changed the date it would trigger a re-render, which cleared out react-table's filtered list. 我们遇到了同样的问题并且@CrustyRatFink发现问题是我们使用组件状态来管理DateRangePicker的值,所以当你更改日期时它会触发重新渲染,这会清除react-table的过滤列表。

The solution is to either manage the filtered list in component state: 解决方案是管理组件状态中的筛选列表:

class App extends React.Component {
  constructor() {
    super();
    this.state = {
       filtered: []
    }
  }
  render() {
    return (
      <ReactTable
        filtered={this.state.filtered}
        onFilteredChange={filtered => this.setState({ filtered })}
        ...
      />
    )
  }
}

or to manage the state and value of the DateRangePicker in a separate component so it doesn't trigger a re-render of the component containing the table. 或者在单独的组件中管理DateRangePicker的状态和值,因此它不会触发包含该表的组件的重新呈现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM