openui5 icon indicating copy to clipboard operation
openui5 copied to clipboard

sap.m.Tree expand tree by node property

Open Icydillic opened this issue 8 years ago • 5 comments

Hello,

I am new to openUI5 and currently stuck at a relatively easy task but I am not able to find a suitable solution for it. I have a sap.m.Tree with standard tree items and I simply want to expand it to a certain node by a given node id. I have provided a working sample with static tree.expand(int) calls. Does someone has any idea how I can retrieve the item index of a tree item by a node property?

And another problem I see is the indentation. It does not decrease when expanding the tree over the tree expand methods. Is this intended or a bug?

OpenUI5 version: 1.56.6

Browser/version (+device/version): Chrome / 70

URL (minimal example if possible): https://next.plnkr.co/edit/MbaTFESRbWYebpKV

Any help is appreciated! Thank you very much and best regards, Stefan

Icydillic avatar Nov 14 '18 14:11 Icydillic

Hello,

The current index of an item can be retrieved from Tree.prototype.indexOfItem(item). However keep in mind that this index changes, as the nodes are added dynamically to the items aggregation when the tree nodes get expanded.

If you need to expand the tree to a given node, you should get to the top most node and expand it first, then expand the next node in the chain and so on, until you reach the needed node. Once you have an instance of an node, you can get its parent (TreeItem.getParentNode method) and retrieve the index of the parent with the first method (indexOfItem). Then you can call the expand method with this index as argument.

About the second issue, I can't reproduce issue with the indentation. Could you attach a screenshot? Also on what device are you testing?

Thanks and kind regards, Radostina

radostinaTs avatar Nov 15 '18 10:11 radostinaTs

Hello Radostina,

thank you very much for your reply. So the getItems result depends on the expanded nodes in the tree. That explains why I only got one item. I never expanded the tree. That is really not obvious. I will give it a try with the "find node -> expand node -> find next node -> expand next node ...." method.

The indentation is easy to reproduce.

  1. Open my sample in a fresh browser window
  2. Click the button "Expand To Node1-1-2-1-1"
  3. Now close the expanded node Node1-1-2-1 and open it again

Thank you very much and best regards, Stefan

Icydillic avatar Nov 15 '18 12:11 Icydillic

Hey Radostina,

That did the trick. That should really be documented. 😄 I updated my sample and provided a second expand method that expands dynamically. Thank you for your help!

Best regards, Stefan

Icydillic avatar Nov 15 '18 14:11 Icydillic

Hi Stefan,

Thanks for the update! :)

I've created an internal incident 1880729416 for the indentation issue. The status of the issue will be updated here in GitHub.

Kind regards, Radostina

radostinaTs avatar Nov 15 '18 14:11 radostinaTs

Hello Stefan @Icydillic,

to the first question, currently there is no API that support to expand to certain node.

With the current implementation, you can expand certain root node to certain level. If you bind Tree with ODATA, call the binding API oTree.getBinding("items").expandNodeToLevel(iIndex, iLevel). If you just have JSON data, you can do as Radostina suggested.

For the second issue, I will keep you updated once it's fixed.

Best regards, Wenqian

awui avatar Nov 15 '18 15:11 awui