简体   繁体   English

React JS-解析复杂的Json结构并在下拉列表中填充

[英]React JS - Parse a complex Json structure and Populate in a dropdown list

I get a backend data structure as following. 我得到如下的后端数据结构。

{
    "courseDetails" : 
        [{"departmentId" : 105654,
        "courses" : 
            [{"stream" : "science","courseIds" : ["104","105","106"]}, 
             {"stream" : "art","courseIds" : ["102", "103"]}]
         }, 
          {"departmentId" : 105655,
          "courses" : 
            [{"stream" : "commerce","courseIds" : ["101", "100"]}]
          }
         ]
}

In the UI, I have three dropdown lists. 在用户界面中,我有三个下拉列表。

Department ID       |      Stream        | Course ID

First Department ID dropdown list should be populated with departmentIDs 105654 and 105655. When department id 105654 is selected, Stream dropdown list should be populated as science and art. 应该用“部门ID 105654”和“ 105655”填充“第一部门ID”下拉列表。选择“部门ID 105654”后,“流”下拉列表应填充为科学和艺术类。 When science is selected from this second dropdown list, course ids should be populated in third dropdown list as 104, 105, 106. 从第二个下拉列表中选择科学后,课程ID应该在第三个下拉列表中填充为104、105、106。

The json data structure above is to have all data at once so that I don't have to make backend call each time a dropdown option is selected. 上面的json数据结构是一次拥有所有数据,因此我不必每次选择下拉选项时都进行后端调用。

How can I achieve this using react js? 如何使用react js实现此目的? I am new to it. 我是新来的。

Assuming that you receive data in data and your dropdown components each accept an array that must be inserted in their respective dropdown menus, you get the arrays like this: 假设您接收到数据中的data并且下拉菜单组件均接受一个必须插入其各自的下拉菜单中的数组,则您将获得如下数组:

const Department = data.courseDetails.map(obj => obj.departmentId);

Now assuming you have some departmentId selected, you can do: 现在假设您选择了一些departmentId,则可以执行以下操作:

const streams = data.courseDetails.filter(x => x.departmentId === departmentId)[0].courses.map(obj => obj.stream)

Now assuming you have the stream stored in streamId, you can do: 现在假设您已将流存储在streamId中,则可以执行以下操作:

const courseIds = data.courseDetails.filter(x => x.departmentId === departmentId)[0].courses.filter(x => x.stream === streamId)[0].courseIds;

You should store each array in a state and pass on that state as a prop to the corresponding dropdown components. 您应该将每个数组存储在一个状态中,并将该状态作为对相应下拉组件的支持。

I would like to use 3 react-select for 3 dropdown list and connect them based on the selected value of one another. 我想对3个下拉列表使用3 react-select并根据选定的另一个值连接它们。 the state will look like your provided data and three flag for three dropdown fields. 状态看起来像您提供的数据和三个下拉字段的三个标志。

dropdown1: true, dropdown2: false, dropdown3: false

The render will look like: 渲染看起来像:

{this.state.dropdown1 ? <Select options=[all the Department ID] onChange={handleSelectDepartment} />: null}
{this.state.dropdown2 ? <Select options=[course corresponding stream ] onChange={handleSelectStream} />: null}
{this.state.dropdown3 ? <Select optons=[Stream corresponding courseID ] onChange={handleSelectCourse} />: null}

So first the dropdown list of Department will be visible only. 因此,首先“部门”的下拉列表将仅可见。 On selecting a value in dropdown list of Department i would set the dropdown2 state to true and it will make the dropdown list of Stream visible and so on.. 在Department的下拉列表中选择一个值时,我会将dropdown2状态设置为true ,这将使Stream的下拉列表可见,依此类推。

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

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