问题
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