获取记录中的字段值,以在<参考input>中使用。反应通用的过滤

发布于 2025-02-11 04:48:49 字数 987 浏览 3 评论 0 原文

我想使用记录字段的值来过滤一些自动完成选项,但是我不知道如何获取使用它的值。

<ReferenceInput source="text_resume_template_id" reference="text_templates"
                    filter={{ "client_id": my_client_id_value }}>
                    <AutocompleteInput optionText="name" />
</ReferenceInput>

在我编辑的记录中,我有“ client_id”字段,我想要的是从该字段获取值,并将其放入 my_client_id_value 中。

我知道,如果我想使用要编辑的记录的ID,我会使用:

export const ReportsEdit = () => {
const { id } = useParams()
return (...

我对Admin版本3.xx进行反应,我想我会使用类似的内容:(使用道具)

    export const ClientsShow = props => {

    return (
        <Edit {...props}>
            <ReferenceInput source="text_resume_template_id" reference="text_templates"
                        filter={{ "client_id": props.client_id }}>
                 <AutocompleteInput optionText="name" />
            </ReferenceInput>

如何从中获取值client_id我的记录并将其用作过滤器?

I want to use the value of a field of my record to filter some autocomplete options, but I don't know how to get the value to use it.

<ReferenceInput source="text_resume_template_id" reference="text_templates"
                    filter={{ "client_id": my_client_id_value }}>
                    <AutocompleteInput optionText="name" />
</ReferenceInput>

On the record I'm editing, I have the "client_id" field, what I want is to get the value from this field and put it in my_client_id_value .

I know that if I wanted to use the id of the record I'm editing, I would use:

export const ReportsEdit = () => {
const { id } = useParams()
return (...

I React-Admin version 3.xx I think I would use something like: (using props)

    export const ClientsShow = props => {

    return (
        <Edit {...props}>
            <ReferenceInput source="text_resume_template_id" reference="text_templates"
                        filter={{ "client_id": props.client_id }}>
                 <AutocompleteInput optionText="name" />
            </ReferenceInput>

How can I get the value client_id from my record and use it as a filter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天的雪花 2025-02-18 04:48:49

我在 usercontextprovider ,但我无法使其起作用。

我使用的解决方案

import {Edit, ReferenceInput, AutocompleteInput, 
    useGetOne, useRedirect, RecordContextProvider } from 'react-admin';

export const ReportsEdit = () => {
    const { id } = useParams();
    const redirect = useRedirect();
    const { data, isLoading } = useGetOne(
        'reports',
        { id },
        { onError: () => redirect('/reports') }
    );
    if (isLoading) { return <div />; }

    return (
        <RecordContextProvider value={data}>   
     <Edit>
         <ReferenceInput flex={1} source="text_resume_template_id" reference="text_templates" 
          filter={{ "client_id": data.client_config._id }} >
             <AutocompleteInput optionText="name" /></ReferenceInput>

</Edit></RecordContextProvider>

是有效的,但是我想知道是否存在其他选项,而无需 Record -ContextProvider (因为 Edit> Edit 已经可以访问记录数据)。
如果有人有其他答案,那会很好。

更新:

另一个选择:
使用FormData从字段中获取所选值(无需从记录中获取,可用于创建视图和更灵活的编辑)。
优点是,如果更改字段值,则将更新过滤器(仅在记录更改时,上一个选项才会更改)

import {
    Edit, ReferenceInput, AutocompleteInput, FormDataConsumer
} from 'react-admin';

export const ReportsEdit = () => {
    return (
        <Edit>
            <ReferenceInput source="client_id" reference="clients">
                <AutocompleteInput optionText="client_name" isRequired />
            </ReferenceInput>
            <FormDataConsumer>
                {({ formData }) =>
                    <ReferenceInput source="text_resume_template_id" reference="text_templates"
                        filter={{ "client_id": formData.client_id }} >
                        <AutocompleteInput optionText="name" /></ReferenceInput>
                }
            </FormDataConsumer>

        </Edit>

I found my answer in the Show View documentation and I was not expecting to find it there because I'm working in the Edit.
I tried the UserContextProvider I found in other parts, but I couldn't make it work.

The solution I used:

import {Edit, ReferenceInput, AutocompleteInput, 
    useGetOne, useRedirect, RecordContextProvider } from 'react-admin';

export const ReportsEdit = () => {
    const { id } = useParams();
    const redirect = useRedirect();
    const { data, isLoading } = useGetOne(
        'reports',
        { id },
        { onError: () => redirect('/reports') }
    );
    if (isLoading) { return <div />; }

    return (
        <RecordContextProvider value={data}>   
     <Edit>
         <ReferenceInput flex={1} source="text_resume_template_id" reference="text_templates" 
          filter={{ "client_id": data.client_config._id }} >
             <AutocompleteInput optionText="name" /></ReferenceInput>

</Edit></RecordContextProvider>

It worked, but I wonder if other option exists, without the need of the RecordContextProvider (because the Edit already have access to the record data).
If anyone have other answer, would be nice.

UPDATE:

Another option:
Using formData to get the selected values from a field (no need to get from record, useful to Create views and more flexible for Edit).
The advantage is that if you change the field value, the filter will be update (the previous option would only change if the record changed)

import {
    Edit, ReferenceInput, AutocompleteInput, FormDataConsumer
} from 'react-admin';

export const ReportsEdit = () => {
    return (
        <Edit>
            <ReferenceInput source="client_id" reference="clients">
                <AutocompleteInput optionText="client_name" isRequired />
            </ReferenceInput>
            <FormDataConsumer>
                {({ formData }) =>
                    <ReferenceInput source="text_resume_template_id" reference="text_templates"
                        filter={{ "client_id": formData.client_id }} >
                        <AutocompleteInput optionText="name" /></ReferenceInput>
                }
            </FormDataConsumer>

        </Edit>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文