cancel
Showing results for 
Search instead for 
Did you mean: 

Call XML View ui elements from controller

Former Member
0 Kudos

Hi,

I used to call and do operations on ui elements by accessing them from controller using below syntax.

this.getView().byId("txtid");

Most of the times , I am able to access UI Elements with above syntax and write validations as well but some times , it is not useful to read panel/tiles by using above syntax.

Are there any known restrictions/limitations in using above syntax?. Are there any alternative ways to call XML View UI Elements from controller,please suggest me.

Regards,

Koti Reddy

Accepted Solutions (1)

Accepted Solutions (1)

Qualiture
Active Contributor
0 Kudos

My (personal) preference is to *never* access a view element by it's ID if you can avoid it

Although the SAPUI5 framework isn't a strict MVC framework, I try to stick to the MVC principles as much as possible, and this means the Controller should not call/reference items in its View(s).

There are a few exceptions, but generally, using model binding you can almost always bypass the need for referencing controls by their ID's

Former Member
0 Kudos

Hi,

Thank you for your suggestion but If we work with xml views,The only way to access ui element is with their ID's if any validations needed.

I got other syntax as well with which I overcome d above issues.

sap.ui.getCore().byId("viewid").byId("txtid");

Regards,

Koti Reddy

Qualiture
Active Contributor
0 Kudos

I disagree. Even with XML views (I never use any other type of view myself) you can perfectly use validation functionality (either clientside or serverside) without referencing View elements from the controller

Just make sure you set your control's validation methods propery, and set the validation eventhandlers accordingly

Former Member
0 Kudos

We can do validation functionality without referencing view elements from controller by setting validation methods properly. To set data to UI Elements dynamically based on validations , we must call UI Elements from controller

Regards,

Koti Reddy

Qualiture
Active Contributor
0 Kudos

I have to disagree again

To set data to UI elements dynamically -- whether it's based on validations, eventing, data retrieval, etc -- it is Highly Recommended to use data binding anytime

former_member211279
Active Participant
0 Kudos

Hi Robin,

If I have a simple form and I want to make sure some field will not be blank, how can we validate it in the controller without getting the value from the UI Element?

I am using a PHP server as a backend with the following logic:

- In the view (form) when I click in the "insert" button I call a function (in the controller) that will do a AJAX post in my php, and to validate if all the fields are filled out I have (?) to get one by one (from the UI) in the controller.

Is there a easier/better way to do it? Can you provide us some example?

Thanks a lot!

Cheers,

Eduardo

Qualiture
Active Contributor
0 Kudos

Hi Eduardo,

Again, use the power of your model

If you need to check if all the fields are filled, you can do it in just a few lines of code, and there is no need -- I repeat, no need -- to get the value of the UI fields one by one:


//assuming your data is an object which is stored in your model as property '/mydata'

var obj = this.getView().getModel().getProperty("/mydata");

for(var prop in obj) {

    if(obj.hasOwnProperty(prop)) {

        if(!obj[prop]) {   // property is undefined, null, or empty string

            // in here, do whatever you need to raise a flag for the empty/null value

        }

    }

}

I hope you'll see the benefits are obvious: No need to reference View elements (since their ID's may change, are dynamically generated, etc), your code is cleaner and easier to read, etc

No offence to anyone here, but I might write a blog about this anytime soon since it strikes me as odd so many people neglect the power and use cases of model binding

Former Member
0 Kudos

Robin,

Thanks for explanation!!

Instead of calling UI Element , we should bind ui element data to model if we need to use UI element and access model data for validations always.

Regards,

Koti Reddy

former_member211279
Active Participant
0 Kudos

Hi Robin,

I am not offended at all and thanks a lot for your tips!

I am new in the SAPUI5/OpenUI5 world and I am trying to really learn/understand the best practices.

Please check my example here -> http://plnkr.co/edit/aBRvdM

I could not fully understand how to implement your tip in the controller.

Cheers,

Eduardo

Qualiture
Active Contributor
0 Kudos

Hi Eduardo,

I see you're not using a model right now.

I would advice you to look at chapter Model View Controller first to get a thorough understanding of the theory behind it, and then the chapter about Models and Data Binding which gives code examples and use cases

Hope this helps!

Answers (0)