on 05-21-2019 10:25 AM
The problem for me is I want to massage (read & modify) the data to add additional values before I binding to a control Here is my complete code of oData V2:
this_.getOwnerComponent().getModel("myModel").read("/zprojects", {
"async": true,
"success": function (oData) {
var myArray = [];
var pos;
//Do some massages....
for (var i = 0; i < oData.results.length; i++) {
pos = myArray.map(function (e) {
return e.ID;
}).indexOf(oData.results[i].PROJECTID);
if (pos === -1) {
myArray.push({
ID: oData.results[i].PROJECTID,
PROJECT_DESC: oData.results[i].PROJECT_DESC
});
}
}
myArray.sort((a, b) => (a.PROJECT_DESC > b.PROJECT_DESC) ? 1 : -1);
myArray.unshift({
ID: "-1",
PROJECT_DESC: "Please select Project ID"
});
//Done..then bind to a control (setModel)
oModel = new sap.ui.model.json.JSONModel(myArray);
sap.ui.core.Fragment.byId("idFragment", "project").setModel(oModel);
},
"error": function (oError) {
console.log(oError);
}
});
I can do this very easily in OData V2, how do I achieve similar things with oData V4 ?
Update: 03.04.20: Please check the comment below which refers to public methods that are available in the meantime.
Hi Don,
currently, you will need to use the protected method v4.ODataListBinding.getContexts to fetch the records.
With model parameter autoExpandSelect:false:
function fillJSONModel(){
var oListBinding = oModel.bindList("/People", undefined, undefined, undefined, {$select:"UserName"});
oListBinding.getContexts(0, 10);
oListBinding.attachEventOnce("change", function (oEvent){
var aContexts = oListBinding.getContexts(0, 10),
oData = {
People:aContexts.map(oContext => oContext.getObject())
};
oData.People.unshift({UserName:"Add new user"});
oJSONModel.setData(oData);
});
}
With model parameter autoExpandSelect:true:
function fillJSONModel(){
var oListBinding = oModel.bindList("/People", undefined, undefined, undefined, {$select:"UserName"}),
bRefreshed;
function handleChange(oEvent){
var aContexts = oListBinding.getContexts(0, 10),
oData;
if (bRefreshed){
oData = {
People:aContexts.map(oContext => oContext.getObject())
};
oData.People.unshift({UserName:"Add new user"});
oJSONModel.setData(oData);
} else {
oListBinding.attachEventOnce("change", handleChange);
}
}
oListBinding.getContexts(0, 10);
oListBinding.attachEventOnce("change", handleChange);
oListBinding.attachEventOnce("refresh", function(oEvent){
oListBinding.getContexts(0, 10);
bRefreshed = true;
});
}
Best regards
Mathias.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Since SAPUI5 1.70 public method v4.ODataListBinding#requestContexts exists and should be used. Please refer also to Accessing Data in Controller Code.
User | Count |
---|---|
77 | |
8 | |
7 | |
6 | |
6 | |
6 | |
6 | |
6 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.