我正在尝试将搜索功能的结果(用户表中的用户名)推入MenuItem下拉列表中。搜索功能可以正常工作并提取正确的结果,但是我无法弄清楚为什么结果中的用户名未显示在MenuItem中。
这是在当前用户列表中进行搜索并拉出那些符合我所需条件的函数,然后将这些结果推送到MenuItem:
private getActiveLsList = () => {
const opts = new Array<React.ReactElement<any>>();
const sr = new SearchRequest();
sr.addFilter("role", "LoanSpecialist");
sr.addFilter("ls_status", "Active");
searchUsers(this.props.app.api, sr).then((res: SearchResults<User>) => {
if (!res || !res.numHits || res.numHits < 1 || !res.results) {
opts.push(
<Select key="0" value="0">
"Error loading LS List"
</Select>
);
return opts;
}
res.results.forEach(n => {
opts.push(
<MenuItem key={n.id} value={n.id}>
<Typography> {n.userName} </Typography>
</MenuItem>
);
});
return opts;
});
};
在这里我调用getActiveLsList函数(在render内):
<TextField
select
fullWidth
variant="outlined"
value={this.state.assignedLS}
onChange={this.updateAssignment("assigned_loan_specialist", "assignedLS")}
margin="normal"
>
<MenuItem value="Unassigned">Unassigned</MenuItem>
{this.getActiveLsList()}
</TextField>
答案 0 :(得分:0)
我认为您至少需要重新组织一下:
state = {
activeLsList: null,
}
private getActiveLsList = () => {
const opts = new Array<React.ReactElement<any>>();
const sr = new SearchRequest();
sr.addFilter("role", "LoanSpecialist");
sr.addFilter("ls_status", "Active");
searchUsers(this.props.app.api, sr).then((res: SearchResults<User>) => {
if (!res || !res.numHits || res.numHits < 1 || !res.results) {
opts.push(
<Select key="0" value="0">
"Error loading LS List"
</Select>
);
} else {
res.results.forEach(n => {
opts.push(
<MenuItem key={n.id} value={n.id}>
<Typography> {n.userName} </Typography>
</MenuItem>
);
});
}
// use setState to correctly refresh the component after returning from async function
this.setState({ activeLsList: opts });
});
};
render() {
// check before triggering the async function to prevent infinite loop
if (!this.state.activeLsList) this.getActiveLsList();
return (
<TextField
select
fullWidth
variant="outlined"
value={this.state.assignedLS}
onChange={this.updateAssignment("assigned_loan_specialist", "assignedLS")}
margin="normal"
>
<MenuItem value="Unassigned">Unassigned</MenuItem>
{this.state.activeLsList} // render from this.state
</TextField>
);
}