Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to toggle a checkbox in a row of a antd table?

Tags:

reactjs

antd

I have a checkbox in the first column of each row in the table. I can toggle the checkbox by clicking it.

example

How do I properly, "antd way", make it toggle by clicking in any area of the whole row?

like image 776
Sergei Basharov Avatar asked Oct 17 '22 08:10

Sergei Basharov


People also ask

How do I select a row 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 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.

How do I hide columns in ANTD table?

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

How do I add sorting to 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.


2 Answers

It is there in antd documentation, but you need to take a state to handle it.

Rows can be selectable by making first column as a selectable column.

You can use

onRow={record => ({
  onClick: () => {
    this.selectRow(record);
  }
})}

and in selectRow

selectRow = record => {
  const selectedRowKeys = [...this.state.selectedRowKeys];
  if (selectedRowKeys.indexOf(record.key) >= 0) {
    selectedRowKeys.splice(selectedRowKeys.indexOf(record.key), 1);
  } else {
    selectedRowKeys.push(record.key);
  }
  this.setState({ selectedRowKeys });
}

Demo and Source

like image 92
Just code Avatar answered Oct 21 '22 09:10

Just code


There is an onRow property that you can use. See the example from the official documentation:

class App extends React.Component {
  state = {
    selectedRowKeys: [],
  };

  selectRow = (record) => {
    const selectedRowKeys = [...this.state.selectedRowKeys];
    if (selectedRowKeys.indexOf(record.key) >= 0) {
      selectedRowKeys.splice(selectedRowKeys.indexOf(record.key), 1);
    } else {
      selectedRowKeys.push(record.key);
    }
    this.setState({ selectedRowKeys });
  }

  onSelectedRowKeysChange = (selectedRowKeys) => {
    this.setState({ selectedRowKeys });
  }

  render() {
    const { selectedRowKeys } = this.state;

    const rowSelection = {
      selectedRowKeys,
      onChange: this.onSelectedRowKeysChange,
    };

    return (
      <Table
        rowSelection={rowSelection}
        columns={columns}
        dataSource={data}
        onRow={(record) => ({
          onClick: () => {
            this.selectRow(record);
          },
        })}
      />
    );
  }
}

Source

like image 22
Daniel Studziński Avatar answered Oct 21 '22 11:10

Daniel Studziński