Larry Bergman
Larry Bergman

Reputation: 11

How to add a new node to a dijit.Tree

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

Upvotes: 1

Views: 4507

Answers (3)

Tech_Dummy
Tech_Dummy

Reputation: 51

store.newItem is giving an error (Uncaught Error: dojo.data.ItemFileReadStore: Invalid attribute argument. ). Could not really find a fix for this so far and my store does not have duplicate id's.

Hence i tried using model.newItem(newItem, parentItem). This works perfectly alright.

Thanks, Srilatha.

Upvotes: 2

Larry
Larry

Reputation: 21

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

Upvotes: 2

Fu Cheng
Fu Cheng

Reputation: 3395

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.

Upvotes: 2

Related Questions