2017-04-20 61 views
0

我正在研究antd选择和正确预填充问题,我可以通过它的initialValue字段装饰器预填充选择框,但它填充字符串,似乎没有办法有一个值(我可以解决但不理想),更重要的是,如果该选项未被选中/删除,它将不再在选择中可用,与标准选项不同。我可以包含选择列表选项和初始值(如下面的代码所示),但它允许重复自动输入,并在下拉列表中出现两次。任何想法我做错了什么?预选的antd prepopulate标签和多选

水面浮油和全部选项列表

let defaultSelect = []; 
    let selectList = []; 
    for (var a = 0; a < this.props.myData.length; a++) { 
     //push all options to select list 
     selectList.push(<Select.Option key={this.props.myData[i].id} >{this.props.myData[i].name}</Select.Option>) 
     //this is my code to pre-populate options, by performing a find/match 
     let matchedTech = _.find(this.props.myDataPrepopulate, { id: this.props.myData[i].id }); 
     if (matchedTech) { 
      //notice I can push just the string name, not the name and the id value. 
      defaultSelect.push(this.props.myData[i].name); 
     } 
    } 

选择代码

{getFieldDecorator(row.name, { 
     initialValue: defaultSelect 
    })(
    <Select 
     tags 
     notFoundContent='none found' 
     filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0} 
     {selectList} 
    </Select> 
    )} 
+0

您发布的代码中有许多语法错误:我未定义,<选择标签未关闭等。 –

回答

0

我想我明白你的问题,即使你贴不运行的代码。

<Select.Option>与普通html <select><option/></select>一样工作。为了给该选项一个值,它需要一个value属性,该属性用于标识该选项。

查看http://codepen.io/JesperWe/pen/YVqBor的工作示例。

转化为您的示例中的关键部分将变成:

this.props.myData.forEach(data => { 
    selectList.push(<Select.Option value={data.id} key={data.id}>{data.name}</Select.Option>); 
}); 

defaultSelect = this.props.myDataPrepopulate.map(data => data.id); 

(我冒昧地使用一些更现代的代码模式比原来的)