cancel
Showing results for 
Search instead for 
Did you mean: 

Get ID of element in runtime

Former Member
0 Kudos

Hi ,


I have a sap.m page with a chart that's is getting created dynamically by using add content in runtime to the page.

How can i get the chart ID in rutime ?

Thanks,

Arie.

Accepted Solutions (1)

Accepted Solutions (1)

saivellanki
Active Contributor
0 Kudos

Hi Arie,

If you are adding chart to the page content aggregation then it would be placed at the last element of content array.

Try something like this (Just a guess):


var oPage = sap.ui.getCore().byId("oPage");          //Get Hold of Page Control

var oContent = oPage.getContent();                         //Gets the Page Conent

var oLastElement = oContent.slice(-1)[0]                    //Fetches the last element in content aggregation, which will be chart that is created

var oChartID = oLastElement.getId();

console.log(oChartID);

Regards,

Sai Vellanki.

Answers (2)

Answers (2)

Former Member
0 Kudos

Got it by looping in oPage.mAggregations.content and extract "id" .


Your solution is great , thanks.

prashil
Advisor
Advisor
0 Kudos

Hi Arie,

When creating the content, you can use this.createId("idName"). This will automatically create an id for the element you are creating dynamically with namespace.

It is recommended that you should not use sap.ui.getCore().byId(), as this will find the content based on the id without namespace.

Instead you can use this.getView().byId(). This is recommended way of getting reference to the dynamically created content.

Thanks

Prashil Wasnik