How can all CRUD actions of a resource by linked to a particular foreign key?

做~自己de王妃 提交于 2019-12-24 08:56:46

问题


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



来源:https://stackoverflow.com/questions/49289320/how-can-all-crud-actions-of-a-resource-by-linked-to-a-particular-foreign-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!