ratty-blush
ratty-blush8mo ago

Table selection

How to use delete action on selected line with table selection in ant design
8 Replies
kapa.ai
kapa.ai8mo ago
Hi @Sly. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- In refine, you can use the DeleteButton component from @refinedev/antd to delete a record in a table. This component uses Ant Design's Button and Popconfirm components. When you click the delete button, a pop-up shows up and asks for confirmation. When confirmed, it executes the useDelete method provided by your dataProvider. Here's an example of how to use the DeleteButton in a table row:
import {
List,
useTable,
DeleteButton,
} from "@refinedev/antd";
import { Table } from "antd";

const PostList: React.FC = () => {
const { tableProps } = useTable<IPost>();

return (
<List>
<Table {...tableProps} rowKey="id">
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" width="50%" />
<Table.Column<IPost>
title="Actions"
dataIndex="actions"
key="actions"
render={(_, record) => (
<DeleteButton size="small" recordItemId={record.id} />
)}
width="50%"
/>
</Table>
</List>
);
};

interface IPost {
id: number;
title: string;
}
import {
List,
useTable,
DeleteButton,
} from "@refinedev/antd";
import { Table } from "antd";

const PostList: React.FC = () => {
const { tableProps } = useTable<IPost>();

return (
<List>
<Table {...tableProps} rowKey="id">
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" width="50%" />
<Table.Column<IPost>
title="Actions"
dataIndex="actions"
key="actions"
render={(_, record) => (
<DeleteButton size="small" recordItemId={record.id} />
)}
width="50%"
/>
</Table>
</List>
);
};

interface IPost {
id: number;
title: string;
}
In this example, the DeleteButton is used in the render method of the actions column. The recordItemId prop is used to specify the id of the record to be deleted. If you want to delete multiple records at once, you can use the useDeleteMany hook. This hook removes more than one data from the database. You can remove multiple records by selecting them and pressing delete. For more details, you can refer to the following documentation: - DeleteButton documentation (https://refine.dev/docs/ui-integrations/ant-design/components/buttons/delete-button) - useDeleteMany documentation (https://refine.dev/docs/data/hooks/use-delete/) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai8mo ago
<@1172098337760096258> kapa.ai is still learning and improving, please let us know how it did by reacting below
ratty-blush
ratty-blush8mo ago
@kapa.ai this is the ant design function:
kapa.ai
kapa.ai8mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- I'm sorry, but your question seems incomplete. Could you please provide more details or clarify what you need help with regarding the Ant Design function in refine? ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai8mo ago
<@1172098337760096258> kapa.ai is still learning and improving, please let us know how it did by reacting below
ratty-blush
ratty-blush8mo ago
@kapa.ai const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]); const onSelectChange = (newSelectedRowKeys: React.Key[]) => { console.log('selectedRowKeys changed: ', newSelectedRowKeys); setSelectedRowKeys(newSelectedRowKeys); }; const rowSelection: TableRowSelection<IProdutos> = { selectedRowKeys, onChange: onSelectChange, selections: [ Table.SELECTION_ALL, Table.SELECTION_INVERT, Table.SELECTIONNONE, { key: 'odd', text: 'Select Odd Row', onSelect: (changeableRowKeys) => { let newSelectedRowKeys = []; newSelectedRowKeys = changeableRowKeys.filter((, index) => { if (index % 2 !== 0) { return false; } return true; }); setSelectedRowKeys(newSelectedRowKeys); }, }, { key: 'even', text: 'Select Even Row', onSelect: (changeableRowKeys) => { let newSelectedRowKeys = []; newSelectedRowKeys = changeableRowKeys.filter((_, index) => { if (index % 2 !== 0) { return true; } return false; }); setSelectedRowKeys(newSelectedRowKeys); }, }, ], };
kapa.ai
kapa.ai8mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- The code you provided is a typical way to handle row selection in Ant Design's Table component. It uses a state variable selectedRowKeys to keep track of the keys of the selected rows. The onSelectChange function updates this state when the selection changes. The rowSelection object is then passed to the rowSelection prop of the Table component. It specifies how row selection works and what happens when the selection changes. Here's a brief explanation of the properties in the rowSelection object: - selectedRowKeys: This is an array of the keys of the currently selected rows. - onChange: This is a callback function that is called when the selection changes. It receives the keys of the newly selected rows. - selections: This is an array of additional selection options. In your code, it includes options to select all rows, invert the current selection, clear the selection, select odd rows, and select even rows. If you want to perform an action (like deleting a row) when a row is selected, you can do so in the onChange callback or in the onSelect callbacks of the additional selection options. For more information, you can refer to the Ant Design Table documentation (https://ant.design/components/table/). Please note that while this is a standard way to handle row selection in Ant Design, refine provides its own hooks and components for handling data and actions in tables. For example, you can use the useTable hook to create a table with sorting, filtering, and pagination features compatible with Ant Design's Table component. You can also use the DeleteButton component to delete a row. You can find more information about these features in the refine documentation (https://refine.dev/docs). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai8mo ago
<@1172098337760096258> kapa.ai is still learning and improving, please let us know how it did by reacting below