Odata CRUD Operations In SAP UI5 - 1)Odata READ

firstpro.view.xml

===============================================

<mvc:View displayBlock="true" xmlns="sap.m"
xmlns:mvc="sap.ui.core.mvc"
controllerName="demoodatacrud.controller.firstpro"
xmlns:f="sap.ui.layout.form">


<Table id="idProductsTable"
        inset="false"
        items="{path:'Data>/'}">
        <columns>
            <Column>
                <Text text="Address" />
            </Column>
            <Column>
                <Text text="Contact" />
            </Column>
             <Column>
                <Text text="Customer" />
            </Column>
            <Column>
                <Text text="DOB" />
            </Column>
             <Column>
                <Text text="Name" />
            </Column>
            <Column>
                <Text text="Validfrm" />
            </Column>
             <Column>
                <Text text="Validto" />
            </Column>
            
        </columns>
        <items>
            <ColumnListItem>
                <cells>
                    <Text
                        text="{Data>Address}" />
                    <Text
                        text="{Data>Contact}" />
                         <Text
                        text="{Data>Customer}" />
                    <Text
                        text="{Data>DOB}" />
                         <Text
                        text="{Data>Name}" />
                    <Text
                        text="{Data>Validfrm}" />
                         <Text
                        text="{Data>Validto}" />
                    
                </cells>
            </ColumnListItem>
        </items>
    </Table>
<Button text="Say Hello!" press="onShowGetfirstPro" />
</mvc:View>



==========================================

firstpro.Controller.js

===============================================

sap.ui.define(
        [ "jquery.sap.global", "sap/ui/core/mvc/Controller",
           
            "sap/ui/model/json/JSONModel", "sap/m/Dialog", 'sap/m/Text',
            "sap/m/MessageToast", "sap/m/MessageBox",
            "sap/ui/core/format/NumberFormat",
            "sap/ui/model/Filter", "sap/ui/model/FilterOperator", 'sap/ui/core/util/Export',
            'sap/ui/core/util/ExportTypeCSV',
            'sap/m/MessagePopover','sap/m/MessagePopoverItem','sap/m/Button','sap/ui/model/ValidateException'

        ],
        function(jQuery, Controller, BaseController, JSONModel,Dialog,Text,
            MessageToast, MessageBox, formatter, NumberFormat,
            Filter, FilterOperator, ExportTypeCSV, MessagePopover,USER,
            MessagePopoverItem,Button,ValidateException)
        {
          "use strict"; 
          var oModel=new sap.ui.model.json.JSONModel();
            return Controller.extend("demoodatacrud.controller.firstpro", 
{
            onInit: function () 
            {
    this.getView().byId("idProductsTable").setModel(oModel,"Data");
    
            //var that=this;
            var iModel=this.getOwnerComponent().getModel("ZTEST_314_SRV"); 
            iModel.read("/ES_CustomerSet",
            {
            success: function(d, oResponse)
            {
            oModel.setData(d.results);
            },
/*error:function(oResponse)
{
}*/
           
           })
  },

//Create operation in odata

onShowGetfirstPro : function() {
// debugger;

var tabledata = [];

var iModel = this.getOwnerComponent().getModel("ZTEST_314_SRV");
console.log("hii");
var obj = {};
var Address = this.getView().byId("input1").getValue();
var Contact = this.getView().byId("input2").getValue();
var Customer = this.getView().byId("input3").getValue();
var DOB = this.getView().byId("input4").getValue();
var Name = this.getView().byId("input5").getValue();
var Validfrm = this.getView().byId("input6").getValue();
var Validto = this.getView().byId("input7").getValue();

obj = {
Address : Address,
Contact : Contact,
Customer : Customer,
DOB : DOB,
Name : Name,
Validfrm : Validfrm,
Validto : Validto
};

tabledata.push(obj);
iModel.create('/ES_CustomerSet', obj, {
success : function(oData, oResponse) {
// debugger;
alert('Record Created Successfully...');
},
error : function(err, oResponse) {
// debugger;
alert('Error while creating record - '
.concat(err.response.statusText));
}
});

},





  
  onBeforeRendering : function()
  {
/*   this.getView().byId("idProductsTable").setModel(oModel,"Data");*/
},
  
onShowGetfirstPro : function ()
{
console.log("hii");
}
});

});


=================================================

manifest.json

========================================

{
"_version": "1.1.0",
"sap.app": {
"_version": "2.0.0",
"id": "myapp",
"type": "application",
"title": "{{appTitle}}",
"i18n": "i18n/i18n.properties",
"description": "{{appDescription}}",
"applicationVersion": {
"version": "2.0"
},
    "dataSources": {
      "ZTEST_314_SRV": {
        "uri": "proxy/sap/opu/odata/sap/ZTEST_314_SRV/",
        "type": "OData",
        "settings": {
          "odataVersion": "2.0"
        }
      }
    }
},
"sap.ui": {
"_version": "1.3.0",
"technology": "UI5",
"deviceTypes": {
"desktop": true,
"tablet": true,
"phone": true
},
"supportedThemes": [
"sap_bluecrystal"
]
},
"sap.ui5": {
"_version": "1.2.0",
"rootView": {
"viewName": "demoodatacrud.view.firstpro",
"type": "XML",
"id": ""
},
"autoPrefixId": true,
"dependencies": {
"minUI5Version": "1.34",
"libs": {
"sap.ui.core": {
"minVersion": "1.34.0"
},
"sap.m": {
"minVersion": "1.34.0"
},
"sap.ui.layout": {
"minVersion": "1.34.0"
}
}
},
"models": {
"ZTEST_314_SRV": {
        "dataSource": "ZTEST_314_SRV",
        "settings": {
          "useBatch": false,
          "defaultUpdateMethod":"PUT"
        }
      }
      
},
"contentDensities": {
"compact": true,
"cozy": true
},
"routing": {
"config": {
"routerClass": "sap.m.routing.Router",
"viewType": "XML",
"viewPath": "DemoOdataCrud.view",
"controlId": "app",
"controlAggregation": "pages",
"bypassed": {
"target": [
"firstpro"
]
},
"async": true
}
}
}
}


======================================

Component.js

================================

jQuery.sap.declare("demoodatacrud.Component");
jQuery.sap.require("sap.ui.core.UIComponent");

sap.ui.define([
"sap/ui/core/UIComponent"
], function (UIComponent) {   
"use strict";

return UIComponent.extend("demoodatacrud.Component", {

metadata : {
manifest: "json",
},


});
});


================================================


in web.xml we need to set prefix url 

<context-param>
    <param-name>com.sap.ui5.proxy.REMOTE_LOCATION</param-name>
    <param-value>==== Here u will get prefix url just need to set here=</param-value>
  </context-param>

eg:---http://abcd.com:0000  here 0000 is port number   











Post a Comment

Previous Post Next Post