on 03-04-2015 7:20 PM
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();
}
}
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}"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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?
Brilliant Maksim. That works marvelously.
Some things to point out for future reference for anyone:
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!
User | Count |
---|---|
93 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.