我有两个硬编码了下拉列表的MUI下拉列表.我需要根据从第一个下拉列表中 Select 的内容填充第二个下拉列表

const [queryType, setqueryType] = React.useState('');
const [subCategory, setsubCategories] = React.useState('');

const handleQueryTypeDropdownChange = (event) => {
    const {
      target: { value },
    } = event;
    setqueryType(
      // On autofill we get a stringified value.
      typeof value === 'string' ? value.split(',') : value,
    );
};
const handleSubCategoryDropdownChange = (event) => {
    const {
      target: { value },
    } = event;
    setsubCategories(
      // On autofill we get a stringified value.
      typeof value === 'string' ? value.split(',') : value,
    );
};

const queryTypes = ['X', 'Y', 'Z'];
const subTypes = {X: ['AA', 'AAA'],
                  Y: ['B', 'BBB'],
                  Z: ['CC', 'CCC]};



<FormControl sx={{ m: 0, minWidth: '100%' }}>
    <InputLabel id="demo-simple-select-helper-label1">Query Type</InputLabel>
    <Select
      labelId="Query Type"
      id="queryType"
      value={queryType}
      onChange={handleQueryTypeDropdownChange}
      input={<OutlinedInput label="Query Type" />}
    >
      {queryTypes.map((queryType) => (
        <MenuItem
          key={queryType}
          value={queryType}
        >
        {queryType}
        </MenuItem>
      ))}
    </Select>
</FormControl>

<FormControl sx={{ m: 0, minWidth: '100%' }}>
<InputLabel id="demo-simple-select-helper-label1">Sub Type</InputLabel>
<Select
  labelId="Sub type"
  id="subCategory"
  value={subCategory}
  onChange={handleSubCategoryDropdownChange}
  input={<OutlinedInput label="Sub Category" />}
>
  **// need to do subTypes.A if the queryType is selected as 'A'**
  {subTypes.A.map((queryType) => (
    <MenuItem
      key={queryType}
      value={queryType}
    >
    {queryType}
    </MenuItem>
  ))}
</Select>
</FormControl>

如果有一种方法,我可以根据 Select 的queryType下拉值填充JSON中的subCategory下拉列表.

// need to do subTypes.A if the queryType is selected as 'A'

谢谢,

推荐答案

据我所知,您希望 Select 与第一个下拉菜单相关的子类别.

我认为你可以在价值中计入这样的东西.

value = {subCategory[originalCategory]}

它将类似于这个子类别[‘X’].

您需要访问子对象中的CurentParentType,因此您 需要这样做:父[子]->;子类型[‘X’].map()

代码示例:

{subTypes[currentQueryType].map((queryType) => (
    <MenuItem
      key={queryType}
      value={queryType}
    >
    {queryType}
    </MenuItem>
  ))}

Javascript相关问答推荐

在JavaScript中打开和关闭弹出窗口

添加/删除时React图像上传重新渲染上传的图像

如何在NightWatch.js测试中允许浏览器权限?

将json数组项转换为js中的扁平

在贝塞尔曲线的直线上找不到交叉点:(使用@Pomax的bezier.js)

Phaser框架-将子对象附加到Actor

无法在nextjs应用程序中通过id从mongoDB删除'

React 17与React 18中的不同setState行为

在服务器上放置了Create Reaction App Build之后的空白页面

当试图显示小部件时,使用者会出现JavaScript错误.

简单的PayPal按钮集成导致404错误

无法读取未定义错误的属性路径名''

JS—删除对象数组中对象的子对象

当使用';字母而不是与';var#39;一起使用时,访问窗口为什么返回未定义的?

如何在不影响隐式类型的情况下将类型分配给对象?

如何使用基于promise (非事件emits 器)的方法来传输数据?

JavaScript:如果字符串不是A或B,则

在Java脚本中构建接口的对象

如何将对象推送到firestore数组?

在对象的嵌套数组中添加两个属性