admin管理员组文章数量:1305628
Is there any way to display options
in an alphabetical order in the drop-down menu of Material-UI?
I know that arrays can be sorted simply using arr.sort()
. However, if I do const options = [...].sort()
, then I still see unsorted values in the drop-down menu.
const options = [
{label:"B",value:8033.86},
{label:"A",value:483.93},
{label:"Z",value:1246.3},
{label:"C",value:145.0},
{label:"E",value:244.5}
]
<Grid item xs={true}>
<FormControl
className={this.props.styles.formControl}
margin="normal">
<InputLabel shrink htmlFor="distanceTarget-label-placeholder">
Target:
</InputLabel>
<Select
onChange={(event) => this.props.handleChange("distanceTarget", event)}
value={this.props.state.distanceTarget}
input={<Input name="distanceTarget" id="distanceTarget-label-placeholder" />}
displayEmpty="true"
name="distanceTarget"
>
{options && options.length && options.map((option, i) => {
return <MenuItem value={option.value} key={i}>{option.label}</MenuItem>
})}
</Select>
</FormControl>
</Grid>
Is there any way to display options
in an alphabetical order in the drop-down menu of Material-UI?
I know that arrays can be sorted simply using arr.sort()
. However, if I do const options = [...].sort()
, then I still see unsorted values in the drop-down menu.
const options = [
{label:"B",value:8033.86},
{label:"A",value:483.93},
{label:"Z",value:1246.3},
{label:"C",value:145.0},
{label:"E",value:244.5}
]
<Grid item xs={true}>
<FormControl
className={this.props.styles.formControl}
margin="normal">
<InputLabel shrink htmlFor="distanceTarget-label-placeholder">
Target:
</InputLabel>
<Select
onChange={(event) => this.props.handleChange("distanceTarget", event)}
value={this.props.state.distanceTarget}
input={<Input name="distanceTarget" id="distanceTarget-label-placeholder" />}
displayEmpty="true"
name="distanceTarget"
>
{options && options.length && options.map((option, i) => {
return <MenuItem value={option.value} key={i}>{option.label}</MenuItem>
})}
</Select>
</FormControl>
</Grid>
Share
Improve this question
asked Aug 4, 2019 at 17:06
ScalaBoyScalaBoy
3,39214 gold badges52 silver badges96 bronze badges
4 Answers
Reset to default 5here a simple way to sort the array of objects and you can read the documentation
const options = [
{label:"B",value:8033.86},
{label:"A",value:483.93},
{label:"Z",value:1246.3},
{label:"C",value:145.0},
{label:"E",value:244.5}
]
console.log(options.sort((a, b) => (a.label > b.label) ? 1 : -1))
Material-UI is about styles, not logic. First of all, you need to sort your options.
for (const item in options) {
console.log(options[item].label)
}
Its first steps. The second you should try yourself.
When you're dealing with an Array of Objects, you need to tell what determines which one es first:
function sortAlphabeticallyAscending(a,b){
if(a.label < b.label) return -1;
else if (a.label > b.label) return 1;
else return 0;
}
options.sort(sortAlphabeticallyAscending);
basically, return -1 if you want to say that the index of a is lower than b (i.e. a es first), return 1 if you want to say that b es first, and 0 if you don't want to change the order
See the section pareFunction in the docs...
You have to define the sorting logic as your input array is an array of objects.
More info can be found here
const options = [
{label:"B",value:8033.86},
{label:"A",value:483.93},
{label:"Z",value:1246.3},
{label:"C",value:145.0},
{label:"E",value:244.5}
];
const objComparator = function(a, b) {
if (a.label < b.label) {
return -1;
}
if (a.label > b.label) {
return 1;
}
return 0;
};
options.sort(objComparator);
本文标签: javascriptHow can I sort dropdown menu items alphabeticallyStack Overflow
版权声明:本文标题:javascript - How can I sort drop-down menu items alphabetically? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741807306a2398583.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论