[英]Select doesn't show options
I'm trying to load the options of a select dynamically so I create an array of options which is passed to the select.我正在尝试动态加载选择的选项,因此我创建了一个传递给选择的选项数组。
The code of the constructor, handle and get data is next:构造函数、处理和获取数据的代码如下:
constructor(){
super();
this.state = {
loaded: false,
selected_season: null,
seasons: {},
competitions: {},
gameday: {}
}
this.handleChange = this.handleChange.bind(this);
}
handleChange = selected_season => {
this.setState({ selected_season });
console.log(`Option selected:`, selected_season);
};
async getData(){
let params = [{"###id_league###": 1}];
let seasons = await getDataFromServer(URL_FEB_API, HOMELF1_SEASON, params);
let gameday = await getDataFromServer(URL_FEB_API, RESULT_GAMEDAY_LF1, params);
this.setState({
selected_season: seasons["data"]["seasons"][0]["id"],
seasons: seasons["data"]["seasons"],
gameday: gameday,
loaded: true
});
}
componentDidMount(){
this.getData();
}
And the render function is:而渲染函数是:
render(){
let gameday = this.state.loaded ? this.state.gameday["data"]["last_game_day"][0] : [];
let season_options = () => {
let items = this.state.seasons.map(season => {
return{
value: season.id,
label: season.description
}
});
items.map(item => {
console.log(item);
});
return items;
}
return(
<div style = {{marginTop: 15 + 'px'}}>
{
(this.state.loaded) ?
<Form.Row>
<Form.Label column md = "1">Temporada</Form.Label>
<Col md = "3">
<Form.Control as="select"
controlid = "season"
options = {season_options()}
value = {this.state.selected_season}
onChange = {this.handleChange}
/>
</Col>
<Form.Label column md = "1">Competición</Form.Label>
<Col md = "3">
<Form.Control as="select" controlid = "season">
<option>Selecciona Competición</option>
<option>...</option>
</Form.Control>
</Col>
<Form.Label column md = "1">Jornada</Form.Label>
<Col md = "3">
<Form.Control as="select" controlid = "season">
<option>Selecciona Jornada</option>
<option>...</option>
</Form.Control>
</Col>
</Form.Row>
:
"" //If data is not loaded -> Don't do anything!!!
}
);
Before to return the array items, I write the content in console to check the content and I've got this:在返回数组项之前,我在控制台中写入内容以检查内容,我得到了这个:
So, the array is not empty.所以,数组不为空。 But in the view I don't load anything in the select.但是在视图中我没有在选择中加载任何东西。
The, what am I doing wrong?的,我做错了什么? Where is my mistake?我的错误在哪里? I don't understand why my select is not loaded with the conten of the array returned by the function season_options();我不明白为什么我的选择没有加载函数 season_options(); 返回的数组的内容;
Edit I:编辑我:
I have modified my initial code so insted of return an array of json objects I return an array of strings with the code of each option.我已经修改了我的初始代码,因此为了返回一个 json 对象数组,我返回了一个带有每个选项代码的字符串数组。
Now, my code in render method is like that:现在,我在 render 方法中的代码是这样的:
render(){
let gameday = this.state.loaded ? this.state.gameday["data"]["last_game_day"][0] : [];
const season_options = () => {
let items = this.state.seasons.map(season => {
return("<option value = " + season.id + ">" + season.description + "</option>");
});
items.map(item => {
console.log(item);
});
return items;
};
return(
<div style = {{marginTop: 15 + 'px'}}>
{
(this.state.loaded) ?
<Form.Row>
<Form.Label column md = "1">Temporada</Form.Label>
<Col md = "3">
<Form.Control as="select"
controlid = "season"
value = {this.state.selected_season}
onChange = {this.handleChange}
>
{season_options()}
</Form.Control>
</Col>
</Form.Row>
:
"" //If data is not loaded -> Don't do anything!!!
}
</div>
)}
And, I check the code of the page I've got this:而且,我检查了我有这个页面的代码:
All the options appears inside the select, but in the select there is nothing!!!所有选项都出现在选择中,但在选择中什么都没有!!!
What's happend?发生了什么? Why the options are not loaded in the select?为什么选择中没有加载选项? :S :S
Edit II (Solution):编辑二(解决方案):
Finally, following the advice of BearCoder this is the code of render method which works correctly:最后,按照 BearCoder 的建议,这是正确运行的 render 方法的代码:
render(){
let gameday = this.state.loaded ? this.state.gameday["data"]["last_game_day"][0] : [];
const season_options = () => {
let items = this.state.seasons.map(season => {
return(<option key = {season.id} value = {season.id}>{season.description}</option>);
});
return items;
};
const Header = () =>
<h4 style={{ borderRadius: '0.25em', textAlign: 'center', color: '#FFFFFF', backgroundColor: '#091e36', padding: '0.5em' }}>
Liga DIA / {gameday.name} / Jornada {gameday.num_jornada}
</h4>
return(
<div style = {{marginTop: 15 + 'px'}}>
{
(this.state.loaded) ?
<Form.Row>
<Form.Label column md = "1">Temporada</Form.Label>
<Col md = "3">
<Form.Control as="select"
controlid = "season"
value = {this.state.selected_season}
onChange = {this.handleChange}
>
{season_options()}
</Form.Control>
</Col>
</Form.Row>
:
"" //If data is not loaded -> Don't do anything!!!
}
</div>
)
}
Now, how you can see in this screep cap the select is loaded correctly with the data:现在,您如何在这个 screep cap 中看到选择已正确加载数据:
The error was to return the options like a string.错误是像字符串一样返回选项。 These options have to been returned without quotes: *return(<option key = {season.id} value = {season.id}>{season.description}</option>)*
这些选项必须不带引号返回: *return(<option key = {season.id} value = {season.id}>{season.description}</option>)*
Therefore, you've got an array not of strings but *<option key = x value = x>description</option>*
因此,您有一个不是字符串的数组,而是*<option key = x value = x>description</option>*
I am not sure if options can be passed as prop.我不确定选项是否可以作为道具传递。 Try to create <option>
react elements and use them as children尝试创建<option>
反应元素并将它们用作子元素
据我所知,您必须在渲染中包含 return 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.