Skip to Content
avatar image
Former Member

How to pass the data from database(ODATA) to Json model for using list

Hi all,

This is bala,Currently i am working in a project,In this project i am using Search field here i am getting data from odata(Back end),i need to get the data from database and put that in JSON model.then only i can use the search function.i need code to put the data in Json which is coming from ODATA

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • Best Answer
    avatar image
    Former Member
    Oct 29, 2015 at 04:39 AM

    Hi all,

    This is working fine.and thank you for your response,

    The code is

    In xml view put entity set as "A>/List/results",

    onInit : function(){

      oController=this;

      oController2=this;

      var that=this;

      oController.oData1 = new sap.ui.model.odata.ODataModel ("url",true);

      var fnSuccess=function(oData,oResponse)

      {

                 var jsonArray ={

                "List":oData

                 } 

               

    var oJSONModel = new sap.ui.model.json.JSONModel();

    sap.ui.getCore().setModel(oJSONModel);

    oJSONModel.setData(jsonArray);

    oController.getView().setModel(oJSONModel,"A");

    }

      oController.oData1.read("/MatSet ",null,null,true,fnSuccess)

    // oController.setModel(oJSONModel);

      },

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 26, 2015 at 12:12 PM

    Hi Bala,

    By using ODATA Model--->under Success Block--->

    var oModel = new sap.ui.model.odata.v2.ODataModel(sServiceUrl, null, username, passwrd,

    function(odata){

    var data = odata.results;

    //Now data has [{ "name":"1" }, {"name" : "2"}]

    });

    use this data in your JSON Model. if you want to change this structure.

    Use for loop to create your own structure.

    Check this: Simple OData Binding with SAPUI5 | SCN

    OR

    You can directly bind odata model to table/list.

    for eg:

    1. //navigation service 
    2. var sServiceUrl = "/sap/opu/odata/sap/ZT38MP_TM_HIER_SRV/"; 
    3. var oModel = new sap.ui.model.odata.v2.ODataModel(sServiceUrl, { useBatch : true }); 
    4.   
    5. oTable.setModel(oModel); 
    6.   
    7. //navigation service binding 
    8. oTable.bindRows({ 
    9. path : "/Nodes", 
    10. parameters : { 
    11. expand : "ChildNodes", 
    12. navigation : { 
    13. 'Nodes' : 'ChildNodes' 
    14. }); 


    Referred from: http://scn.sap.com/community/developer-center/front-end/blog/2015/10/23/treetable-odata-binding


    Thanks,

    Karthik A



    Add comment
    10|10000 characters needed characters exceeded

  • Oct 26, 2015 at 12:25 PM

    Bala

    jQuery.ajax({
      url: ' @Url.Action("Translate")',
      type: 'POST',
      contentType: "application/json",
      dataType: "json",
      data: JSON.stringify({ 'List': list }),
      traditional: true
    });

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 27, 2015 at 04:35 AM

    Hi Bala,


    try this code




    var oModelPriority = new sap.ui.model.odata.ODataModel("/xxxxxxxxxxxxxxxxxxxxxxxxxxxxx_SRV/");

    oModelPriority.read("/url ",null,null,true,function(oData,oResponse){

                var jsonArray = [];

       jsonArray = oResponse.data.results;

       var oJSONModel = new sap.ui.model.json.JSONModel();

    oJSONModel.setData(jsonArray);

    olist.setModel(oJSONModel); (assuming  olist is the control name ur using)

    if any doubts after using this code post your code.

    Thanks,

    Venkat

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      oController.oData1 = new sap.ui.model.odata.ODataModel ("odata/sap/ZMATERIAL_ODATA_SRV",true);

      //

        var fnSuccess=function(oData,oResponse)

        {

        var jsonArray = [];

        jsonArray = oResponse.data.results;

        alert(oResponse.data.results[0].ID);

         

          oController.oModel = new sap.ui.model.json.JSONModel();

        oController.oModel.setData(jsonArray);

        

        oController.oModel.setModel(oController2.oModel,"A");

        }

        oController.oData1.read("/MatSet",null,null,true,fnSuccess);

       

        },

      this is my code in init function but in view .xml what to put for items(EntitySet)?

  • avatar image
    Former Member
    Oct 27, 2015 at 04:59 AM

    thank you all....😊

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 27, 2015 at 06:25 AM

    Why oController.oModel.setModel(oController2.oModel,"A"); ??

    Shouldn't you be writing just oController.setModel(oController2.oModel,"A");

    Add comment
    10|10000 characters needed characters exceeded