通过另一个参数自动完成搜索
在下面的示例中, -
https://codesandbox.io/s/g5ucdj?file= /demo.tsx
我想实现功能,以便如果我有数组 -
const top100Films = [
{ title: 'The Shawshank Redemption', year: 1994 },
{ title: 'The Godfather', year: 1972 },
{ title: 'The Godfather: Part II', year: 1994 },]
然后,在我通过年度搜索时,我应该获得标题的标签选项。 例如。如果我在搜索框中输入1994年,我应该获得两个选择的标题选项 -
The Shawshank Redemption
The Godfather: Part II
我尝试了更改选项 -
options={top100Films.map((option) => option.year)}
但是通过此,我将获得一年作为我想要作为选项的选项,将年份输入文本框中。
In below example , -
https://codesandbox.io/s/g5ucdj?file=/demo.tsx
I want to achieve functionality such that , If I have array -
const top100Films = [
{ title: 'The Shawshank Redemption', year: 1994 },
{ title: 'The Godfather', year: 1972 },
{ title: 'The Godfather: Part II', year: 1994 },]
Then , in autocomplete when I search via year , I should get label options for title.
Eg. If I enter year 1994 in search box , I should get two title options for selection -
The Shawshank Redemption
The Godfather: Part II
I tried changing option -
options={top100Films.map((option) => option.year)}
But through this I get year as a option which I want title as a option , having year entered in text box.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用mui
createFilterOptions
声明过滤器,
然后在您的自动完成标签中添加以下添加以下:
codesandbox 示例。
该代码在JavaScript中,您可以相应地更改为打字稿。对于打字稿,请按照此 link
You can use MUI
createFilterOptions
Declare filterOptions
Then in you Autocomplete tag add this:
Codesandbox example.
The code is in javascript, you can change to typescript accordingly. For typescript follow this link
当我看到您正在使用React JS时,
因此,我建议您使用React-Select
:反应选择NPM https://reaect-select.com/home
参考文档
As I see you are using React js,
so I will suggest you, to use React-select for better functionality
Reference documantion: React-select npm https://react-select.com/home
Everything is perfectly documented