ra-order-buttons
This addon provides a component which renders up and down buttons to reorder records in react-admin. It connects with your Data Provider by dispatching a move
action. All the magic is done in the backend so a custom logic is needed to make it work correctly. Check the backend section below to learn how to adapt your API.
Installation
npm install --save ra-order-buttons
# or
yarn add ra-order-buttons
Usage
You should pass as source to <OrderButtons />
the attribute you are using to order the records.
import React from 'react';import List Datagrid TextField from 'react-admin';import OrderButtons from 'ra-order-buttons'; const ProductList = <List > <Datagrid> <TextField ="id" /> <TextField ="name" /> <OrderButtons ="ordering" /> </Datagrid> </List>;
In your Data Provider you will need to handle the new request type move
and map it to the appropriate API call. It is called as follow:
dataProvider;
Implementing the backend
When user clicks in one of the two buttons we have to switch the order value between the clicked record and the immediately above (move up) or below record (move down). The example below was based on the logic behind Joomla's Administration page which has the same functionality. You can check the original code here.