0

Is there any way to insert new record into TreeStore?

TreeStore is a data source of NestedList, so at the end point i need to add new item into NestedList

Here is my example https://fiddle.sencha.com/#fiddle/4f4

var dataStore;
var dataObj;

Ext.application({
    name: 'testApp',

    launch: function() {
        // data
        dataObj = {
            items: [
                {
                    field1: '1',
                    items: [{
                        field1: '1.1',
                        items: [{
                            field1: '1.1.1 last',
                            leaf: true
                        }, {
                            field1: '1.1.2 last',
                            leaf: true
                        }]
                    }, {
                        field1: '1.2 last',
                        leaf: true
                    }]
                }, 
                {
                    field1: '2',
                    items: [{
                        field1: '2.1 last',
                        leaf: true
                    }, {
                        field1: '2.2 last',
                        leaf: true
                    }]
                }
            ]
        }

        // model
        Ext.define('dataModel', {
            extend: 'Ext.data.Model',
            config: {
                fields: [
                    {name: 'field1', type: 'string'},
                ]
            }
        });

        // TreeStore
        dataStore = Ext.create("Ext.data.TreeStore", {
            storeId: "usersStore",
            model: "dataModel",
            defaultRootProperty: 'items',
            data : dataObj
        });

        // Nestedlist and display
        Ext.create('Ext.NestedList', {
            fullscreen: true,
            displayField: 'field1',
            store: dataStore
        });
    }

});

Similar threads: Sencha Touch 2: Insert into TreeStore/NestedList

Thanks!

Community
  • 1
  • 1
Yaroslav
  • 651
  • 2
  • 9
  • 17

1 Answers1

0

Store is binded with your nestedlist, so just add new data to store.

    dataObj.items.push({
        field1: '3',
        items: [{
            field1: '3.1 last',
                leaf: true
            }, {
                field1: '3.2 last',
                leaf: true
        }]                    
    });
    dataStore.setData(dataObj);
Scoup
  • 1,323
  • 8
  • 11
  • Thank you Scoup! I know this way but it is not convenient, cause `dataStore.setData(dataObj);` leads to NestedList to be reloaded. So if you open some record, after `dataStore.setData(dataObj);` root records will be desplayed – Yaroslav Mar 24 '14 at 17:01
  • 1
    Sorry for disappointing but you cant do that. Nestedlist is binded with store. You can see source here: http://docs.sencha.com/touch/2.3.0/source/NestedList.html#Ext-dataview-NestedList And this comment: http://www.sencha.com/forum/showthread.php?205097-How-to-create-a-Nested-List-with-dynamic-loading-of-each-node – Scoup Mar 24 '14 at 17:44