I have a checkbox in the first column of each row in the table. I can toggle the checkbox by clicking it.
How do I properly, "antd way", make it toggle by clicking in any area of the whole row?
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.
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.
Add a property "hidden" to the column object, then filter it out. Save this answer.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With