Hybrid View

  1. #1
    Sencha Premium Member Zdeno's Avatar
    Join Date
    Nov 2009
    Location
    Prague
    Posts
    401
    Vote Rating
    17
    Zdeno will become famous soon enough

      0  

    Default Ext.data.TreeStore - remove and restore node

    Ext.data.TreeStore - remove and restore node


    I've own function rejectDeleted nodes on treeStore. Before you will try this code you need fix removeChild function in NodeInterface.js - add comment before node.clear(); This is hard fix - read more at http://www.sencha.com/forum/showthre...data.TreeStore

    Code:
    if (destroy) { 
        node.destroy(true); 
    } else { 
        //node.clear(); 
    }
    Now when we have fixed it we can run our code:

    Code:
    Ext.override(Ext.data.TreeStore, {
        onNodeRemove: function(parent, node, isMove) {
            var me = this,
                removed = me.removed;
    
            if (!node.isReplace && Ext.Array.indexOf(removed, node) == -1) {
                removed.push(node);
            }
    
            if (me.autoSync && !me.autoSyncSuspended && !isMove) {
                me.sync();
            }
        },
        rejectDeleted: function() {
            var me = this,
                removed = me.removed,
                rootNode = me.getRootNode(),
                length = removed.length;
    
            removed.reverse();
    
    
            for (var i = 0; i < length; i++) {
                node = removed[0];
    
                parentNode = node.parentNode;
    
                isParentNode = rootNode.findChildBy(function(){
                    if (this.internalId == parentNode.internalId)
                        return true;
                });
    
                index = node.data.index;
    
                if (!isParentNode) {
                    console.log(node, index);
                    rootNode.insertChild(index, node);                 
                } else {
                    console.log(node, index);
                    parentNode.insertChild(index, node);
                }
            }
        }
    });
    
    var store = Ext.create('Ext.data.TreeStore', {
        root: {
            expanded: true,
            children: [
                { id: 1, text: "detention", leaf: true, index: 0 },
                { id: 2, text: "homework", index: 1, expanded: true, children: [
                    { id: 3, text: "book report", leaf: true, index: 0 },
                    { id: 4, text: "alegrbra", leaf: true, index: 1 }
                ] },
                { id: 5, text: "buy lottery tickets", leaf: true, index: 2 }
            ]
        }
    });
    
    var tp = Ext.create('Ext.tree.Panel', {
        title: 'Simple Tree',
        width: 200,
        height: 150,
        store: store,
        rootVisible: false,
        renderTo: Ext.getBody()
    });
    
    Ext.create('Ext.Button', {
        text: 'Delete',
        renderTo: Ext.getBody(),
        handler: function() {
    
            var sn = tp.getSelectionModel().getSelection();
            sn[0].remove()
            store.rejectDeleted();
            tp.getView().refresh()
    
            Ext.each(tp.getRootNode().childNodes, function(node) {
                console.log('ID: ' + node.data.id)
                console.log('TEXT: ' + node.data.text)
                console.log('INDEX: ' + node.data.index)
            });
        }
    });
    What we will get after we click on Delete button? Everything what we expected except when we click on homework node.

    Code:
    ID: 2
    TEXT: homework
    INDEX: 0
    ID: 1
    TEXT: detention
    INDEX: 1
    ID: 5
    TEXT: buy lottery tickets
    INDEX: 2
    Index is changed to 0. Why?

    Before i call function insertChild(index, node) console say: 1. So index of this node is correct before we put him into childNodes array.

    I wanted say anything here: http://www.sencha.com/forum/showthre...de-insertChild but tvanzoelenis right - function works well.

    So where is the problem? When we want insertChild at index, we will find siblingnode "buy lottery tickets". Then we continue and put our node before this siblindnode. I still didnt see any problem. When i check sourcecode at http://docs.sencha.com/ext-js/4-1/so...od-insertChild i didnt find anything bad too.

    What we see in tree view? Correct output. So where is the problem?

    When you delete same node twice, you will see the change finally.

  2. #2
    Sencha Premium Member Zdeno's Avatar
    Join Date
    Nov 2009
    Location
    Prague
    Posts
    401
    Vote Rating
    17
    Zdeno will become famous soon enough

      0  

    Default


    Anybody found a way how to restore nodes? Cheers.

  3. #3
    Sencha Premium Member Zdeno's Avatar
    Join Date
    Nov 2009
    Location
    Prague
    Posts
    401
    Vote Rating
    17
    Zdeno will become famous soon enough

      0  

    Default


    *bump*