简体   繁体   English

在 Material-UI 自动完成中获取选定的值

[英]Getting selected value in Material-UI Autocomplete

Here's the link for the sandbox code: Problem这是沙盒代码的链接: 问题

Here's my problem: I can't retrieve the values in my autocomplete component just like my other components.这是我的问题:我无法像其他组件一样检索自动完成组件中的值。 If you click the Retrieve data button, it will retrieve the value for creator, title, description, project, severity, and status but the members field is empty.如果单击检索数据按钮,它将检索创建者、标题、描述、项目、严重性和状态的值,但成员字段为空。 I think the problem is at my Autocomplete component (line 95), am I doing it right?我认为问题出在我的自动完成组件(第 95 行)上,我做得对吗?

Note: I remade the code so that I can share it on codesandbox, in my original code I'm using redux (but I'm pretty sure that the problem is not there but in the Autocomplete component)注意:我重新制作了代码,以便可以在codesandbox上共享它,在我的原始代码中我使用的是redux(但我很确定问题不存在,而是在自动完成组件中)

Do it this way这样做

     <Autocomplete
        multiple
        style={{ margin: "10px" }}
        limitTags={1}
        options={sampleUsers}
        getOptionLabel={(option) => option.name}
        fullWidth
        onChange={(e, newMember) =>
          setBugData({ ...bugData, members: newMember })      //return new member
        }

it works codesandbox它可以工作

  1. Use object instead of string in retrievedBugData: {name: "Minnie Mouse"}使用 object 代替字符串 retrievedBugData: {name: "Minnie Mouse"}

const retrievedBugData = {
  creator: "Mickey Mouse",
  title: "A title",
  description: "A Description",
  project: "Project",
  members: [{name: "Minnie Mouse"}, {name: "Donald Duck"}],
  severity: "High",
  status: "Pending"
};

  1. Set value of autocomplete to: value={bugData.members}将自动完成的值设置为: value={bugData.members}

<Autocomplete
    multiple
    style={{ margin: "10px" }}
    limitTags={1}
    value={bugData.members}
    options={sampleUsers}
    getOptionLabel={(option) => option.name}
    fullWidth
    )}
/>

  1. Use object inside on change event https://codesandbox.io/s/brave-pare-4w60h?file=/src/App.js在更改事件https://codesandbox.io/s/brave-pare-4w60h?file=/src/App.js 中使用 object

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

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