如何使用react-bootstrap创建动态下拉列表

Joh*_*hnL 14 javascript reactjs react-bootstrap

我是全新的反应,所以我可能错过了一些基本的东西.react-bootstrap站点React-Bootstrap中的示例代码显示以下内容.我需要使用数组驱动选项,但我很难找到将编译的示例.

<Input type="select" label="Multiple Select" multiple>
  <option value="select">select (multiple)</option>
  <option value="other">...</option>
</Input>
Run Code Online (Sandbox Code Playgroud)

The*_*heo 28

您可以从这两个功能开始.第一个将根据传递给页面的props动态创建选择选项.如果它们映射到状态,则select将重新创建自身.

 createSelectItems() {
     let items = [];         
     for (let i = 0; i <= this.props.maxValue; i++) {             
          items.push(<option key={i} value={i}>{i}</option>);   
          //here I will be creating my options dynamically based on
          //what props are currently passed to the parent component
     }
     return items;
 }  

onDropdownSelected(e) {
    console.log("THE VAL", e.target.value);
    //here you will see the current selected value of the select input
}
Run Code Online (Sandbox Code Playgroud)

然后你将在渲染中拥有这个代码块.您将向onChange prop传递函数引用,并且每次调用onChange时,所选对象将自动与该函数绑定.而不是手动编写选项,您只需调用createSelectItems()函数,该函数将根据某些约束(可以更改)构建并返回您的选项.

  <Input type="select" onChange={this.onDropdownSelected} label="Multiple Select" multiple>
       {this.createSelectItems()}
  </Input>
Run Code Online (Sandbox Code Playgroud)


dev*_*ria 11

我的工作榜样

this.countryData = [
    { value: 'USA', name: 'USA' },
    { value: 'CANADA', name: 'CANADA' }            
];

<select name="country" value={this.state.data.country}>
    {this.countryData.map((e, key) => {
        return <option key={key} value={e.value}>{e.name}</option>;
    })}
</select>
Run Code Online (Sandbox Code Playgroud)

  • 我认为省略`key`(我认为是数组索引)会更正确,而是设置`&lt;option key={e.value}&gt;`。键应该是组件的 id 引用,如果说列表被重新排序,依赖数组索引可能会导致不一致。 (2认同)

小智 6

使用箭头函数绑定动态放置。

class BindDropDown extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      values: [
        { name: 'One', id: 1 },
        { name: 'Two', id: 2 },
        { name: 'Three', id: 3 },
        { name: 'four', id: 4 }
      ]
    };
  }
  render() {
    let optionTemplate = this.state.values.map(v => (
      <option value={v.id}>{v.name}</option>
    ));

    return (
      <label>
        Pick your favorite Number:
        <select value={this.state.value} onChange={this.handleChange}>
          {optionTemplate}
        </select>
      </label>
    );
  }
}

ReactDOM.render(
  <BindDropDown />,
  document.getElementById('root')
);
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root">
    <!-- This element's contents will be replaced with your component. -->
</div>
Run Code Online (Sandbox Code Playgroud)


Vig*_*ran 6

// on component load, load this list of values
// or we can get this details from api call also    
const animalsList = [
{
    id: 1,
    value: 'Tiger'
}, {
    id: 2,
    value: 'Lion'
}, {
    id: 3,
    value: 'Dog'
}, {
    id: 4,
    value: 'Cat'
}
];

// generage select dropdown option list dynamically
function Options({ options }) {
    return (
        options.map(option => 
                    <option key={option.id} value={option.value}>                                   
                    {option.value}
                    </option>)
                   );
}

<select
name="animal"
className="form-control">
<Options options={animalsList} />
</select>
Run Code Online (Sandbox Code Playgroud)