javascript - Antd Select value label - Stack Overflow

I have my state with only id's to load the selectComponentDidMount methodlet countries_data = a

I have my state with only id's to load the select

//ComponentDidMount method
let countries_data = await axios.get('/api/v1/countries/');
countries_data.data.map((data) => {
  countries.push(<Option key={data.id}>{data.name}</Option>);
});
//My Options are a Option Component

//Render method
<Select 
 showSearch
 placeholder={'Country'}
 size={"large"}
 value={this.state.country_id} //Should show the text from option selected
 onChange={this.onChangeCountry}
 filterOption={(input, option) =>
     option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}>
     {countries}
 </Select>

The main problem here is I set the value option with a integer the Select will show the number, like 27, 40 but no the country label from the option like Country A, Country B, etc. My problem here is how to set a value setting the value as integer and show me the Option selected from my country array?

I have my state with only id's to load the select

//ComponentDidMount method
let countries_data = await axios.get('/api/v1/countries/');
countries_data.data.map((data) => {
  countries.push(<Option key={data.id}>{data.name}</Option>);
});
//My Options are a Option Component

//Render method
<Select 
 showSearch
 placeholder={'Country'}
 size={"large"}
 value={this.state.country_id} //Should show the text from option selected
 onChange={this.onChangeCountry}
 filterOption={(input, option) =>
     option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}>
     {countries}
 </Select>

The main problem here is I set the value option with a integer the Select will show the number, like 27, 40 but no the country label from the option like Country A, Country B, etc. My problem here is how to set a value setting the value as integer and show me the Option selected from my country array?

Share Improve this question asked Sep 5, 2019 at 21:35 drkpkgdrkpkg 731 gold badge1 silver badge10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 0

You need to add the value attribute to your <Option />

countries_data.data.map((data) => {
  countries.push(<Option key={data.id} value={data.id}> {data.name} </Option>);
//                                     ^^^^^^^^^^^^^^^
});

Refer to Select.Option API, the property title sets the title of Select after selecting this option.

Moreover, why would you use a map without a return value? Use forEach for this case.

countries_data.data.forEach((data) => {
  countries.push(<Option key={data.id} value={data.name} title={data.name}>{data.name}</Option>);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745374560a4624943.html

相关推荐

  • javascript - Antd Select value label - Stack Overflow

    I have my state with only id's to load the selectComponentDidMount methodlet countries_data = a

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信