cancel
Showing results for 
Search instead for 
Did you mean: 

this.getView().byId vs sap.ui.getCore().byId

Former Member

Hello!

I don't understand difference between subject method.

If I created XML-view, then work method this.getView().byId.

However, if I try to create JS-view, then this method does not work properly. It return unknow value. If I replace method with sap.ui.getCore().byId, then I received requested control by Id.

What's the difference?

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

I understood method difference.
this.getView().byId return element by relative ID.

sap.ui.getCore().byId  return element by absolute ID.


I inspected html code of created control.


If I create element with id='detail' in view "idMain1", then

1) Element definition in XML view is


<div id="idMain1--detail" class="sapMNavItem sapMPage sapMPageBgStandard sapMPageWithHeader" data-sap-ui="idMain1--detail">

i.e.: id equal view_id--element_id

2) Element definition in JS view is


<div id="detail" class="sapMNavItem sapMPage sapMPageBgStandard sapMPageWithHeader" data-sap-ui="detail">

My new question is: How to define control in JS-view, if I want its id like id in XML-view?

Best regards.

Former Member
0 Kudos

Hi Ivan,

not sure if I understood you correctly, but when you - for example - want an textfield with an specific ID, you can create it like this:


var tf = new sap.ui.commons.TextField("myCustomId");

In general, the first parameter of an control is always the ID. It's optional, so if you don't specifiy it, the ID will be generated by SAPUI5.


Greets,

ben

Former Member
0 Kudos

Hi Ben!

Yes, I created control with id "detail".

This is a XML view control definition


<Page

                id="detail"

                navButtonTap="onBack"

                showNavButton="true"

                title="Order" >

This is a JS view control definition


var oDetailPage = new sap.m.Page("detail",{

            title: "Orders Items",

            content: [oList],

            headerContent: []

        });

       

But SAPUI5 framework correct it in XML view using upper view id (idMain1) as prefix. How I can receive same result with JS view?

BR

Former Member
0 Kudos

Hi Ivan,

I'm not sure, but I don't think that you can change the way how the framework assigns the ID's.

So I guess the only way to get the same result in the JS view would be to set the desired ID "by hand".

But usually it's best practice to let the framwork handle the ID's instead of setting them manually, so you are on the save side and don't get into duplicate ID issues.

Greets,

ben

former_member182372
Active Contributor
0 Kudos
  1. var oDetailPage = new sap.m.Page( this.getView().createId("detail"),{ 
  2.             title: "Orders Items"
  3.             content: [oList], 
  4.             headerContent: [] 
  5.         }); 
Former Member
0 Kudos

Thank You very much,

Maksim!

Answers (0)