[英]Select doesn't show options
我正在尝试动态加载选择的选项,因此我创建了一个传递给选择的选项数组。
构造函数、处理和获取数据的代码如下:
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();
}
而渲染函数是:
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!!!
}
);
在返回数组项之前,我在控制台中写入内容以检查内容,我得到了这个:
所以,数组不为空。 但是在视图中我没有在选择中加载任何东西。
的,我做错了什么? 我的错误在哪里? 我不明白为什么我的选择没有加载函数 season_options(); 返回的数组的内容;
编辑我:
我已经修改了我的初始代码,因此为了返回一个 json 对象数组,我返回了一个带有每个选项代码的字符串数组。
现在,我在 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>
)}
而且,我检查了我有这个页面的代码:
所有选项都出现在选择中,但在选择中什么都没有!!!
发生了什么? 为什么选择中没有加载选项? :S
编辑二(解决方案):
最后,按照 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>
)
}
现在,您如何在这个 screep cap 中看到选择已正确加载数据:
错误是像字符串一样返回选项。 这些选项必须不带引号返回: *return(<option key = {season.id} value = {season.id}>{season.description}</option>)*
因此,您有一个不是字符串的数组,而是*<option key = x value = x>description</option>*
我不确定选项是否可以作为道具传递。 尝试创建<option>
反应元素并将它们用作子元素
据我所知,您必须在渲染中包含 return 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.