Catfish
Catfish

Reputation: 19294

Primefaces TreeTable - how to get the index of the selected node?

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

Answers (2)

Szuja
Szuja

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

kolossus
kolossus

Reputation: 20691

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.

Upvotes: 1

Related Questions