1

I am creating an item selector with two boxes that move things back and forth within an extjs application. On the right box, I am creating buttons that serve to move items up and down. Essentially I am swapping the item with one above or below it. So, my code is straight forward in that regard

MoveUp: function(button, event, eOpts){
  var theChosen = Ext.getStore('storeId').getRootNode().findChild('text', 'Chosen folder');
  var chosenPanel = Ext.ComponenetQuery.query('#chosenTreePanel')[0];
  var selected = chosenPanel.getSelectionModel().getSelection();

  for( var i = 1; i < theChosen.childNodes.length; i++){
    if(Ext.Array.contains(selected, theChosen.childNodes[i]) && (!Ext.Array.contains(selected, theChosen.childNodes[i-1]){
      var temp = theChosen.childNodes[i];
      theChosen.childNodes[i] = theChosen.childNodes[i-1];
      theChosen.childNodes[i-1] = temp;

    }

  }
}

All of this code seems to work fine, because after clicking my button, and checking the DOM in firebug, I can see that the selected nodes have moved in the array correctly, however, this effect is never shown within my treepanel. ???How do I make the treepanel update when it's elements change. ???

TreePanel heirarchy looks like this just to clarify

Root Node 'Chosen Folder Node' Array of items I am moving up and down within the 'folder'

I am USING VERSION 4.0.7

Attempting to use replaceChild() to fire an event to rerender does not behave as I expected

Changing:

var temp = theChosen.childNodes[i];
theChosen.childNodes[i] = theChosen.childNodes[i-1];
theChosen.childNodes[i-1] = temp;

To:

var temp = theChosen.childNodes[i];
theChosen.replaceChild(theChosen.childNodes[i-1], theChosen.childNodes[i]);
theChosen.replaceChild(temp, theChosen.childNodes[i-1]);

Results in odd behavior in which some nodes go missing. Certaintly not what I was looking for. Where am I going wrong here?

Tried the following code using the datachanged and (undocumented)refresh event:

Ext.getStore('storeId').fireEvent('datachanged', Ext.getStore('chosen') );
Ext.getStore('storeId').fireEvent('datachanged', Ext.getStore('chosen') );

This does not reload anything...

SOLUTION:

Use the insertChild method of nodeInterface....

I have noticed something strange in how insertChild works in that I need to change my index more based on moving up or down will explain with code below.

To move Up:

theChosen.insertChild( (i-1), theChosen.childNodes[i]);

To move down:

theChosen.insertChild( (i+2), theChosen.childNodes[i]);

Although the -1 vs +2 they both effectively move the item by one in the appropriate direction.

2 Answers 2

5

If you want to update the view of the nodes, I recommend using yourTree.getView().refresh();

But you can avoid that by using parentNode.insertChild(index, childNode); where index is where you want the node to show up and parentNode is the parent to the nodes you are reordering. ChildNode can be a config for a new node or any other nodeinterface that already exists. If the node does already exist and you use the insertChild method to insert it, it will automatically remove that node from whereever else it is.

So as you provided in your question in response to my answer, your code will work with something like (this is probably how I'd do it, but this is untested):

MoveUp: function(button, event, eOpts){
    var chosenPanel = Ext.ComponenetQuery.query('#chosenTreePanel')[0];
    var selectedNodes = chosenPanel.getSelectionModel().getSelection();
    for( var i = 0; i < selectedNodes.length; i++){
        var currentNode = selectedNodes[i];
        if(!currentNode.isFirst())
        {
            var parentNode = currentNode.parentNode;
            var newIndex = parentNode.indexOf(currentNode) - 1;
            parentNode.insertChild(newIndex, currentNode);
        }
    }
}
6
  • Will this take care of removing the otheri nstance of the node??
    – Bbb
    Sep 6, 2012 at 13:34
  • so node is not node here? the first node is the parentNode, and then how do I reference the second node if it has the same parent?
    – Bbb
    Sep 6, 2012 at 14:22
  • oh sorry, my code is confusing... it would be parentNode.insertChild(index, childNode); Where parent node is the parent of the nodes you want to reorder.
    – Reimius
    Sep 6, 2012 at 15:13
  • theChosen.insertChild((i-1), theChosen.childNodes[i]) seems to do the trick
    – Bbb
    Sep 6, 2012 at 15:20
  • I've made some notes in my question regarding how I solved the problem.
    – Bbb
    Sep 6, 2012 at 15:30
1

Edit:

Back to the the responsible event question... You need to fire the

'datachanged' 
'refresh'

Events on the store with the store as only param. That should cause a UI update. But please note that I just had a glimpse into the sourcecode and I am sure this can all be done much smarter. At least a DD solution for this exists already. If I find some time I my look into this again, but I guess you should be fine with these events for the first.

You never see anything cause you just do it without the appropriate methods that then fire the responsible events that cause rerender. Take a look at

There may also be more methods that can help on the Ext.data.NodeInterface. I recommend you to use these instead of doing it under hood without any responsible event fired.

In addition to my second comment (just a wild guess without knowing if that is exactly what you want):

MoveUp: function(button, event, eOpts){
    var target = Ext.getStore('storeId').getRootNode().findChild('text', 'Chosen folder');
    var selected = chosenPanel.getSelectionModel().getSelection();
    target.appendChild(selected);
}
8
  • I'll look into that, but if I hacked it a bit, is there a specific event I could fire, or a custom event that would initiate a re-render?
    – Bbb
    Sep 6, 2012 at 12:11
  • I tried the replaceChild method. See my edits to the question.
    – Bbb
    Sep 6, 2012 at 12:20
  • @Bbb If you append a node to another one using appendChild() this will automatically remove the moved node from the old parent node (if there is one) and then append it. As Far as I see you are moving nodes so use appendChild() on the target node and not replaceChild() It seems you are doing it the wrong way. All in all it seems totally complicated the way you doing this.. Am I wrong that you just moving the selected node to a new parent? You want need any arrays then. Tree(nodes) take care of the child and parent....
    – sra
    Sep 6, 2012 at 12:27
  • Let me explain what I am doing. All the items I am manipulating are under the same parent, and they never leave that parent. What I am attemping to do is move the selected items up or down, relative to its other sibling items. All the items I am manipulating are siblings of one another and they always will be. I am only trying to move them up or down relative to one another (but they will all stay under the same parent). Does that clarify it at all?
    – Bbb
    Sep 6, 2012 at 12:32
  • In your example you are adding an item under the parent. All of my items already exist under the particular parent. I just want to move them up or down, but only the ones that are selected. I accomplished it with my initial code, I just could not get it to show in the view.
    – Bbb
    Sep 6, 2012 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.