Skip to Content
author's profile photo Former Member
Former Member

Databinding to enabled property on SAPUI5 button

Hey guys,

Looking for thoughts / best practices here... I have in my mind a design for data binding to a button's enabled property. Basically, I want to have the save button enabled when there are pending changes in the json model data, which are identified by an object's STATUS field being set (to "I", "D", or "C").

I realize I could just manually call btnSave.setEnabled(true) in all the places where I am changing the STATUS value, but it seems like it would be cleaner and potentially save on maintenance down the road to be able to bind the save button to be enabled whenever any rows have a status set. However, what I'm running into is that the formatter function gets evaluated when the table is initially created, but never gets reevaluated as the records in the model change.

What do you think... am I leaving out a key step, or is this approach not so great to begin with?

The data is just an array of records with a dozen or so simple properties.

Here's the save button (wiithin a toolbar) in the .view.js:

rightItems: [new sap.ui.commons.Button("btnDsnSave", {
                            icon: "sap-icon://save",
                            tooltip: "Save Changes",
                            lite: true,
                            style: sap.ui.commons.ButtonStyle.Accept,
                            press: oController.onBtnDsnSavePress,
                            enabled: {
                                path: '/',
                                formatter: oController.getDoChangesExist
                        }), ....

Here's the getDoChangesExist function in the controller:

getDoChangesExist: function() {
        return thisController &&
              thisController.getData().some(function (r) { return r.STATUS == 'I' || r.STATUS == 'C' || r.STATUS == 'D' });
 getData: function() {
        if(oTable && oTable.getModel()){
        return oTable.getModel().oData.modelData;
        return [];

Then, also in the controller, I have methods for inserting, deleting, and editing rows. Here is the event for the delete button press, which applies to selected rows in the table:

onBtnDsnDeletePress: function (oEvent) {
      if (oTable) {
                var aSelectedRows = thisController.getSelectedRows();

               aSelectedRows.forEach(function(r) {
                    r.SELECTED = false;
                    r.STATUS = 'D'; //Deleted
Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Best Answer
    Posted on Mar 04, 2015 at 07:40 PM

    What does some function do?

    If you have a json model why dont define a variable which will return enable flag which you can bind to property

    Object.defineProperty(jsonData, "isSaveEnabled", {

    get: function () {

    return $.grep(this.<ACTUAL_DATA>, function (r, idx) {

    return return r.STATUS == 'I' || r.STATUS == 'C' || r.STATUS == 'D';





    enabled: "{/isSaveEnabled}"

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Maksim Rashchynski

      Brilliant Maksim. That works marvelously.

      Some things to point out for future reference for anyone:

      • Make sure you call the Object.defineProperty before binding the model to the table, or you get weird one-off results where the evaluation you expect happens one change after the corresponding action taken.

      • Call table.getModel().refresh() after data changes, which causes the getter to be reevaluated

      So this was the final code inside the success call back on the

      var data = {modelData:};
      var jsonModel = new sap.ui.model.json.JSONModel(data);
      Object.defineProperty(data, "isSaveEnabled", {
           get: function () {
                return  this.modelData.some(function(r) {return r.STATUS == 'I' || r.STATUS == 'C' || r.STATUS == 'D'});

      Thank you very much for your help!

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.