Ant design show record on row click

How can I show the information of a record (in the show screen) when the corresponding row is clicked in the list view in ant design?
12 Replies
kapa.ai
kapa.ai5mo ago
Hi @BlackBearFTW. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To show the information of a record when a row is clicked in the list view, you can use the <ShowButton> component from Refine. This component uses the show method from useNavigation under the hood and can be useful when redirecting the app to the show page with the record id route of resource. Here is an example of how you can use it:
import {
List,
useTable,
ShowButton,
} 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="100%" />
<Table.Column<IPost>
title="Actions"
dataIndex="actions"
key="actions"
render={(_, record) => (
<ShowButton size="small" recordItemId={record.id} />
)}
/>
</Table>
</List>
);
};

interface IPost {
id: number;
title: string;
}
import {
List,
useTable,
ShowButton,
} 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="100%" />
<Table.Column<IPost>
title="Actions"
dataIndex="actions"
key="actions"
render={(_, record) => (
<ShowButton size="small" recordItemId={record.id} />
)}
/>
</Table>
</List>
);
};

interface IPost {
id: number;
title: string;
}
In this example, the ShowButton is used in the render function of the Table.Column component. The recordItemId prop is used to append the record id to the end of the route path. By default, the recordItemId is inferred from the route params. When the ShowButton is clicked, it will trigger the show method of useNavigation and then redirect the app to the show action path of the resource, filling the necessary parameters in the route. For more information, you can refer to the Refine documentation (https://refine.dev/docs/ui-integrations/ant-design/components/buttons/show-button). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@490961337896402944> kapa.ai is still learning and improving, please let us know how it did by reacting below
adverse-sapphire
adverse-sapphire5mo ago
@kapa.ai I want to do that but without the user clicking the show button, it should be the entire row in the list
kapa.ai
kapa.ai5mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To make the entire row clickable and redirect to the show page of the record, you can use the onRow prop of the Ant Design's Table component. This prop allows you to set event handlers on table rows. Here's an example of how you can use it:
import { List, useTable } from "@refinedev/antd";
import { Table } from "antd";
import { useHistory } from "react-router-dom";

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

return (
<List>
<Table
{...tableProps}
rowKey="id"
onRow={(record) => ({
onClick: () => {
history.push(`/posts/show/${record.id}`);
},
})}
>
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" width="100%" />
</Table>
</List>
);
};

interface IPost {
id: number;
title: string;
}
import { List, useTable } from "@refinedev/antd";
import { Table } from "antd";
import { useHistory } from "react-router-dom";

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

return (
<List>
<Table
{...tableProps}
rowKey="id"
onRow={(record) => ({
onClick: () => {
history.push(`/posts/show/${record.id}`);
},
})}
>
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" width="100%" />
</Table>
</List>
);
};

interface IPost {
id: number;
title: string;
}
In this example, we're using the useHistory hook from react-router-dom to programmatically navigate to the show page of the clicked record. The onClick handler is set to push a new entry onto the history stack with the path of the show page of the clicked record. Please note that this code assumes that your resource is named "posts". You should replace "posts" with the actual name of your resource. This solution does not directly come from the provided extracts, but it's a common pattern in React and Ant Design applications. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@490961337896402944> kapa.ai is still learning and improving, please let us know how it did by reacting below
adverse-sapphire
adverse-sapphire5mo ago
@kapa.ai make this futureproof, what if the show url changes, there must be a different way
kapa.ai
kapa.ai5mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To make your code future-proof and handle potential changes in the URL structure, you can use the useNavigation hook provided by Refine. This hook provides a show method that generates the correct URL for the show page of a record. Here's how you can use it:
import { List, useTable, useNavigation } from "@refinedev/antd";
import { Table } from "antd";

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

return (
<List>
<Table
{...tableProps}
rowKey="id"
onRow={(record) => ({
onClick: () => {
show("posts", String(record.id));
},
})}
>
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" width="100%" />
</Table>
</List>
);
};

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

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

return (
<List>
<Table
{...tableProps}
rowKey="id"
onRow={(record) => ({
onClick: () => {
show("posts", String(record.id));
},
})}
>
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" width="100%" />
</Table>
</List>
);
};

interface IPost {
id: number;
title: string;
}
In this example, the show method from useNavigation is used to navigate to the show page of the clicked record. This method generates the correct URL based on the resource name and record id, so it will still work correctly even if the URL structure changes in the future. Please note that this code assumes that your resource is named "posts". You should replace "posts" with the actual name of your resource. For more information, you can refer to the Refine documentation (https://refine.dev/docs/routing/hooks/use-navigation/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@490961337896402944> kapa.ai is still learning and improving, please let us know how it did by reacting below
adverse-sapphire
adverse-sapphire5mo ago
@kapa.ai This does not work
kapa.ai
kapa.ai5mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- I'm sorry to hear that the solution didn't work for you. Unfortunately, based on the information provided in the extracts, I don't have enough information to troubleshoot the issue further. I would recommend checking the Refine documentation (https://refine.dev/docs) or the Refine GitHub Discussions (https://github.com/pankod/refine/discussions) for more help. You can also post a new question there, providing more details about your issue, and the community or the Refine team might be able to help you. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
fascinating-indigo
fascinating-indigo5mo ago
Hey @BlackBearFTW, you can use onRow prop of <Table /> to pass onClick handlers to each of the row and trigger a navigation there using useNavigation or useGo of Refine. Check out the Ant Design API here https://ant.design/components/table#table Check out useNavigation docs to learn how to handle navigations with it https://refine.dev/docs/routing/hooks/use-navigation/
adverse-sapphire
adverse-sapphire5mo ago
Thank you