如何在选择框中使用React JS进行搜索?

发布于 2025-01-28 03:19:15 字数 1691 浏览 5 评论 0 原文

我正在借助 antd Design

当前场景

- >我在这里制作了两个选择框,其中一个选择具有静态数据,另一个来自API。

问题

- >当我键入关键字以选择选项时,静态一个可行,但是动态选项(来自API的数据)不起作用并显示无数据文本。

期望

静态和动态选择框都需要按照选择框中的搜索关键字显示选项。

代码:(选项为静态)

  <Select mode="multiple" style={{ width: 120 }}>
    <Select.Option value="jack">Jack</Select.Option>
    <Select.Option value="lucy">Lucy</Select.Option>
    <Select.Option value="disabled" disabled>
      Disabled
    </Select.Option>
    <Select.Option value="Yiminghe">yiminghe</Select.Option>
  </Select>

”输入映像在此处“

代码:(来自API的选项)

  <Select mode="multiple" style={{ width: 120 }}>
    {data.map(({ label, value, text }) =>
      label ? (
        <Select.Option value={value || ""} key={label}>
          {text}
        </Select.Option>
      ) : null
    )}
  </Select>

“在此处输入图像说明”

工作示例:

I am making a select box with the help of Antd design.

Current scenario:

-> I am making two select box here, in which one select has static data and another from api.

Issue:

-> While I type keyword to select options, static one works but the dynamic options (data from api) doesn't works and display the No data text.

Expectation:

Both the static and dynamic select box needs to display the options as per the search keyword in the select box.

Code: (Options as static)

  <Select mode="multiple" style={{ width: 120 }}>
    <Select.Option value="jack">Jack</Select.Option>
    <Select.Option value="lucy">Lucy</Select.Option>
    <Select.Option value="disabled" disabled>
      Disabled
    </Select.Option>
    <Select.Option value="Yiminghe">yiminghe</Select.Option>
  </Select>

enter image description here

Code: (Options from api)

  <Select mode="multiple" style={{ width: 120 }}>
    {data.map(({ label, value, text }) =>
      label ? (
        <Select.Option value={value || ""} key={label}>
          {text}
        </Select.Option>
      ) : null
    )}
  </Select>

enter image description here

Working Example:

Edit antd-select-clear (forked)

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

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

发布评论

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

评论(2

也只是曾经 2025-02-04 03:19:15

您已经混合了标签和值值。 标签是您要输入并尝试匹配的名称值。

useEffect(() => {
  fetch("https://api.github.com/users")
    .then((res) => res.json())
    .then((data) => {
      const userData = data.map((item) => ({
        label: item.login, // <-- input values you are matching
        value: item.id
      }));
      setData(userData);
    });
}, []);

...

<Select mode="multiple" style={{ width: 120 }}>
  {data.map(({ label, value, text }) => (
    <Select.Option value={label} key={value}> // <-- label is option value
      {label}
    </Select.Option>
  ))}
</Select>

”

You've mixed up the label and value values. The label is the name value that you are typing in and trying to match.

useEffect(() => {
  fetch("https://api.github.com/users")
    .then((res) => res.json())
    .then((data) => {
      const userData = data.map((item) => ({
        label: item.login, // <-- input values you are matching
        value: item.id
      }));
      setData(userData);
    });
}, []);

...

<Select mode="multiple" style={{ width: 120 }}>
  {data.map(({ label, value, text }) => (
    <Select.Option value={label} key={value}> // <-- label is option value
      {label}
    </Select.Option>
  ))}
</Select>

Edit how-to-make-search-in-select-box-using-react-js

笑着哭最痛 2025-02-04 03:19:15

当价值与选项文本一样,它起作用。可能是Antd的特殊性

  <Select mode="multiple" style={{ width: 120 }}>
    {data.map(({ label, value, text }) =>
      label ? (
        <Select.Option value={label} key={label}>
          {text}
        </Select.Option>
      ) : null
    )}
  </Select>

It works when value is as same as option text. Probably antd's peculiarity

  <Select mode="multiple" style={{ width: 120 }}>
    {data.map(({ label, value, text }) =>
      label ? (
        <Select.Option value={label} key={label}>
          {text}
        </Select.Option>
      ) : null
    )}
  </Select>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文