R
R
rhost2016-06-17 12:12:54
Mobile development
rhost, 2016-06-17 12:12:54

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
                }
            ]
        }
    ]

I'm trying to get through like this
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);
    },

gives an error 1A5ZbKhKlxPBrE.jpg
please tell me how to fix it, you just need to enter
the title of the
dish category with prices
the title of the
dish category with prices

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2016-06-17
@hoOstel

I think renderRow and renderSectionHeader should return renderable elements, not undefined as in the code above. If you have something to dig here: https://facebook.github.io/react-native/docs/listv...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question