2
votes

I'm working with the primefaces treetable and I'm trying to create buttons on each row so that a user can reorder the elements in a tree table.

For example, if i have a tree table with 3 nodes all at the same level, and each of those three nodes in the treetable has an "Up" button, if the up button is clicked on one of the nodes, I want that node to move up (basically reorder the nodes).

How can i get the index of of the selected node?

for(int i = 0; i < selectedNode.getParent().getChildren().size(); i++) {

    // how can i compare the index of the current child in the loop to the selected child?
}
2
Create a custom class extending TreeNode having an index?siebz0r

2 Answers

1
votes

It's not clean, but you could

  1. Using the selection attribute on the table, get a handle on the selected node in your backing bean and then

  2. Using the indexOf method on your backing (i'm assuming) List, get the index of the item. The order of dynamically generated databound items of primefaces generally follows the index/ordering of backing collections, so you can trust the indexing.

Alternatively, you could try the <p:ajax/> component to set the selected option and do as above.

0
votes

U can get selected node index using the method "getRowKey()"

Perhaps:

selectedNode.getRowKey()

And, if U want count nodes size U can to use method "getChildCount()", perhaps:

selectedNode.getParent().getChildCount()