Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get filtered data in component table after executing the filter function on dataSource in ant design

Tags:

reactjs

antd

When useing the Table component in Ant-desigin, i want to get the filterd data in table after executing the filter function on the dataSource, but i cannot find the way to get it. There is a function called onChange which can only get the filter conditions which can not get the filtered data.

like image 792
Jochen Shi Avatar asked Apr 28 '18 09:04

Jochen Shi


People also ask

How do you make an ant design table responsive?

Just add a From To column with a custom render function, and set the responsive property on that column to only show on xs screens. The From and To columns will have the responsive property set to show on md and above.

How do I sort in ANTD table?

Simply define a new sorting routine in utils/sorter. js , add it to the Sorter “enum,” and use it in your sorter prop for any column that needs it. You can check out the CodeSandbox for this tutorial to play around with the result.

How do I select rows in ANTD table?

Rows can be selectable by making first column as a selectable column. You can use rowSelection.type to set selection type. Default is checkbox . selection happens when clicking checkbox by default.

How do I hide columns in ANTD table?

Add a property "hidden" to the column object, then filter it out. Save this answer.


3 Answers

I kinda found a way. The props title and footer on the <Table /> component take a function which does provide the filtered data.

<Table
  footer={currentPageData => {
    console.log(currentPageData); // <-- This is an array of the filtered dataSource.
    return null;
  }}
/>
like image 74
air Avatar answered Nov 05 '22 04:11

air


Add onChange function to Table(sample code is ts, if you are using js, do a little adjustment. And you can check the debug result: total dataSource size is 54, filtered dataSource size is 40, pagination doesn't impact the result

  handleChange = (pagination: any, filters: any, sorter: any, extra: { currentDataSource: Array<any>[] }) => {
    console.log('Various parameters', extra.currentDataSource);
    this.setState({
      filteredInfo: extra['currentDataSource']
    });
  }

  renderTable = (columns: Array<object>, dataSource: Array<any>) => {
    return (
      <Table
        dataSource={dataSource}
        columns={columns}
        expandedRowRender={(record) => (<Markdown source={record['description']} className="brz-mentor-markdown" />)}
        onChange={this.handleChange as any}
      />
    )
  }

enter image description here

like image 36
wenwen Avatar answered Nov 05 '22 04:11

wenwen


You can easily add onChange attribute inside <Table/>. Create a function with 4 params: pagination, filters, sorter, extra and the data you're looking for is extra.currentDataSource.

onChange={
     (pagination, filters, sorter, extra) => {
          console.log(extra.currentDataSource)
     } 
} 
like image 3
Lintang Wisesa Avatar answered Nov 05 '22 03:11

Lintang Wisesa