How can all CRUD actions of a resource by linked t

2019-08-01 10:36发布

问题:

Version: Next (react-admin)

Resources Classes and Students.

"Students" has a foreign key to "Classes": class_id.

So, in the List of Classes we have for each row (class) a button "View/Edit Students of Class".

When we click on this button, we have this code:

<Button
  label={'View/Edit Students of Class'}
  onClick={(e) => {
    localStorage.setItem('classId', record.id);
    history.push({
        pathname: 'students',
        search: stringify({
            page: 1,
            perPage: 25,
            sort: 'createdOn',
            order: 'DESC',
            classId: record.id,
        }),
    })
  }}
>

The idea is that we need somehow to get a 'classId' value in all CRUD operations of the 'Student', so that we filter by 'classId' in List and use this classId as a disabled field in CREATE.

I tried 2 ways: localStorage or query params as you can see in the code.

But these are not good, not working as expected (query params are lost when CREATE redirects, etc.)

Any ideas?

回答1:

You should link to a filtered list, something like

<Button
  label={'View/Edit Students of Class'}
  onClick={(e) => {
    localStorage.setItem('classId', record.id);
    history.push({
        pathname: 'students',
        search: stringify({
            page: 1,
            perPage: 25,
            sort: 'createdOn',
            order: 'DESC',
            filter: { classId: record.id },
        }),
    })
  }}
>

This is done in the demo, see https://github.com/marmelab/admin-on-rest-demo/blob/master/src/categories/LinkToRelatedProducts.js