简体   繁体   English

ReactJS Object 反应选择中的数组数据结构

[英]ReactJS Object Array Data Structuring in react-select

Hope we are doing great fams?希望我们做伟大的家庭? I trust.我相信。 I have a data object in a separate js file and i wanted to binded it to a react-select so that the react-select dropdown can be dynamically populated.我在一个单独的 js 文件中有一个数据 object,我想将它绑定到一个 react-select,以便可以动态填充 react-select 下拉列表。 The data object is like this:数据object是这样的:


    =======DDLListValues.js=======

export const countryListObject = [
    {label: "Afghanistan", value: "Afghanistan"},
    {label: "Albania", value: "Albania"},
    {label: "Åland Islands", value: "Åland Islands"},
    {label: "Algeria", value: "Algeria"},
    {label: "American Samoa", value: "American Samoa"},
    {label: "Andorra", value: "Andorra"},
];

export const stateList = [
    Afghanistan: [
          "Abc",
          "efg",
          "hij",
          "klm",         
        ],
        Albania: [
         "Abc",
          "efg",
          "hij",
          "klm",   
        ],
      Algeria: [
         "Abc",
          "efg",
          "hij",
          "klm",   
        ],
       American Samoa: [
         "Abc",
          "efg",
          "hij",
          "klm",   
        ],
       Andorra: [
         "Abc",
          "efg",
          "hij",
          "klm",   
        ],
];

WHAT I WANTED TO ACHIEVE i have two react-select dropdown that are dependent and I binded the countryListObject to the first dropdown select and onchange of the first react-select, it should automatically populates the second react-select dropdown with the corresponding selected value of the country react-select dropdown in the stateListObject in the javascript file ie it checks the stateListObject and pick the inner array that corresponds to the selected value of the country.我想要实现的目标我有两个依赖的反应选择下拉列表,我将 countryListObject 绑定到第一个下拉列表 select 和第一个反应选择的 onchange,它应该自动填充第二个反应选择下拉列表相应的选定值javascript 文件中 stateListObject 中的国家 react-select 下拉列表,即它检查 stateListObject 并选择与国家选定值相对应的内部数组。

My code is below:我的代码如下:

In my reactjs file, i have the following:在我的 reactjs 文件中,我有以下内容:


    =========Reactform.js

import { countryListObject, stateListObject } from './DDLListValues';

const [selectedcountry, setSelectedcountry] = useState({});
const [selectedstate, setSelectedstate] = useState({});

const [countries, setCountries] = useState(countryListObject);
const [stateoforigin, setStateoforigin] = useState({}); 

const onChangeCountry = (obj) => {
  setSelectedcountry(obj);
}

const onChangeState = (obj) => {
  setSelectedstate(obj);
}


{/* Binded countryListObject getting selected value successfully  */}
<Select name="countries" 
placeholder="Select your country"
value={selectedcountry}
options={ countryListObject } 
getOptionLabel={(option) => option.label}
getOptionValue={(option) => option.label}
onChange={(option) => onChangeCountry(option)}
/>

{/* I don't know how to accomplish this */}
<Select 
placeholder="States"
value={}
options={} 
onchange={} 
/>

Thank you fams in advance提前谢谢各位家人

First, you need to transform your state object keys into an array (i assume it's an object and you made a syntax mistake) and then iterate to find the country.首先,您需要将 state object 键转换为一个数组(我假设它是一个 object 并且您犯了语法错误)然后迭代以找到国家/地区。

const onChangeState = (obj) => {
  setSelectedstate(obj);
  
  const stateCountryName = Object.keys(stateList).find((countryName) => {
    const isStateCountry = stateList[countryName].indexOf(obj.value) > -1;
    
    if (isStateCountry) return true;
    return false;
  })

 const countryObj = countryArray.find((country) => country.label === stateCountryName)
 
 onChangeCountry(countryObj);
}

deals with:对付:

export const countryListObject = [
    {label: "Afghanistan", value: "Afghanistan", key: 'af'},
    {label: "Albania", value: "Albania", key: 'al'},
    {label: "Åland Islands", value: "Åland Islands", key: 'ai'},
    {label: "Algeria", value: "Algeria", key: 'alg'},
    {label: "American Samoa", value: "American Samoa", key: 'as'},
    {label: "Andorra", value: "Andorra", key: 'an'},
];

export const stateListObject = {
    af: [
          {label: "ABC", value: "ABC"},
          ......
        ],
        al: [
          {label: "ABC", value: "ABC"},
          ......  
        ],
      alg: [
          {label: "ABC", value: "ABC"},
          ......  
        ],
       as: [
          {label: "ABC", value: "ABC"},
          ...... 
        ],
       an: [
          {label: "ABC", value: "ABC"},
          ......  
        ],
};
const [states, setStates] = useState([]);
const onChangeCountry = (obj) => {
  setSelectedcountry(obj);
  setStates(stateListObject[obj.key]);
}

Thank you Vini for your efforts and time taken to answer this question but I have states array and i shared it in the question.感谢 Vini 为回答这个问题付出的努力和时间,但我有状态数组,我在问题中分享了它。 Take a look at it below: –看看下面: -


    export const stateListObject = [
    Afghanistan: [
          "Abc",
          "efg",
          "hij",
          "klm",         
        ],
        Albania: [
         "Abc",
          "efg",
          "hij",
          "klm",   
        ],
      Algeria: [
         "Abc",
          "efg",
          "hij",
          "klm",   
        ],
       American Samoa: [
         "Abc",
          "efg",
          "hij",
          "klm",   
        ],
       Andorra: [
         "Abc",
          "efg",
          "hij",
          "klm",   
        ],
];

Is the state array good like that or I should wrap the value of each country in the state array like this {label,value}? state 数组是这样好还是我应该像这样将每个国家/地区的值包装在 state 数组中 {label,value}? But mind you, I am only populating the state dropdown on select of country dropdown but your solution looks like you are doing the population in country dropdown.但请注意,我只是在 select 国家/地区下拉列表中填充 state 下拉列表,但您的解决方案看起来像是在国家/地区下拉列表中填充人口。

deal with this:处理它:

export const countryListObject = [
  {label: "Afghanistan", value: "Afghanistan"},
  {label: "Albania", value: "Albania"},
  {label: "Angola", value: "Angola"}
];

export const stateListObject = {
  Afghanistan: [
    {value: "ABC"},
    {value: "DEF"},
    {value: "GHI"},
  ],
  Albania: [
    {value: "123"},
    {value: "456"},
    {value: "789"},
  ],
  Angola: [
    {value: "A12"},
    {value: "B12"},
    {value: "C12"},
  ],
}
==========ReactForm.js
const [states, setStates] = useState([]);
const [selectedCountry, setSelectedCountry] = useState(null);
const [selectedState, setSelectedState] = useState(null);

const onChangeCountry = (obj) => {
    setSelectedCountry(obj);
    setStates(stateListObject[obj.value]);
}

const onChangeState = (obj) => {
    setSelectedState(obj);
}

<Select name="countries"
              placeholder="Select your country"
              value={selectedCountry}
              options={ countryListObject }
              getOptionLabel={(option) => option.label}
              getOptionValue={(option) => option.label}
              onChange={onChangeCountry}
      />

      <Select
        placeholder="States"
        options={ states }
        defaultValue={selectedState}
        getOptionLabel={(option) => option.value}
        getOptionValue={(option) => option.value}
        onChange={onChangeState}
      />

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

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