Answer the question
In order to leave comments, you need to log in
How to display data through ListView?
I have a json like...
......
"main": [
{
"categories": [
{
"children_categories": [],
"poster": "",
"description": "",
"title": "Parmigiana Sandwiches",
"order": 1,
"goods": [{
"composition": "",
"prices": {
"medium": "0.00",
"small": "7.95",
"large": "8.95"
},
"title": "Chicken",
"order": 0,
"id": 613
},],
"id": 96
},
{
"children_categories": [],
"poster": "",
"description": "",
"title": "Just Good Sandwiches",
"order": 3,
"goods": [
{
"composition": "",
"prices": {},
"title": "Honey Mustard Chicken",
"order": 0,
"id": 617
},
{
"composition": "",
"prices": {
"medium": "0.00",
"small": "7.95",
"large": "8.95"
},
"title": "Honey Mustard Chicken 1",
"order": 1,
"id": 620
},
],
"id": 94
}
]
}
]
getInitialState: function(){
return {
dataBlob: {},
dataSource : new ListView.DataSource({
rowHasChanged : (row1, row2) => row1 !== row2,
sectionHeaderHasChanged : (s1, s2) => s1 !== s2
})
}
},
fethData: function() {
var categories = this.context.content.main[0].categories,
category,
goods,
dish,
dataBlob = {},
sectionIDs = [],
rowIDs = [];
for(var i = 0; i < categories.length; i++){
category = categories[i];
sectionIDs.push(category.id);
dataBlob[category.id] = category.title;
goods = category.goods;
rowIDs[i] = [];
for(var j = 0; j < goods.length; j++){
dish = goods[j];
rowIDs[i].push(dish.title);
dataBlob[category.id + ':' + dish.title] = dish;
}
}
this.setState({
dataSource: this.state.dataSource.cloneWithRowsAndSections(dataBlob, sectionIDs, rowIDs)
});
},
componentDidMount: function() {
this.fethData();
},
render: function() {
return (
<View style={styles.container}>
<View>
<ListView
dataSource = {this.state.dataSource}
renderRow = {this.renderRow}
renderSectionHeader = {this.renderSectionHeader}
/>
</View>
</View>
);
},
renderRow: function(rowData, sectionID, rowID) {
console.log(rowData);
},
renderSectionHeader: function(sectionData, sectionID) {
console.log(sectionData);
},
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question