This question is about react native mobile application. I want to know how to generate dropdown according to my API JSON array length values.
In my case output values are like this
eg:- Choice 1 (Label of the Drop Down)
-Sub Choice 1 . (Value Data)
-Sub Choice 2 (Value Data)
-Sub Choice 3 (Value Data)
Choice 2 (Label of the second Drop Down)
-Sub Choice 1 . (Value Data)
-Sub Choice 2 . (Value Data)
-Sub Choice 3 . (Value Data)
Like this, if API response gives Choice 1, Choice 2 -> then I need to generate 2 dropdowns accordingly.
This is my code and according to my code I only get the last Choice and Choice Data of API response array
I'm using the react-native-material-dropdown library and I need to map this accordingly with JSON array length
if(responseText.success == true)
{
var count = Object.keys(responseText.data).length; //Getting Array Data Length
let drop_down_data = []; // For Dropdown box
for(var i=0;i<count;i++){
// Next Loop for Fetching Choice Items
var t_count = Object.keys(responseText.data[i].choiceItems).length;
for(var j=0;j<t_count;j++){
var Add_On_Name = responseText.data[i].name
console.log(Add_On_Name)
this.setState({addOn_name: Add_On_Name});
this.setState({riceTypeData:responseText.data[i].choiceItems[j].name});
drop_down_data.push({ value: responseText.data[i].choiceItems[j].name});
}
}
this.setState({ drop_down_data , progressDialog:false}); // Set the new state
}
Hope this helps.
//This is the initial state.
state = {
dropdownsdata: []
};
//This is going into the api successcallback
if (responseText.success) {
let all_dropdownData = [];
for (let i = 0; i < responseText.data.length; i++) {
let ind_dropdownData = [];
for (let j = 0; j < responseText.data[i].choiceItems.length; j++) {
let Add_On_Name = responseText.data[i].choiceItems[j].name;
let temp = {};
temp.value = Add_On_Name;
ind_dropdownData.push(temp);
}
let temp_obj = {};
temp_obj.label = responseText.data[i].name;
temp_obj.data = ind_dropdownData;
all_dropdownData.push(temp_obj);
}
this.setState({ dropdownsdata: all_dropdownData });
}
//Function for rendering n number of dropdowns.
renderDropdowns = () => {
return this.state.dropdownsdata.map(e => {
return <Dropdown label={e.label} data={e.data} />;
});
};
render(){
return <View>{this.renderDropdowns()}</View>;
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.