[英]How to change React-Admin filters
I am new to react, and I use react-admin for my dashboard.我是新手,我在仪表板中使用 react-admin。 But I have problem of fetching data from my API.
但是我从我的 API 中获取数据时遇到问题。
here's my API:这是我的 API:
http://localhost:3333/verifications/browse?page=1&per_page=10&verification_level_id=2&verification_status_id=3
then I saw the endpoint that react-admin was using,然后我看到了 react-admin 正在使用的端点,
http://localhost:3333/verifications/browse?filter=%7B%7D&range=%5B0%2C9%5D&sort=%5B%22id%22%2C%22ASC%22%5D
how can I change that endpoint?我怎样才能改变那个端点?
The place to modify the request before it is sent (and the response after it is received) is the dataProvider .在发送之前修改请求(以及接收到响应之后)的地方是dataProvider 。 React-admin has extensive documentation about it .
React-admin 有大量关于它的文档。 If you don't understand dataProviders in react-admin, you're going to meet a lot of blockers, so this should be your starting point.
如果你不了解 react-admin 中的 dataProviders,你会遇到很多阻碍,所以这应该是你的起点。
I believe you're using ra-data-simplerest as a base for your example (or is it something else?), you can override the getList
to something that better fits your need, based on the following implementation:我相信您正在使用 ra-data-simplerest 作为示例的基础(或者是其他东西?),您可以根据以下实现将
getList
覆盖为更适合您需要的东西:
import { fetchUtils } from 'react-admin';
import { stringify } from 'query-string';
import simpleRestDataProvider from './simpleRestDataProvider';
const apiUrl = 'https://my.api.com/';
const httpClient = fetchUtils.fetchJson;
const dataProvider = {
...simpleRestDataProvider,
getList: (resource, params) => {
const { page, perPage } = params.pagination;
const { field, order } = params.sort;
const query = {
sort: JSON.stringify([field, order]),
range: JSON.stringify([(page - 1) * perPage, page * perPage - 1]),
filter: JSON.stringify(params.filter),
};
const url = `${apiUrl}/${resource}?${stringify(query)}`;
return httpClient(url).then(({ headers, json }) => ({
data: json,
total: parseInt(headers.get('content-range').split('/').pop(), 10),
}));
},
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.