views:

332

answers:

3

I want to add a new node to a dijit.ree as a sibling of the currently selected node. I've found sample code (I'm new to dojo) that adds a new item to the tree using the newItem method of ItemFileWriteStore, but the new item always appears at the bottom of the tree. How would I add to the store at a specified position, in particular the position corresponding to the current selection? Pointers to sample code would be welcome :)

Thanks, Larry

A: 

You need to find the parent item of current selected node and use that item as the parent of the newly created item.

store.newItem(itemObj, {parent : parentItem, attribute : children});

Normally an item in the store doesn't have a back pointer points to its parent. So you may need to maintain that yourself. For example, you can store the parent item's id in the child item and use fetchItemByIdentity to get the parent item.

Alex Cheng
A: 

Hi Alex, Thanks, that's a good start. I want the inserted node to be at a particular place wrt it's siblings. In other words, I don't want to simply insert as the new last child of the parent, instead I want to become a child that is inserted after the currently selected node. Any thoughts on doing that?

Thanks, Larry

Larry
A: 

I figured it out; here's the answer for future searchers. Use newItem as Alex suggested. Then use model.pasteItem to reposition the new item. pasteItem takes a parent (selectedNode.item.parent[0]) and a position (selectedNode.getIndexInParent()+1)

Larry

Larry