单击react-admin中的列表标题后排序不起作用

时间:2019-05-10 13:49:32

标签: reactjs react-admin

我遵循了react-admin的教程。一切正常,直到我连接到自己的后端。该列表显示正确,但是当我单击任何标题时,它根本没有排序。

我将自定义数据提供程序与教程中提供的jsonplaceholder API一起使用,并且工作正常。但是唯一的问题是排序无法正常工作。

我该如何解决这个问题?

如果有人知道答案,请仅以reactjs或react-admin提供解决方案。

Image of how site looks like

import API from "./config";
import {
    GET_LIST,
    GET_ONE,
    GET_MANY,
    GET_MANY_REFERENCE,
    CREATE,
    UPDATE,
    DELETE,
    DELETE_MANY,
    fetchUtils,
} from 'react-admin';

import { stringify } from 'query-string';

const API_URL = `${API.domain}${API.admin_path}`;

/**
 * @param {String} type One of the constants appearing at the top if this 
 file, e.g. 'UPDATE'
 * @param {String} resource Name of the resource to fetch, e.g. 'posts'
 * @param {Object} params The Data Provider request params, depending on the 
 type
 * @returns {Object} { url, options } The HTTP request parameters
 */



const convertDataProviderRequestToHTTP = (type, resource, params) => {
    let options = {
        headers: new Headers({ Accept: 'application/json' })
    }
    const token = localStorage.getItem('token');
    options.headers.set('Authorization', `Bearer ${token}`);
    switch (type) {
        case GET_LIST: {
            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),
            };
            return { url: `${API_URL}/${resource}?${stringify(query)}`, 
            options };
        }
        case GET_ONE:
            return { url: `${API_URL}/${resource}/${params.id}`, options };
        case GET_MANY: {
            const query = {
                filter: JSON.stringify({ id: params.ids }),
            };
            return { url: `${API_URL}/${resource}?${stringify(query)}` };
        }
        case GET_MANY_REFERENCE: {
            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, [params.target]: 
                params.id }),
            };
            return { url: `${API_URL}/${resource}?${stringify(query)}` };
        }
        case UPDATE:
            return {
                url: `${API_URL}/${resource}/${params.id}`,
                options: { ...options, method: 'PUT', body: 
                JSON.stringify(params.data) },
            };
        case CREATE:
            return {
                url: `${API_URL}/${resource}`,
                options: { ...options, method: 'POST', body: 
                JSON.stringify(params.data) },
            };
        case DELETE:
            return {
                url: `${API_URL}/${resource}/${params.id}`,
                options: { method: 'DELETE' },
            };
        case DELETE_MANY:
            const query = {
                filter: JSON.stringify({ id: params.ids }),
            };
            return {
                url: `${API_URL}/${resource}?${stringify(query)}`,
                options: { method: 'DELETE' },
            };
        default:
            throw new Error(`Unsupported fetch action type ${type}`);
    }
};

/**
 * @param {Object} response HTTP response from fetch()
 * @param {String} type One of the constants appearing at the top if this 
   file, e.g. 'UPDATE'
 * @param {String} resource Name of the resource to fetch, e.g. 'posts'
 * @param {Object} params The Data Provider request params, depending on the 
   type
 * @returns {Object} Data Provider response
 */

const convertHTTPResponseToDataProvider = (response, type, resource, params) 
 => {
    const { headers, json } = response;
    switch (type) {
       case GET_LIST:
            return {
                data: json.data.map(x => x),
                total: 10, //parseInt(headers.get('content- 
                range').split('/').pop(), 10),
            };
        case CREATE:
            return { data: { ...params.data, id: json.id } };
        default:
            return { data: json.data };
    }
};

/**
 * @param {Object} response HTTP response from fetch()
 * @param {String} type One of the constants appearing at the top if this 
   file, e.g. 'UPDATE'
 * @param {String} resource Name of the resource to fetch, e.g. 'posts'
 * @param {Object} params The Data Provider request params, depending on the 
 type
 * @returns {Object} Data Provider response
 */

const convertHTTPResponseToDataProvider = (response, type, resource, params) 
=> {
    const { headers, json } = response;
    switch (type) {
        case GET_LIST:
            return {
                data: json.data.map(x => x),
                total: 10, //parseInt(headers.get('content- 
                range').split('/').pop(), 10),
            };
        case CREATE:
            return { data: { ...params.data, id: json.id } };
        default:
            return { data: json.data };
    }
};

/**
 * @param {string} type Request type, e.g GET_LIST
 * @param {string} resource Resource name, e.g. "posts"
 * @param {Object} payload Request parameters. Depends on the request type
 * @returns {Promise} the Promise for response
 */
export default (type, resource, params) => {
    const { fetchJson } = fetchUtils;
    const { url, options } = convertDataProviderRequestToHTTP(type, 
    resource, params);
    return fetchJson(url, options)
        .then(response => convertHTTPResponseToDataProvider(response, type, 
         resource, params));
};

1 个答案:

答案 0 :(得分:0)

使用不同的排序选项在React-Admin中排序对数据库发出新请求,检查您的自定义dataProvider是否返回正确排序的数据,以及您的后端是否理解该请求的搜索字符串。