3
votes

I am new to Sencha Touch and web-programming. I am trying to display a nested list and am only getting the first level items in a loop over and over again.

I have looked at the documentation and kitchen sink examples, but I just can't get it right. I would be very thankful for tips!

This is my Code:

 Ext.regModel('Site', {
     fields: [{
         name: "sitename",
         type: "string"
     }, {
         name: "last_connection",
         type: 'auto'
     }]
 });


 Ext.setup({
     icon: 'icon.png',
     tabletStartupScreen: 'tablet_startup.png',
     phoneStartupScreen: 'phone_startup.png',
     glossOnIcon: false,
     onReady: function() {

         var store = new Ext.data.TreeStore({
             model: 'Site',
             proxy: {
                 type: 'ajax',
                 url: 'network.json',
                 reader: {
                     type: 'json',
                     root: 'items'
                 }
             }
         });


         var nestedList = new Ext.NestedList({
             fullscreen: true,
             title: 'Netzwerk',
             displayField: 'sitename',
             plugins: [new Ext.LeafSelectedPlugin()],
             // add a / for folder nodes in title/back button
             getTitleTextTpl: function() {
                 return '{' + this.displayField + '}<tpl if="leaf !== true">/</tpl>';
             },
             // add a / for folder nodes in the list
             getItemTextTpl: function() {
                 return '{' + this.displayField + '}<tpl if="leaf !== true">/</tpl>';
             },

             store: store
         });
     }
 });

And the structure of the json-file (only one node as example):

{
    "items": [{
        "id": "11",
        "sitename": "Site A",
        "items": [{
            "id": "11",
            "sitename": "Endpoint 1",
            "last_connection": "2012-03-02T10:03:22+0100",
            "ping_time": 63,
            "leaf": true
        }, {
            "id": "12",
            "sitename": "Endpoint 2",
            "last_connection": "2012-03-02T10:03:22+0100",
            "ping_time": 57,
            "leaf": true
        }],
        "last_connection": "2012-03-02T10:03:22+0100",
        "ping_time": 57
    }]
}

Thanks!

1

1 Answers

0
votes

I know this post is a little bit old, but if it would already be answered it would have saved me 2 "long" hours of my life, so here it goes:

I figured out I was missing the defaultRootProperty inside TreeStore. According to your JSON you need to set this property to items.

Here is how your TreeStore should look like:

var store = new Ext.data.TreeStore({
    model: 'Site',
    defaultRootProperty: 'items',
    proxy: {
       type: 'ajax',
       url: 'network.json',
        reader: {
            type: 'json',
            root: 'items'
        }
    }
});