Skip to Content
0
Former Member
Mar 04, 2015 at 07:20 PM

Databinding to enabled property on SAPUI5 button

1486 Views

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