简体   繁体   English

在 select 框反应中显示数组中的项目

[英]Display items from array in select box react

I am working on an application where I am taking data(array of sectors) from firestore database and adding this array to select box options.我正在开发一个应用程序,我正在从 firestore 数据库中获取数据(扇区数组)并将此数组添加到 select 框选项。 My problem is that I can't properly display each item in a separate option, instead my entire array is displayed in one line in the select box option.我的问题是我无法在单独的选项中正确显示每个项目,而是我的整个数组显示在 select 框选项中的一行中。 a screenshot of what my application looks like with the described problem.我的应用程序与所描述问题的屏幕截图。

Parts of the code responsible for getting an array from the database and displaying it in the select box options:负责从数据库中获取数组并将其显示在 select 框选项中的部分代码:

const [allSectors, setAllSectors] = useState([]);

useEffect(() => {
    getSectors();
  }, []);

  const getSectors = async () => {
    const data = await UserDataService.getAllSectorsData();
    setAllSectors(data.docs.map((doc) => ({ ...doc.data(), id: doc.id })));
  };

  const FormSelect = () => {
    return allSectors.map((sector) => (
      <option key={sector.id} value={sector.id}>
        {sector.Manufacturing}
      </option>
    ));
  };

return (
    <>
   <Form onSubmit={handleSubmit}>
      <Form.Group className="mb-3" controlId="formUserSector">
         <InputGroup>
            <InputGroup.Text id="formUserSector">Sectors: </InputGroup.Text>
               <Form.Select size="sm" onChange={(e) => setSectors(e.target.value)}>
                {FormSelect()}
              </Form.Select>
         </InputGroup>
      </Form.Group>
   </Form>
   </>
)

Not sure which UI library you are using.不确定您使用的是哪个 UI 库。 The first thing I noticed is that you use <option> tag without surrounding it with <select> tag我注意到的第一件事是你使用<option>标签而不用<select>标签包围它

  <option key={sector.id} value={sector.id}>
    {sector.Manufacturing}
  </option>

I'm assuming you are trying to implement HTML <select> dropdown.我假设您正在尝试实现 HTML <select>下拉菜单。 I think if you use only tags without surrounding it with it will just return strings, which is what happening in your example.我想如果你只使用标签而不用它包围它,它只会返回字符串,这就是你的例子中发生的事情。

Try changing your FormSelect function to something like this:尝试将您的 FormSelect function 更改为如下所示:

const FormSelect = () => {
    return (
        <select>
            {allSectors.map((sector) => (
                <option key={sector.id} value={sector.id}>
                    {item}
                </option>
            ))}
        </select>
    );
};

EDIT 1编辑 1

From your comments, I noticed that your sector.Manufacturing is an array, but it should be a string.从您的评论中,我注意到您的sector.Manufacturing是一个数组,但它应该是一个字符串。 When you put an array inside option like this <option>{array}</option> your get all the array values squashed into one <option> tag.当你把一个数组放在像这样的<option>{array}</option>中时,你会得到所有的数组值被压缩到一个<option>标签中。

try also looping over sector.Manufacturing rather than allSector.也尝试循环sector.Manufacturing而不是 allSector。 Hard to solve without seeing the full code, but it might look something like this:没有看到完整的代码很难解决,但它可能看起来像这样:

    const FormSelect = () => {
    return (
        <select>
            {allSectors.map((sector) => (
                <option key={sector.id} value={sector.id}>
                    {sector.Manufacturing.map((item) => {
                        return item;
                    })}
                </option>
            ))}
        </select>
      );
   };

EDIT 2 Try this:编辑 2试试这个:

const FormSelect = () => {
    return (
        <select>
            {allSectors.map((sector) =>
                sector.Manufacturing.map((item, i) => {
                    return (
                        <option key={i} value={item}>
                            {item}
                        </option>
                    );
                })
            )}
        </select>
    );
};

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

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