在使用循环时,无法在 react-select 中选择多个选项。

huangapple go评论68阅读模式
英文:

unable to select multiple options in react-select when we use it in loop

问题

以下是您提供的内容的翻译部分:

"该表格使用静态数据循环显示,并在每一行中提供一个选择框,以选择多个选项。因此,如果我选择任何一行的选项,那么它将不显示其他选项,无法从剩余的选项中选择任何一个,甚至不可见。没有选项可供选择。我需要选择多个选项并保存整个表格。"

const [selectedOptions, setSelectedOptions] = useState([]);
const objects = [
  {'id': 1, 'objectName': 'Obj1'},
  {'id': 2, 'objectName': 'Obj2'},
  {'id': 3, 'objectName': 'Obj3'}
];
const options = [
  {'id': 1, 'attrName': 'atr1'},
  {'id': 2, 'attrName': 'atr2'},
  {'id': 3, 'attrName': 'atr3'}
];
const handleSelectChange = (selectedOptions, index) => {
  console.log(selectedOptions, index);
  setSelectedOptions((prevSelectedOptions) => {
    const updatedOptions = [...prevSelectedOptions];
    updatedOptions[index] = selectedOptions;
    return updatedOptions;
  });
};

<Table variant='simple' color='gray.500' mb='24px' border={'1px'} borderColor={borderColor}>
  <Thead>
    <Tr >
      <Th pe='10px' borderColor={borderColor}>
        <Flex
          justify='space-between'
          align='center'
          fontSize={{ sm: "10px", lg: "12px" }}
          color='gray.400'>
          对象
        </Flex>
      </Th>
      <Th pe='10px' borderColor={borderColor}>
        <Flex
          justify='space-between'
          align='center'
          fontSize={{ sm: "10px", lg: "12px" }}
          color='gray.400'>
          属性
        </Flex>
      </Th>
    </Tr>
  </Thead>
  <Tbody >
    {
      objects.map((object, index) => (
        <Tr key={object.id}>
          <Td>{object["objectName"]}</Td>
          <Td>
            <Select key={object.id}
              closeMenuOnSelect={false}
              value={selectedOptions[index] || []}
              onChange={(selectedOptions) =>
                handleSelectChange(selectedOptions, index)
              }
              isMulti={true}
              options={options}
            />
          </Td>
        </Tr>
      ))
    }
  </Tbody>
</Table>

希望这可以帮助您完成所需的任务。

英文:

The table is looped with static data, with one select box to select multiple options in each row. So if I select any row option, then it shows no option. unable to select any one from remaining options and not even visible. showing no option. i need to select multiple options and save the entire table.

const [selectedOptions, setSelectedOptions] = useState([]);
const objects = [
{&#39;id&#39;:1, &#39;objectName&#39;:&#39;Obj1&#39;}
{&#39;id&#39;:2, &#39;objectName&#39;:&#39;Obj2&#39;}
{&#39;id&#39;:3, &#39;objectName&#39;:&#39;Obj3&#39;}
];
const options = [
{&#39;id&#39;:1, &#39;attrName&#39;:&#39;atr1&#39;}
{&#39;id&#39;:2, &#39;attrName&#39;:&#39;atr2&#39;}
{&#39;id&#39;:3, &#39;attrName&#39;:&#39;atr3&#39;}]
const handleSelectChange = (selectedOptions, index) =&gt; {
console.log(selectedOptions, index)
setSelectedOptions((prevSelectedOptions) =&gt; {
const updatedOptions = [...prevSelectedOptions];
updatedOptions[index] = selectedOptions;
return updatedOptions;
});
};
&lt;Table variant=&#39;simple&#39; color=&#39;gray.500&#39; mb=&#39;24px&#39; border={&#39;1px&#39;} borderColor={borderColor}&gt;
&lt;Thead&gt;
&lt;Tr &gt;
&lt;Th pe=&#39;10px&#39; borderColor={borderColor}&gt;
&lt;Flex
justify=&#39;space-between&#39;
align=&#39;center&#39;
fontSize={{ sm: &quot;10px&quot;, lg: &quot;12px&quot; }}
color=&#39;gray.400&#39;&gt;
Object
&lt;/Flex&gt;
&lt;/Th&gt;
&lt;Th pe=&#39;10px&#39; borderColor={borderColor}&gt;
&lt;Flex
justify=&#39;space-between&#39;
align=&#39;center&#39;
fontSize={{ sm: &quot;10px&quot;, lg: &quot;12px&quot; }}
color=&#39;gray.400&#39;&gt;
Attributes
&lt;/Flex&gt;
&lt;/Th&gt;
&lt;/Tr&gt;
&lt;/Thead&gt;
&lt;Tbody &gt;
{
objects.map((object,index)=&gt;(
&lt;Tr key={object.id}&gt;
&lt;Td&gt;{object[&quot;objectName&quot;]}&lt;/Td&gt;
&lt;Td&gt;
&lt;Select key={object.id}
closeMenuOnSelect={false}
value={selectedOptions[index] || []}
onChange={(selectedOptions) =&gt;
handleSelectChange(selectedOptions, index)
}
isMulti={true}
options={options}
/&gt;
&lt;/Td&gt;
&lt;/Tr&gt;
))
}
&lt;/Tbody&gt;
&lt;/Table&gt;

please help me to do this.

答案1

得分: 1

请检查以下代码,它可以帮助您。

您可以将 object.id 作为您的状态的键,并将数组设置为值,这可以用于循环处理具有相同选项的对象。

import React, { useState } from "react";

import Select from "react-select";

export default () => {
  const objects = [
    { id: 1, objectName: "Obj1" },
    { id: 2, objectName: "Obj2" },
    { id: 3, objectName: "Obj3" }
  ];
  const options = [
    { value: 1, label: "atr1" },
    { value: 2, label: "atr2" },
    { value: 3, label: "atr3" }
  ];

  const [selectedOptions, setSelectedOptions] = useState({});

  return (
    <>
      <div>
        {objects.map((obj) => (
          <Select
            key={`obj_${obj.id}`}
            isMulti
            options={options}
            value={selectedOptions[obj.id] || []}
            onChange={(newVal) => {
              setSelectedOptions((prev) => ({
                ...prev,
                [obj.id]: newVal
              }));
            }}
          />
        ))}
      </div>
    </>
  );
};

链接至 codesandbox

英文:

Please check the following code which can help you.

You can take object.id as key for the your state and set array as an value which can be used for your loop of objects with same options.

import React, { useState } from &quot;react&quot;;

import Select from &quot;react-select&quot;;

export default () =&gt; {
  const objects = [
    { id: 1, objectName: &quot;Obj1&quot; },
    { id: 2, objectName: &quot;Obj2&quot; },
    { id: 3, objectName: &quot;Obj3&quot; }
  ];
  const options = [
    { value: 1, label: &quot;atr1&quot; },
    { value: 2, label: &quot;atr2&quot; },
    { value: 3, label: &quot;atr3&quot; }
  ];

  const [selectedOptions, setSelectedOptions] = useState({});

  return (
    &lt;&gt;
      &lt;div&gt;
        {objects.map((obj) =&gt; (
          &lt;Select
            key={`obj_${obj.id}`}
            isMulti
            options={options}
            value={selectedOptions[obj.id] || []}
            onChange={(newVal) =&gt; {
              setSelectedOptions((prev) =&gt; ({
                ...prev,
                [obj.id]: newVal
              }));
            }}
          /&gt;
        ))}
      &lt;/div&gt;
    &lt;/&gt;
  );
};

Link to codesandbox

huangapple
  • 本文由 发表于 2023年7月13日 12:06:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76675843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定