简体   繁体   English

如何为100个菜单项创建一个下拉菜单?

[英]How to create a drop-down menu for 100 menu items?

I have the following piece of code in React JS front-end that I created using Material-UI . 我在使用Material-UI创建的React JS前端中有以下代码。 It creates a drop-down menu field. 它创建一个下拉菜单字段。

Menu items are hard-coded. 菜单项是硬编​​码的。 This works fine if the number of items is small. 如果项目数量少,则可以正常工作。 What if I have 100 items? 如果我有100件物品怎么办? Of course, I could list all 100 items as I did below in my example. 当然,我可以在示例中列出所有100个项目。 However, this approach is sort of an ugly workaround. 但是,这种方法有点丑陋。 Is there any correct way to do it? 有什么正确的方法吗? For example, can I read menu items from CSV file or a separate class of constants? 例如,我可以从CSV文件或单独的常量类中读取菜单项吗?

<Grid item xs={true}>
     <FormControl
         className={this.props.styles.formControl}
         margin="normal">
         <InputLabel shrink htmlFor="origin-label-placeholder">
            Origin
         </InputLabel>
         <Select
             onChange={(event) => this.props.handleChange("origin", event)}
             value={this.props.state.origin}
             input={<Input name="origin" id="origin-label-placeholder" />}
             displayEmpty
             name="origin">
                        <MenuItem value={"A"}>A-option</MenuItem>
                        <MenuItem value={"B"}>B-option<</MenuItem>
                        <MenuItem value={"C"}>C-option<</MenuItem>
                        <MenuItem value={"D"}>D-option<</MenuItem>
                        <MenuItem value={"E"}>E-option<</MenuItem>
                        <MenuItem value={"F"}>F-option<</MenuItem>
                        <MenuItem value={"G"}>G-option<</MenuItem>
                        <MenuItem value={"A2"}>A2-option<</MenuItem>
                        <MenuItem value={"B2"}>B2-option<</MenuItem>
                        <MenuItem value={"C2"}>C2-option<</MenuItem>
                        <MenuItem value={"D2"}>D2-option<</MenuItem>
         </Select>
    </FormControl>
</Grid>

You can create an array of options and iterate over the same, 您可以创建一个选项数组,并在相同的选项上进行迭代,

const options = [{label:"A-option",value:"A"},{label:"B-option",value:"B"},{label:"C-option",value:"C"},{label:"D-option",value:"D"}]

You have following places to write this, 您可以在以下地方写这个,

  1. You can maintain this in separate file, export the array and import where you want to use. 您可以将其保存在单独的文件中,导出阵列,然后导入要使用的位置。

  2. You can maintain this directly in component where you want to use it. 您可以直接在要使用它的组件中维护它。

  3. You can maintain in state. 您可以保持状态。

You can iterate like this, 你可以这样迭代

{options && options.length && options.map(option => {
      return <MenuItem value={option.value}>{option.label}</MenuItem>
   })
}

Note: If you choose to maintain in state, you need to use this.state.options . 注意:如果选择保持状态,则需要使用this.state.options


Update 更新资料

Maintain the array in separate file and export the same. 将阵列维护在单独的文件中,然后将其导出。 For example, Constants.js file 例如, Constants.js文件

export const options = [{label:"A-option",value:"A"},{label:"B-option",value:"B"},{label:"C-option",value:"C"},{label:"D-option",value:"D"}]

Then you need to import this in your component like, 然后,您需要将其导入到组件中,例如,

import {options} from './Constant';  //Notice the curly braces due to named export

Now you can iterate like I shown above. 现在您可以像上面显示的那样进行迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM