材质UI选择字段多选 [英] Material UI Select Field multiselect

查看:40
本文介绍了材质UI选择字段多选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了几次文档中给出的示例.但这对我来说效果不佳.谁能帮我....这是代码

I tried several times the example given in the documentation. but it didn't work well for me. can any one help me.... this is the code

import React, {Component} from 'react';
import SelectField from 'material-ui/SelectField';
import MenuItem from 'material-ui/MenuItem';

const names = [
  'Oliver Hansen',
  'Van Henry',
  'April Tucker',
  'Ralph Hubbard',
  'Omar Alexander',
  'Carlos Abbott',
  'Miriam Wagner',
  'Bradley Wilkerson',
  'Virginia Andrews',
  'Kelly Snyder',
];

/**
 * `SelectField` can handle multiple selections. It is enabled with the `multiple` property.
 */
export default class SelectFieldExampleMultiSelect extends Component {
  state = {
    values: [],
  };

  handleChange = (event, index, values) => this.setState({values});

  menuItems(values) {
    return names.map((name) => (
      <MenuItem
        key={name}
        insetChildren={true}
        checked={values && values.indexOf(name) > -1}
        value={name}
        primaryText={name}
      />
    ));
  }

  render() {
    const {values} = this.state;
    return (
      <SelectField
        multiple={true}
        hintText="Select a name"
        value={values}
        onChange={this.handleChange}
      >
        {this.menuItems(values)}
      </SelectField>
    );
  }
}

http://www.material-ui.com/#/components/select-field

select属性起作用,但是它没有选择多个选项.当我检查states.value时,它仅包含一个值,而不是值的数组

the select property works but it doesnt select multiple options. when i check the states.value it only includes a single value not a array of values

推荐答案

该示例也不适用于我.要添加多选功能,您必须手动将新值添加到状态,因此示例中的handleChange函数将如下所示:

This example didn't work for me either. To add the multi-select feature you have to manually add the new value to the state, so the handleChange function from the example would look something like this:

  handleChange(event, index, values)  { 
    this.setState({ 
     values: [...this.state.values , values]
    });
   }

我将material-ui的版本更新为最新的稳定版本,并且他们的示例像个魅力

I updated my version of material-ui to the latest stable version and their example worked like a charm

这篇关于材质UI选择字段多选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆