tableRequest, rowsOf
tableRequest connects a hand-written ProTable to a list endpoint: paging, sorting, filters and the search box go straight to the query of listOf. rowsOf reads { data, total } from either an array or a page.
Most list pages need neither: <ResourceList> from manh/shell already has status tabs, search, paging and a create form, see List pages. Use tableRequest when the table has its own layout.
Usage
import { useRef } from "react";
import { PageContainer, ProTable, type ActionType } from "@ant-design/pro-components";
import { useApiClient } from "app/api";
import { tableRequest } from "app/hooks";
import { orderColumns } from "./orders-columns";
export default function OrdersPage() {
const api = useApiClient();
const ref = useRef<ActionType>();
return (
<PageContainer title="Orders">
<ProTable rowKey="id" actionRef={ref} columns={orderColumns}
request={tableRequest(api.orders.list)} options={{ search: true }} />
</PageContainer>
);
}Signature
function tableRequest(
list: (q: any) => Promise<any>,
extra?: Record<string, unknown>,
): (params?: any, sort?: Record<string, "ascend" | "descend" | null>, filter?: Record<string, any>) =>
Promise<{ data: any[]; total: number; success: boolean }>;
function rowsOf<T = any>(r: any): { data: T[]; total: number };Parameters
tableRequest(list, extra?):
list: a list function from useApiClient, such asapi.orders.list.extra: fields sent with every load, such as{ customerId: 12 }to show one customer’s orders.
rowsOf(r):
r: the result of a list endpoint: an array, or an object withdata(oritems,rows) andtotal.
Returns
tableRequest returns a function in the shape ProTable’s request expects. On every load it sends:
pageandcurrent(the same page number),pageSize;- the values of the table’s search form under the column names, empty fields left out;
- column filters: one value as is, several as an array;
- the toolbar search text as
search; - the sorted column as
sort, withorderset toascordesc.
The endpoint may return an array or { data, total }. Other keys of the result (counts per status, say) reach ProTable’s onLoad. On error it shows the server’s message, answers success: false and the table keeps its rows; it doesn’t throw.
rowsOf returns { data, total }; for an array, total is its length.
Examples
Names in sort and the filter fields must be declared on the server, or they are dropped from the query:
export const schema = {
query: listSchema({ filters: { status: z.enum(OrderStatus.values).optional() }, sort: ["createdAt", "total"] }),
};Reading a list elsewhere:
const { data } = useRequest(() => api.orders.list({ status: "new", pageSize: 5 }));
const { data: rows, total } = rowsOf(data);Next steps
- listSchema, listOf: the server side of the same table.
- submit: create and edit forms that reload this table.
- List pages: when to use
<ResourceList>instead of a hand-written table.