cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Get Text from a Tree node.

Former Member
0 Likes
970

Hi,

I am trying to change the example from the basic tree. I want to keep the title of the StandardTreeItem each time I expand a node (without selection).

The xml:

<Tree
   id="Tree"
   items="{path: '/'}"
   toggleOpenState="onToggle">
   <StandardTreeItem id="item" title="{text}"/>
 </Tree>

In the controller I have this:

onInit : function (evt) {
            // set explored app's demo model on this sample
            var oModel = new JSONModel(jQuery.sap.getModulePath("sap.m.sample.Tree", "/Tree.json"));
            this.getView().setModel(oModel);
        },

        onToggle: function(oEvent) {
            
            var data = this.getView().getModel().getData();
            var lItem = oEvent.getParameters().getBindingContext("data");
            console.log(lItem);

           },

When I am trying this, I am getting that the lItem is undefined. Any ideas about why or how can I fix this?

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

Hi, iliana what you're doing wrong is how you're reading the parameters. I would suggest you read the API reference of the toggleOpenState event before.

In your specific case to get the bindingContext of the opened item you can simply do:

        onToggle: function(oEvent) {
            var lItem = oEvent.getParameter("itemContext");
            console.log(lItem); // Item binding context
            console.log(lItem.getObject()); // this will print the JSON object inside the item you're binded to the tree
        }

Answers (2)

Answers (2)

former_member185280
Active Contributor

Try something like this:

var lItem = this.getView().getModel().getProperty(oEvent.getParameter("itemContext").getPath());

Regards,
Christian

SergioG_TX
SAP Champion
SAP Champion
0 Likes

the model is not ready at the time you are trying to read it... move your code from onInit to .. onAfterRender and try that way