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

2024-02-07

我正在使用react-admin创建一个网站,并使用以下代码创建一个包含id和描述的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: enter image description here I'm trying now to generate the same but the choices are not 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:

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"/>
    );
};

之后我按如下方式使用我的组件:

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

    </SimpleForm>
</Create>

但是,执行此操作时,组件 AutocompleteInput 在分配数据之前呈现,因此数据不会到达我的组件。有没有办法让它等到 dataProvider.getList 准备好后再发送返回?我尝试使用 wait/async 但问题是结果是一个承诺而不是一个对象。我确信我已经接近解决方案,但已经被困在这个问题上一段时间了。


看一眼UseEffect钩子示例:查询API https://marmelab.com/react-admin/Actions.html,在 React-Admin 文档中提供。

我刚刚尝试过你的例子,如下所示:

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>
  );
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在react-admin中自动完成带有两个字段的输入? 的相关文章

随机推荐

  • 日期序列化后 Laravel 7 中的时区错误

    我正在开发一个新的 Laravel 7 1 应用程序 不是升级 但似乎使用日期序列化会失去时区 配置 应用程序 php timezone gt Europe Zurich 修补匠的例子 gt gt gt Carbon Carbon pars
  • 使用Python实现并集查找

    所以这就是我想要做的 我有一个包含几个等价关系的列表 l 1 2 2 3 4 5 6 7 1 7 我想合并共享一个元素的集合 这是一个示例实现 def union lis lis set e for e in lis res while T
  • Jackson JSON反序列化:每行中的数组元素

    我在用Jackson https github com FasterXML jackson core并且想要漂亮地打印 JSON 以便数组中的每个元素都进入每一行 例如 foo bar blah 1 2 3 Setting Serializ
  • Kubernetes:如何正确设置 php-fpm 和 nginx 共享卷权限

    我是 kubernetes 的新手 目前我正在尝试在 kubernetes 上部署 laravel 应用程序 我设置了 1 个部署 yaml 其中包含 2 个容器 nginx 和 php fpm 和一个共享卷 这是完整的 yaml apiV
  • 使用 C# .net 挂载其他用户 hive

    我正在编写一个应用程序 它将为每个选定的用户写入一些注册表项 我想知道是否有正确的方法来安装另一个用户的配置单元以在其中写入 目前 我正在使用 REG LOAD 来安装每个配置单元 它很实用 但很混乱 任何想法 预先感谢您的回答 Cheer
  • 如何在 OCR 中区分斜线零和八 (0->8)

    我正在为我的 Android 应用程序使用 ML Kit for Firebase ReCalc 收据计算器 https play google com store apps details id info trekto receipts
  • 消息总线中的总线发现

    我正试图了解消息总线和国际奥委会 但我的脑子里充满了问题 这就是我想到的场景 三台电脑通过局域网连接 无法访问互联网 这三台计算机各自运行一个服务 并自动自我发现其他计算机 换句话说 它们各自在公共总线上发送消息 这表明了他们自己的身份 从
  • 如何使用 Activator.CreateInstance 创建一个在运行时 T 未知的 List

    我在用着Activator CreateInstance通过类型变量创建对象 在运行时未知 static dynamic CreateFoo Type t gt Activator CreateInstance t 显然 我还没有正确理解如
  • 在构建期间调用 setState() 或 markNeedsBuild()。该覆盖小部件无法标记为需要构建

    我尝试在 AbsorbPointer 特别是 GestureDetector onPanEnd 调用的 fling 函数的 SetState 内使用 showGeneralDialog 编写动画对话框脚本 我附加了简单的代码 我尝试用未来的
  • std::atoll 与 VC++

    我一直在使用std atoll from cstdlib将字符串转换为int64 t与海湾合作委员会 该功能似乎在 Windows 工具链上不可用 使用 Visual Studio Express 2010 最好的选择是什么 我也有兴趣转换
  • 如何从 apt-get 下载软件包而不安装它? [关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 我有一台电脑 没有NIC http en wikipedia org wiki Network interface controller 我想通过 U
  • 如何简化这些冗余的 C++ 代码?

    现有两个类 一个是SrcField它返回具体类型值 另一个是联合DSTField 定义相应的数据类型 class SrcField public signed char GetInt8 unsigned char GetUInt8 shor
  • 使用 SQL 在有向图中查找循环

    已经有几个关于查找循环的问题 但我没有在 SQL 中找到解决方案 首选 MSSQL 这些表将是 Node NodeID INT 和 Edge EdgeID INT NodeID1 INT NodeID2 INT 在有向图中查找循环的性能良好
  • ggadjustedcurves survminer if (xi > xj) 1L else -1L 错误

    我正在尝试使用 survminer 通过 ggadjustedcurves 创建调整后的生存曲线 我的代码如下 adjustedcurve lt coxph Surv time DeathTxCensor deadORtx 1 strata
  • 修复协议 Ecto.Queryable 未实现错误

    我刚开始使用 Ecto 和 Elixir 并且遇到了一个无法解释的错误 我的代码看起来就像 Ecto 自述文件中的示例 这是我的 Ecto 模型和查询模块 defmodule Registration do use Ecto Model s
  • 警报对话框肯定按钮问题

    我在单击按钮时启动了警报对话框 它有用户名和密码 在警报对话框触发后 如果您按 确定 按钮 它会强制关闭并关闭应用程序 任何帮助将不胜感激 public class MainActivity extends Activity final C
  • 如何使用 SAX 解析器解析 XML

    我正在关注这个tutorial http www anddev org parsing xml from the net using the saxparser t353 html 它工作得很好 但我希望它返回一个包含所有字符串的数组 而不
  • 如何制作便携式 Jupyter 幻灯片

    如何使 Jupyter 幻灯片放映变得便携 我可以在本地提供幻灯片放映 但我无法将其发送给任何人并让它与所有图像 幻灯片动画功能等一起使用 我在用jupyter nbconver my notebook ipynb to slides并获取
  • IE 6 与位置:固定

    位置 已修复 不适用于 Internet Explorer 6 我无法真正理解在 google 上找到的修复程序 我需要它在 IE6 IE7 IE8 和 FireFox 3 0 中工作
  • 在react-admin中自动完成带有两个字段的输入?

    我正在使用react admin创建一个网站 并使用以下代码创建一个包含id和描述的AutocompleteInput 以便您可以搜索两者并且它会过滤选项 const choices number 102 description Furni