cancel
Showing results for 
Search instead for 
Did you mean: 

Databinding to enabled property on SAPUI5 button

Former Member
0 Kudos

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

                });

                oTable.getModel().refresh();

     }

}

Accepted Solutions (1)

Accepted Solutions (1)

former_member182372
Active Contributor
0 Kudos

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';

                     }).length>0;

                }

            });

....

enabled: "{/isSaveEnabled}"

Former Member
0 Kudos

array.some just returns true if the specified condition is true for any of the records in the array.  It's similar to .NET's linq "any" function if you're familiar with that.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

I will try what you posted, looks promising.

former_member182372
Active Contributor
0 Kudos

dont use grep in my example, use some 😉 some is more effecient, it will stop at first match, grep will return all matches

Former Member
0 Kudos

I'm not sure if I've added that call to the right place or not.  I put it in the oData read success callback, and it doesn't seem to hit the 'get' ever.  The button is always enabled.

Modified btnDsnSave to include:


enabled: "{/isSaveEnabled}"

Then modified the data pull as such:


oModel.read(dynamicUri, {

        async: true,

        success: function (oData, response) {

      var data = response.data.results;

        var jsonModel = new sap.ui.model.json.JSONModel();

        jsonModel.setData({modelData: data});

        utilities.getControlFromUI('btnDsnSave').setModel(jsonModel);

        oTable.setModel(jsonModel);

        oTable.bindRows("/modelData"); 

          Object.defineProperty(jsonModel, "isSaveEnabled", {

                        get: function () {

                            return  data.some(function(r) {return r.STATUS == 'I' || r.STATUS == 'C' || r.STATUS == 'D'});

                        }

                    });

    

        oTable.setBusy(false);

},

        error: function(oError){ .... } ....

But like I said, I put a break point inside the get of the Object.defineProperty() and it's never getting there as far as I can tell.   Any ideas?

former_member182372
Active Contributor
0 Kudos
  1.   var data = {modelData: response.data.results}
  2.        
  3.           Object.defineProperty(data, "isSaveEnabled", { 
  4.                         get: function () { 
  5.                             return  this.modelData.some(function(r) {return r.STATUS == 'I' || r.STATUS == 'C' || r.STATUS == 'D'}); 
  6.                         } 
  7.                     }); 


  8. var jsonModel = new sap.ui.model.json.JSONModel(data); 
  9.        
Former Member
0 Kudos

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 oModel.read:


var data = {modelData: response.data.results};

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'});

     }

});

      

oTable.setModel(jsonModel);

oTable.bindRows("/modelData");

Thank you very much for your help!

Answers (0)