cancel
Showing results for 
Search instead for 
Did you mean: 

Read and modify data in OData V4 before binding to a control

0 Kudos

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 ?

Accepted Solutions (0)

Answers (1)

Answers (1)

mathias_uhlmann
Advisor
Advisor

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.

mathias_uhlmann
Advisor
Advisor

Since SAPUI5 1.70 public method v4.ODataListBinding#requestContexts exists and should be used. Please refer also to Accessing Data in Controller Code.