简体   繁体   English

如何使用React从Material-UI中的SelectField获取ID?

[英]How to get id from SelectField in material-ui using react?

I have looked and tried the solutions from other questions, but I cannot seem to get it to work. 我已经查看并尝试了其他问题的解决方案,但似乎无法使它起作用。 The problem I am facing is that event.target.id and event.target.value do not work here. 我面临的问题是event.target.id和event.target.value在这里不起作用。 I have done some research and found out why. 我做了一些研究,找出了原因。 Now I just need a way to get the "id" from the selectfield 现在,我只需要一种从选择字段中获取“ id”的方法

How do I grab the id, in order to make this function work? 我如何获取ID以使此功能正常工作?

Here I have the select: 在这里,我可以选择:

          <SelectField
          id="category"
          floatingLabelText="Category"
          value={this.state.value}
          onChange={this.updateTask.bind(this)}
        >
          <MenuItem disabled={true} primaryText="Choose A Category" />
          <MenuItem value="delivery" primaryText="Delivery" />
          <MenuItem value="dog walking" primaryText="Dog Walking" />
          <MenuItem value="house cleaning" primaryText="House Cleaning" />
        </SelectField>

And here is the function: 这是函数:

 updateTask(event, index, value) {
    event.preventDefault();
    let updated = Object.assign({}, this.state.task);
    updated[event.target.id] = event.target.value;
    this.setState({
      task: updated,
      value: value,
    });
  }

Not sure entirely sure how Material-UI works, but you could do 不完全确定Material-UI的工作方式,但是您可以

onChange={(event, index, value) => this.updateTask(event, index, value, "id")}

And add the id paramaeter to updateTask 并将id参数添加到updateTask

updateTask(event, index, value, id){
   console.log(id) 
}

It seems like it's been reported as an issue on their github 好像已经在他们的github上报告了一个问题

For some reason someone implemented the ability to pass the name prop and not id prop. 由于某种原因, 有人实现了传递名称prop而不是id prop的功能。

I myself chose to rely on the name prop. 我本人选择依靠道具这个name but, there is also a suggestion provided in the link, which implies accessing the underlying element. 但是,链接中也提供了一条建议,暗示着访问基础元素。

Sadly, it's not possible to pass a JSON-Object as value to the above Selects "onChange" method, or - better said - it is possible, but then MaterialUI injects the value stored in the MenuItem (which is now a JSON Object) into the textField of the SelectMenu - so it displays nothing because you can't alter how the value should be displayed. 可悲的是,不可能将JSON对象作为值传递给上述Selects“ onChange”方法,或者-更好的说-可以,但是MaterialUI将存储在MenuItem(现在是JSON对象)中的值注入到SelectMenu的textField-因此它什么也不显示,因为您不能更改该值的显示方式。 So as of right now, as much as I know, it's only possible to pass one argument via event.target.value. 因此,就目前而言,据我所知,只能通过event.target.value传递一个参数。 If you need more arguments, like an index as an example, I did accomplish it like this: 如果您需要更多的参数(例如以索引为例),则可以这样完成:

In the "onChange" prop of the Select, accept two arguments: the first one is the event to extract the value from, the second one is child-object which was clicked, as is documented here: https://material-ui.com/api/select/ 在Select的“ onChange”道具中,接受两个参数:第一个是从中提取值的事件,第二个是被单击的子对象,如此处所述: https:// material-ui。 com / api / select /

Now you can access the value from event.target.value as well as access any unique property of the child (which you defined first) via child.props.[yourVariable] 现在,您可以访问event.target.value中的值,也可以通过child.props.[yourVariable]访问子级的任何唯一属性(首先定义) child.props.[yourVariable]

in the MenuItem's declaration; 在MenuItem的声明中; set your id as a prop for the menuItem 将您的ID设置为menuItem的道具

 {this.state.availableDays.map((day, i) => {
        return (
              <MenuItem id={i} value={day.key}>{day.key}</MenuItem>
               )
   })}

in the Select's "onChange" Method; 在Select的“ onChange”方法中; accept event and child as arguments; 接受事件和孩子作为参数; access child.props.[yourVariable] 访问child.props。[yourVariable]

onChange={(event, child) => {this.setState({currentDayString: event.target.value, currentDayIndex: child.props.id}) ,this.displayDataForThisDay() }}

I do realize this question was asked a year ago; 我确实知道这个问题是一年前提出的。 but maybe my answer is of help for anyone searching for an answer for this very question 但也许我的答案对寻找这个问题的答案的人有帮助

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

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