Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset ant design table selected rows?

  • I am using ant design table component and I have selected rows.
  • I want onClick reset selected rows.
  • I can not find out where it stores selected rows.

      const rowSelection = {
            onChange: (selectedRowKeys, rows) => {
              this.setState({
                selectedRowsArray: [...rows]
              });
            },
          };
    
      <Table rowSelection={rowSelection} columns={columns} dataSource={paymentsHistory} />
    

Any Idea how to clear selected rows?

like image 712
Yerlan Yeszhanov Avatar asked Oct 14 '19 05:10

Yerlan Yeszhanov


People also ask

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 you sort an 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 make an ANTD table responsive?

You can make use of the responsive property on the column that you want to control for screen sizes. 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.


2 Answers

rowSelection also takes selectedRowKeys property that will help you control the selected rows at any point in time.

const { selectedRowsArray } = this.state;
const rowSelection = {
      selectedRowKeys: selectedRowsArray,
      onChange: (selectedRowKeys, rows) => {
        this.setState({
          selectedRowsArray: [...rows]
        });
      },
    };

<Table rowSelection={rowSelection} columns={columns} dataSource={paymentsHistory} />

Codesandbox Example | Antd Docs

like image 75
Agney Avatar answered Oct 19 '22 06:10

Agney


We can also do this with hooks:

import { useState } from 'react';
import { Table, Button } from 'antd';

function App() {
    const [selectedRowKeys, setRowKeys] = useState([]);
    const [loading, setLoading] = useState([]);


    const start = () => {
        setRowKeys([]);
    };

    const onSelectChange = selectedRowKeys => {
        setRowKeys(selectedRowKeys);
    };

    const rowSelection = {
        selectedRowKeys,
        onChange: onSelectChange,
    };

    const dataSource = [
        {
            key: '1',
            name: 'Mike',
            age: 32,
            address: '10 Downing Street',
        },
        {
            key: '2',
            name: 'John',
            age: 42,
            address: '10 Downing Street',
        }, enter code here
    ];

    const columns = [
        {
            title: 'Name',
            dataIndex: 'name',
            key: 'name',
        },
        {
            title: 'Age',
            dataIndex: 'age',
            key: 'age',
        },
        {
            title: 'Address',
            dataIndex: 'address',
            key: 'address',
        },
    ];


    return (
        <div className="App">
            <Button type="primary" onClick={start} >
                Reload
            </Button>
            <Table dataSource={dataSource} columns={columns} rowSelection={rowSelection} />;
        </div>
    );
}

export default App;
like image 23
Anurag Tripathi Avatar answered Oct 19 '22 06:10

Anurag Tripathi