Reputation: 19294
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?
}
Upvotes: 2
Views: 5101
Reputation: 1
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()
Upvotes: 0
Reputation: 20691
It's not clean, but you could
Using the selection
attribute on the table, get a handle on the selected node in your backing bean and then
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.
Upvotes: 1