Ant design table

export default function PostList() {
const { tableProps } = useTable<Course>()

return (
<Table {...tableProps} rowKey="id">
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" />
<Table.Column dataIndex="imageSrc" title="Image" />
</Table>
)
}
export default function PostList() {
const { tableProps } = useTable<Course>()

return (
<Table {...tableProps} rowKey="id">
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" />
<Table.Column dataIndex="imageSrc" title="Image" />
</Table>
)
}
How can I make each row of the table link on click to another route? For example, instead of clicking on an edit button, I want the user to be able to edit an item by clicking on its row.
8 Replies
kapa.ai
kapa.ai2w ago
Hi @PIGFY. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To make each row of the Ant Design table link to another route when clicked, you can use the onRow prop of the <Table> component to pass onClick handlers to each row. You can then use the useNavigation hook from Refine to handle the navigation. Here is an example of how you can achieve this:
import { useTable, useNavigation } from "@refinedev/antd";
import { Table } from "antd";

export default function PostList() {
const { tableProps } = useTable<Course>();
const { edit } = useNavigation();

return (
<Table
{...tableProps}
rowKey="id"
onRow={(record) => ({
onClick: () => {
edit("posts", record.id);
},
})}
>
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" />
<Table.Column dataIndex="imageSrc" title="Image" />
</Table>
);
}
import { useTable, useNavigation } from "@refinedev/antd";
import { Table } from "antd";

export default function PostList() {
const { tableProps } = useTable<Course>();
const { edit } = useNavigation();

return (
<Table
{...tableProps}
rowKey="id"
onRow={(record) => ({
onClick: () => {
edit("posts", record.id);
},
})}
>
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" />
<Table.Column dataIndex="imageSrc" title="Image" />
</Table>
);
}
In this example: - The useNavigation hook is used to get the edit function. - The onRow prop is used to add an onClick handler to each row. - When a row is clicked, the edit function is called with the resource name ("posts") and the record ID, which navigates to the edit page for that record. This approach ensures that clicking on any row will navigate the user to the edit page for the corresponding item. For more details, you can refer to the useNavigation documentation (https://refine.dev/docs/routing/hooks/use-navigation/) and the Ant Design Table API (https://ant.design/components/table#table). Sources: - Discord support thread: Ant design show record on row click (https://discord.com/channels/837692625737613362/1197200118529663046) - Editing by clicking to row (https://refine.dev/docs/ui-integrations/ant-design/hooks/use-editable-table/#editing-by-clicking-to-row) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2w ago
<@368167875740958721> kapa.ai is still learning and improving, please let us know how it did by reacting below
fascinating-indigo
fascinating-indigo2w ago
@kapa.ai i also have a actions column <Table.Column<Course> fixed="right" title="Actions" dataIndex="actions" render={(_, record) => { return ( <Space> <EditButton hideText size="small" recordItemId={record.id} /> <DeleteButton hideText size="small" recordItemId={record.id} /> </Space> ) }} /> how can i make ti so that the row click doesn't trigger when its the actions being clicked
kapa.ai
kapa.ai2w ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To ensure that the row click doesn't trigger when the actions column is clicked, you can stop the event propagation for the action buttons. This can be done by calling event.stopPropagation() in the onClick handlers of the action buttons. Here is how you can modify your code to achieve this:
import { useTable, useNavigation } from "@refinedev/antd";
import { Table, Space } from "antd";
import { EditButton, DeleteButton } from "@refinedev/antd";

export default function PostList() {
const { tableProps } = useTable<Course>();
const { edit } = useNavigation();
return (
<Table
{...tableProps}
rowKey="id"
onRow={(record) => ({
onClick: () => {
edit("posts", record.id);
},
})}
>
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" />
<Table.Column dataIndex="imageSrc" title="Image" />
<Table.Column<Course>
fixed="right"
title="Actions"
dataIndex="actions"
render={(_, record) => {
return (
<Space>
<EditButton
hideText
size="small"
recordItemId={record.id}
onClick={(event) => {
event.stopPropagation();
edit("posts", record.id);
}}
/>
<DeleteButton
hideText
size="small"
recordItemId={record.id}
onClick={(event) => {
event.stopPropagation();
// Add your delete logic here
}}
/>
</Space>
);
}}
/>
</Table>
);
}
import { useTable, useNavigation } from "@refinedev/antd";
import { Table, Space } from "antd";
import { EditButton, DeleteButton } from "@refinedev/antd";

export default function PostList() {
const { tableProps } = useTable<Course>();
const { edit } = useNavigation();
return (
<Table
{...tableProps}
rowKey="id"
onRow={(record) => ({
onClick: () => {
edit("posts", record.id);
},
})}
>
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" />
<Table.Column dataIndex="imageSrc" title="Image" />
<Table.Column<Course>
fixed="right"
title="Actions"
dataIndex="actions"
render={(_, record) => {
return (
<Space>
<EditButton
hideText
size="small"
recordItemId={record.id}
onClick={(event) => {
event.stopPropagation();
edit("posts", record.id);
}}
/>
<DeleteButton
hideText
size="small"
recordItemId={record.id}
onClick={(event) => {
event.stopPropagation();
// Add your delete logic here
}}
/>
</Space>
);
}}
/>
</Table>
);
}
In this example: - The onClick handlers for the EditButton and DeleteButton call event.stopPropagation() to prevent the row click event from being triggered when these buttons are clicked. - The edit function is called directly within the onClick handler of the EditButton. This ensures that clicking on the action buttons will not trigger the row click event, allowing you to handle row clicks and action button clicks separately. For more details, you can refer to the useNavigation documentation (https://refine.dev/docs/routing/hooks/use-navigation/) and the Ant Design Table API (https://ant.design/components/table#table). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2w ago
<@368167875740958721> kapa.ai is still learning and improving, please let us know how it did by reacting below
fascinating-indigo
fascinating-indigo2w ago
@kapa.ai how can i add selecting to the table
kapa.ai
kapa.ai2w ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To add row selection to your Ant Design table, you can use the rowSelection prop provided by the Ant Design Table component. This prop allows you to manage the selection state of the rows. Here is an example of how you can add row selection to your table:
import { useTable, useNavigation } from "@refinedev/antd";
import { Table, Space } from "antd";
import { EditButton, DeleteButton } from "@refinedev/antd";
import { useState } from "react";

export default function PostList() {
const { tableProps } = useTable<Course>();
const { edit } = useNavigation();
const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);

const onSelectChange = (newSelectedRowKeys: React.Key[]) => {
setSelectedRowKeys(newSelectedRowKeys);
};

const rowSelection = {
selectedRowKeys,
onChange: onSelectChange,
};
return (
<Table
{...tableProps}
rowKey="id"
rowSelection={rowSelection}
onRow={(record) => ({
onClick: () => {
edit("posts", record.id);
},
})}
>
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" />
<Table.Column dataIndex="imageSrc" title="Image" />
<Table.Column<Course>
fixed="right"
title="Actions"
dataIndex="actions"
render={(_, record) => {
return (
<Space>
<EditButton
hideText
size="small"
recordItemId={record.id}
onClick={(event) => {
event.stopPropagation();
edit("posts", record.id);
}}
/>
<DeleteButton
hideText
size="small"
recordItemId={record.id}
onClick={(event) => {
event.stopPropagation();
// Add your delete logic here
}}
/>
</Space>
);
}}
/>
</Table>
);
}
import { useTable, useNavigation } from "@refinedev/antd";
import { Table, Space } from "antd";
import { EditButton, DeleteButton } from "@refinedev/antd";
import { useState } from "react";

export default function PostList() {
const { tableProps } = useTable<Course>();
const { edit } = useNavigation();
const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);

const onSelectChange = (newSelectedRowKeys: React.Key[]) => {
setSelectedRowKeys(newSelectedRowKeys);
};

const rowSelection = {
selectedRowKeys,
onChange: onSelectChange,
};
return (
<Table
{...tableProps}
rowKey="id"
rowSelection={rowSelection}
onRow={(record) => ({
onClick: () => {
edit("posts", record.id);
},
})}
>
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" />
<Table.Column dataIndex="imageSrc" title="Image" />
<Table.Column<Course>
fixed="right"
title="Actions"
dataIndex="actions"
render={(_, record) => {
return (
<Space>
<EditButton
hideText
size="small"
recordItemId={record.id}
onClick={(event) => {
event.stopPropagation();
edit("posts", record.id);
}}
/>
<DeleteButton
hideText
size="small"
recordItemId={record.id}
onClick={(event) => {
event.stopPropagation();
// Add your delete logic here
}}
/>
</Space>
);
}}
/>
</Table>
);
}
In this example: - The rowSelection object is defined to manage the selection state of the rows. - The selectedRowKeys state is used to keep track of the selected rows. - The onSelectChange function updates the selectedRowKeys state when the selection changes. - The rowSelection prop is passed to the <Table> component to enable row selection. This setup allows you to select multiple rows in the table and manage the selection state. For more details, you can refer to the Ant Design Table API (https://ant.design/components/table#rowSelection). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2w ago
<@368167875740958721> kapa.ai is still learning and improving, please let us know how it did by reacting below