kkm*_*lee 4 reactjs material-ui
我在我的 React 项目之一中使用 Material-UI 的 Select 组件。我需要按组显示下拉数据,因此我使用的是<MenuItem>环绕<ListSubheader>. 我很难获得我的MenuItems的价值。如果我的代码有任何明显错误,请告诉我。
<FormControl>
<InputLabel>Product type</InputLabel>
<Select
id="product-type"
input={<Input id="grouped-select" />}
value={this.state.productType}
autoWidth={true}
style={{ width: 200 }}
onChange={(e, child) => {
console.log(e.target.value); // undefined!
}}
>
{this.state.productList.map((p, i) => {
const list = p[1];
let items = list.map((e, j) => {
return (
<MenuItem key={j} value={e.name}>
{e.name}
</MenuItem>
);
});
return (
<div>
<ListSubheader key={i}>{p[0]}</ListSubheader>
{items}
</div>
);
})}
</Select>
</FormControl>
Run Code Online (Sandbox Code Playgroud)
e.target.value正如其他人所提到的,返回 undefined的原因是因为它MenuItem不是Select. 由于productList是动态设置到下拉列表中,因此必须以这种方式呈现:
ListSubheader0
Item0
Item1
ListSubheader1
Item2
Item3
Item4
...
...
Run Code Online (Sandbox Code Playgroud)
我没有将 myListSubheader和包装MenuItem在一个无法div读取 的标签中target.value,而是返回了一个数组。
ListSubheader0
Item0
Item1
ListSubheader1
Item2
Item3
Item4
...
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2456 次 |
| 最近记录: |