简体   繁体   English

react-admin中有两个字段的自动完成输入?

[英]Autocomplete input with two fields in react-admin?

I am creating a site using react-admin and I used the following code to create an AutocompleteInput with both id and description so you can search both and it will filter the options.我正在使用 react-admin 创建一个站点,我使用以下代码创建了一个带有 id 和 description 的 AutocompleteInput,这样您就可以同时搜索它们,它会过滤选项。

const choices = [
        { number: 102, description: 'Furniture' },
        { number: 103, description: 'Rugs' },
        { number: 104, description: 'Plants' },
    ];    
    const optionRenderer = choice => `${choice.number} ${choice.description}`;
        export const ActionsCreate = (props) => (
            <Create title=" " {...props}>
                <SimpleForm>
                    <AutocompleteInput source="casenumber" label="Juttu" choices={choices} optionText={optionRenderer} optionValue="number"/>
                    <TextInput source="billed" label="Tila" />
                    <TextInput source="handler" label="Tekijä" />
                </SimpleForm>
            </Create>
        );

This code generates the following:此代码生成以下内容: 在此处输入图像描述 I'm trying now to generate the same but the choices are not static.我现在正在尝试生成相同的内容,但选择不是 static。 Choices reference to another table in my database and the "number" is the number that will link the entry Im creating with that "choices" table.选择引用我数据库中的另一个表,“数字”是将我创建的条目与该“选择”表链接的数字。 Usually people use a ReferenceInput and an AutocompleteInput but I think I need to make a custom field and fetching the table with useDataProvider() to achieve this, so I tried the following:通常人们使用 ReferenceInput 和 AutocompleteInput 但我认为我需要创建一个自定义字段并使用 useDataProvider() 获取表来实现这一点,所以我尝试了以下方法:

const CasesAutocompleteInput = () => {
    const dataProvider = useDataProvider();
    let choices = [];
    const res =  dataProvider.getList('cases', {
        pagination: { page: 1, perPage: 500 },
        sort: { field: 'number', order: 'ASC' },
        filter: { },
    }).then(response => choices = response.data);

    const optionRenderer = choice => `${choice.number} ${choice.description}`;

    return(
        <AutocompleteInput source="casenumber" label="Juttu" choices={choices} optionText={optionRenderer} optionValue="number"/>
    );
};

After That I use my component as follows:之后我使用我的组件如下:

export const ActionsCreate = (props) => (
<Create title=" " {...props}>
    <SimpleForm>
        <CasesAutocompleteInput />
        <TextInput source="billed" label="Tila" />
        <TextInput source="handler" label="Tekijä" />

    </SimpleForm>
</Create>

But when doing this, the component AutocompleteInput is rendered before the data is assigned, so data doesnt get to my component.但是这样做时,组件 AutocompleteInput 在分配数据之前呈现,因此数据不会到达我的组件。 Is there a way for it to wait until dataProvider.getList is ready before sending the return?有没有办法让它等到 dataProvider.getList 准备好后再发送返回? I tried with wait/async but the problem is then that the result is a promise and not an object.我尝试使用等待/异步,但问题是结果是 promise 而不是 object。 Im sure Im close to the solution but have been stuck in this for a while.我确定我接近解决方案,但已经被困了一段时间。

Take a look at UseEffect hook example: Querying the API , provided in React-Admin documentation.看一下UseEffect钩子示例:查询 API ,在 React-Admin 文档中提供。

I've just tried your example like this:我刚刚试过你的例子是这样的:

const ActivityCreate = (props) => {
  const dataProvider = useDataProvider();
  const [projects, setProjects] = useState();
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState();
  useEffect(() => {
    dataProvider
      .getList("projects", {
        pagination: { page: 1, perPage: 100 },
        sort: { field: "id", order: "ASC" },
        filter: {},
      })
      .then(({ data }) => {
        setProjects(data);
        setLoading(false);
      })
      .catch((error) => {
        setError(error);
        setLoading(false);
      });
  }, []);

  if (loading) return <Loading />;
  if (error) return <Error />;
  if (!projects) return null;

  const project_choices = projects.map((project) => ({
    id: project.id,
    name: project.name,
  }));

  return (
    <Create {...props}>
      <SimpleForm>
        <SelectInput source="project" choices={project_choices} />
      </SimpleForm>
    </Create>
  );
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM