简体   繁体   English

所选值未显示在 Textfield Select - Material UI React 组件中

[英]Selected Value not showing in Textfield Select - Material UI React Component

I have TextField Select Material UI components based on a certain number of value in a variable.我有基于变量中一定数量的值的 TextField Select Material UI 组件。

{this.state.selectedNextHops.map((nextHop, index) => (
              <div>
                <TextField
                    select
                    className="vnfprofile-field"
                    InputProps={{ className: 'variable-value site-details-view-textfield' }}
                    InputLabelProps={{ shrink: true }}
                    SelectProps={{
                        MenuProps: {
                            className: 'vnf-designer-value',
                            getContentAnchorEl: null,
                            anchorOrigin: {
                                vertical: 'bottom',
                                horizontal: 'left',
                            }
                        },
                    }}
                    value = {this.state.selectedNextHops[index] || ''}
                    disabled={this.props.newPopoverPanelView === 'VIEW' ? true : false}
                    onChange={(e) => this.handleChange('nexthop', e)}
                  >
                    {this.state.remainingNextHops.length !== 0 ? this.state.remainingNextHops.map((option, i) => (
                        <MenuItem key ={i} value = {option || ''}>
                          {option}
                        </MenuItem>
                      )) :
                        <MenuItem value = {'No Data Available'}>
                            {'No Data Available'}
                        </MenuItem>}
                  </TextField>
                  <TextField
                    className="vnfprofile-field subnet-textfield"
                    InputProps={{ className: 'variable-value' }}
                    InputLabelProps={{ shrink: true }}
                    value = {'29'}
                  />
                </div>
                ))
          }

The TextFields show up sequentially when I select value from the previous dropdown and filters the menu based on previous selection.当我从上一个下拉列表中选择值并根据上一个选择过滤菜单时,TextField 会按顺序显示。

if(selectedNextHops.indexOf(event.target.value) === -1) {
            selectedNextHops.push(event.target.value);
        }
        remainingNextHops = this.props.nextHopSapds.filter(nextHop => selectedNextHops.indexOf(nextHop) === -1);

this.setState({
            selectedNextHops: selectedNextHops,
            remainingNextHops: remainingNextHops
        });

Update: Here is my handleChange Method ->更新:这是我的 handleChange 方法 ->

handleChange(type, event) {
    let selectedNextHops = JSON.parse(JSON.stringify(this.state.selectedNextHops));
    let remainingNextHops = [];
    if(type === 'nexthop') {
        selectedNextHops = selectedNextHops.filter(nh => nh !== '');
        isContentChanged = true;
        if(selectedNextHops.indexOf(event.target.value) === -1) {
            selectedNextHops.push(event.target.value);
        }
        remainingNextHops = this.props.nextHopSapds.filter(nextHop => selectedNextHops.indexOf(nextHop) === -1);
        if(remainingNextHops.length !== 0) {
            selectedNextHops.push('');
        }
        this.setState({
            selectedNextHops: selectedNextHops,
            remainingNextHops: remainingNextHops
        });
    }
  }

The state is updating fine, but the textfield does not display the selected value.状态正在更新,但文本字段未显示所选值。 I have tried everything I knew.我已经尝试了我所知道的一切。 Any help is appreciated.任何帮助表示赞赏。

So you try to access the key with e.target.value.id but the target object has only the value and not the id itself.因此,您尝试使用 e.target.value.id 访问键,但目标对象只有值而不是 id 本身。 That is why it is undefined after you call the handleChange method.这就是为什么在调用 handleChange 方法后它是未定义的。 There is a way to access the key though:不过,有一种方法可以访问密钥:

The callback does not only pass the event but also the child object as second parameter and this can be used to get the key like this:回调不仅传递事件,还传递子对象作为第二个参数,这可用于获取键,如下所示:

handleChangeTest = (event, child) => {
    this.setState({
      selectedID: child.key,
      visibleValue: event.target.value
    });
  };

This will set the key as selectedID and the value of the selected item as visibleValue.这会将键设置为 selectedID,并将所选项目的值设置为可见值。

This is hard to debug without seeing a working snippet or the state ( especially this.state.selectedNextHops ) , but based on the code sandbox provided ( in the comment ) , I assume it's the same problem, so this answer will apply to the sandbox code :如果没有看到工作片段或状态(尤其是this.state.selectedNextHops ),这很难调试,但基于提供的代码沙箱(在评论中),我认为这是同样的问题,所以这个答案将适用于沙箱代码 :

this.setState({
  selectedID: event.target.value.id,
  visibleValue: event.target.value.name
});

event.target.value.id and event.target.value.name are undefined , event.target.value.idevent.target.value.name undefined

console.log(console.log(event.target)) // {value: "S0002", name: undefined}

For the select to display a selected option , the value attribute for both need to match :对于select以显示选定option ,两者的value属性需要匹配:

<select value="2">
        ^^^^^^^^^
  <option value="1">first value</option>
  <option value="2">second value</option>
          ^^^^^^^^^ 
</select>

in the example in the code sandbox, the value of the Select is value={this.state.visibleValue} and the values of the options are value={x.label}在代码沙箱的例子中, Selectvalue={this.state.visibleValue} ,options的value={x.label}

Since this.state.visibleValue is always undefined , you'll never see the value of the select update.由于this.state.visibleValue始终是undefined ,您将永远不会看到select更新的值。

A quick fix for this is to change the handleChage function to :对此的快速解决方法是将handleChage函数更改为:

handleChangeTest = event => {
  this.setState({
    selectedID: event.target.id,
    visibleValue: event.target.value
  });
};

but that will leave selectedID undefined , to set it, add the attribute id={x.id} to the option and use event.currentTarget to get its value :但这将使selectedID未定义,要设置它,将属性id={x.id}option并使用event.currentTarget获取其值:

{this.state.data.map(x => (
  <MenuItem key={x.id} value={x.label} id={x.id}>
                                      ^^^^^^^^^
    {x.name}
  </MenuItem>
))}

And

handleChangeTest = event => {    
  this.setState({
    selectedID: event.currentTarget.id,
                ^^^^^^^^^^^^^^^^^^^^^^
    visibleValue: event.target.value
  });
};

Working SandBox工作沙盒

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

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