Material Ui 5 自动完成不返回过滤选项

发布于 2025-01-14 15:11:10 字数 1826 浏览 3 评论 0原文

我使用的是material ui V5,

由于自动完成中的默认过滤没有给出正确的结果数组,我编写了自己的filterOptions函数。

const filterOpt = (options, state) => {
 let result = options.filter(option => option.name.includes(state.inputValue))
 return result }   

从函数返回的结果正是我想要的。但我仍然可以看到不需要的选项。

这是我的自动完成组件:

<StyledAutocomplete
    disabled={disabled}
    id="field1"
    getOptionLabel={(option) => option.name || ""}
    isOptionEqualToValue={(option, value) => option.id === value.id}
    value={values[prop] || ""}
    noOptionsText={"No options found"}        
    options={data}
    style={{ width: "100%" }}
    PopperComponent={PopperMy}
    PaperComponent={CustomPaper}
    onChange={(event, newValue) =>
      setValues({ ...values, [prop]: newValue })
    }
    filterOptions={(options, state) => filterOpt(options, state)}
    renderInput={(params) => {
      const inputProps = params.inputProps;
      inputProps.autoComplete = "new-password";
      return (
        <StyledTextField
          {...params}
          inputProps={{
            ...params.inputProps,
            autoComplete: "new-password",
          }}
          name="field1"
          id="field1"
          autoComplete="off"
          type="text"
          label=""
          variant="outlined"
          error={error && !values[prop]}
          helperText={error && errorStatus ? errorTexts[prop] : ""}
        />
      );
    }}
  />

以下是过滤后我看到的选项在此处输入图像描述

以下是从函数返回的结果数组:在此处输入图像描述

是否有任何解决方案可以显示精确的过滤数组用户?

I am using material ui V5,

Due to the default filtering in Autocomplete does not give proper result array, l have written my own filterOptions function.

const filterOpt = (options, state) => {
 let result = options.filter(option => option.name.includes(state.inputValue))
 return result }   

The result returning from the function is exactly what l want. But still, l can see the undesired options.

Here is my Autocomplete component :

<StyledAutocomplete
    disabled={disabled}
    id="field1"
    getOptionLabel={(option) => option.name || ""}
    isOptionEqualToValue={(option, value) => option.id === value.id}
    value={values[prop] || ""}
    noOptionsText={"No options found"}        
    options={data}
    style={{ width: "100%" }}
    PopperComponent={PopperMy}
    PaperComponent={CustomPaper}
    onChange={(event, newValue) =>
      setValues({ ...values, [prop]: newValue })
    }
    filterOptions={(options, state) => filterOpt(options, state)}
    renderInput={(params) => {
      const inputProps = params.inputProps;
      inputProps.autoComplete = "new-password";
      return (
        <StyledTextField
          {...params}
          inputProps={{
            ...params.inputProps,
            autoComplete: "new-password",
          }}
          name="field1"
          id="field1"
          autoComplete="off"
          type="text"
          label=""
          variant="outlined"
          error={error && !values[prop]}
          helperText={error && errorStatus ? errorTexts[prop] : ""}
        />
      );
    }}
  />

Here are the options that l see after filteringenter image description here

Here is the results array returned from the function:enter image description here

Is there any solution to show the exact filtered array to users?

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

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

发布评论

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

评论(1

倾其所爱 2025-01-21 15:11:10

这是因为我的数据数组包含一些具有相同键的项目。

This is because of my data array which includes some items with the same key.

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