cancel
Showing results for 
Search instead for 
Did you mean: 

Filter a Json Model for a particular value

former_member193103
Participant
0 Kudos

Hi Experts,

I am trying to show a list on the basis of some particular records entered in the input field. I want to filter the json model on the basis of that input fields. My input field is in first view and the list is in second view. I am using new sap.ui.model.Filter, when I use hard coded value I get the output but I want the output for the value in the input field. Can anyone please show me how can I do that?

Thanks

Himadri

Accepted Solutions (0)

Answers (2)

Answers (2)

former_member182862
Active Contributor
0 Kudos

Hi

This is what you are looking for ?

Example

-D

former_member193103
Participant
0 Kudos

Hi

Thank you for your reply. In your example the input field is working as a search field and it is on the same page. But in my code the input field is in another page and the list is in another page.  For example if I enter my name in the input field and press submit button then a next page will come which shows only my information.

Thanks

former_member182862
Active Contributor
0 Kudos

Hi

The concept is the same though. Maybe you can explain more about the problems that you have encountered. Then we can help. I cannot simulate next page in jsbin. this is what I can do at most.

Example

-D

former_member193103
Participant
0 Kudos

Hi,

When I run the application all the views are loaded initially and then on the first view I enter some value in the input field and press the submit button,then it shows me the initially loaded second view which is not filtered.Is it possible that the second view is loaded after I press the submit button or can I get the input field value before I use the filter ???


I have coded,


In my First view


createContent : function(oController) {

  jQuery.sap.require("sap.ui.model.FilterOperator");

  var input = new sap.m.VBox({

  items: [

                     new sap.m.HBox("hb",{

                      alignItems : sap.m.FlexAlignItems.Center,

                      items: [

                      new sap.m.Label({

                      text:"Start Date",

                      width:"140px",

                    

                      }),      

                    

                         new sap.m.Input("Start",{

                      type: sap.m.InputType.Text,

                    

                        placeholder : "Start Date"

                

                     }),

                     ]

                     }),

                    

                     new sap.m.HBox("hb1",{

                      alignItems : sap.m.FlexAlignItems.Center,

                      items: [

                      new sap.m.Label({

                      text:"End Date",

                      width:"140px",

                    

                      }),      

                    

                          new sap.m.Input("End",{

                      type: sap.m.InputType.Text,

                    

                        placeholder : "End Date"

                

                      }),

                      ]

                      }),

                     

                      new sap.m.Button({

            

              text : "Submit",

              icon: "sap-icon://submit",

            

              press: function(){

              app = sap.ui.getCore().byId("myApp"); 

                            app.to("idPopup1");

              }

                      })

                    

                     ]

  });

  return new sap.m.Page({

  title: "Title",

  content: [

  input

  ]

  });

  }

In my second view

createContent : function(oController) {

  jQuery.sap.require("sap.ui.model.FilterOperator");

  var oModel = new sap.ui.model.odata.ODataModel(

    "proxy/http/########.com:8000/sap/opu/odata/sap/ZUSERINFO_VER_SRV/",

    true,"######","#######");

  sap.ui.getCore().setModel(oModel,"model");

  var tab = new sap.m.List("list",{

  

       headerText: "Issue List",

       items: {

           path: "myModel>/Form_Responses",

           //sorter : new sap.ui.model.Sorter("FIRSTNAME"),

           filters: new sap.ui.model.Filter("User_Id", sap.ui.model.FilterOperator.EQ, window.str) ,

           template: new sap.m.ObjectListItem({

             title: "{myModel>First_Name}",

             number: "{myModel>User_Id}",

             type: sap.m.ListType.Active,

             press : function (oEvt) {

            

              context = oEvt.getSource().getBindingContext('myModel');

                 selectedIndex = context.sPath.split("/")[2];

                 console.log(window.context);

                 ID = context.oModel.oData.Form_Responses[selectedIndex].User_Id.toString();

                 First = context.oModel.oData.Form_Responses[selectedIndex].First_Name;

                 Last = context.oModel.oData.Form_Responses[selectedIndex].Last_Name;

              Phone = context.oModel.oData.Form_Responses[selectedIndex].Phone_No.toString();

              Email = context.oModel.oData.Form_Responses[selectedIndex].Email;

              Country = context.oModel.oData.Form_Responses[selectedIndex].Country;

              Time = context.oModel.oData.Form_Responses[selectedIndex].Timestamp;

            

              var oParameters = ({

              "UserId" : ID,

              "FirstName" : First,

              'LastName' : Last,

              'Phone' : Phone,

              'Email' : Email,

              'Country' :Country,

              });

              console.log(oParameters);

            

              var oEntry = {};

              oEntry.UserId = ID;

              oEntry.FirstName = First;

              oEntry.LastName = Last;

              oEntry.Phone = Phone;

              oEntry.Email = Email;

              oEntry.Country = Country;

              oModel.create('/Form_Responses', oEntry, null, function(oResponse){

              alert("Create successful");

                    console.log(oResponse);

                    },

                   

                    function(){

                    alert("Create failed");

                    });

       },

      

                   firstStatus : new sap.m.ObjectStatus({

                       text : "{myModel>Timestamp}", 

                       state : "Success"

                     }),

              

                     attributes : [

                       new sap.m.ObjectAttribute({

                         text : "{myModel>Last_Name}"

                       }),

                      

                      

                     

                       ]

                 })

         }

       });

  var but = new sap.m.Button({

  text : "Approve",

  press: function(){

  app = sap.ui.getCore().byId("myApp"); 

                app.to("idsec1");

  }

  });

return new sap.m.Page({

  title: " mGovernance app",

  showNavButton: true, 

            navButtonTap:function(){ 

                           app = sap.ui.getCore().byId("myApp"); 

                           app.to("idfirst1"); 

                          

            },

  content: [tab,but]

  });

  }

in the second cotroller

onBeforeRendering:
function() {
    //jQuery.sap.require("sap.ui.model.FilterOperator");
//var str =  sap.ui.getCore().byId("Start").getValue();
//console.log(str);
    var url = "https://script.google.com/macros/s/AKfycbzZt9afgxTa6EacqMA2SbSU8TZ_gM93e1jTFs3U4GquU-J8wHvt/exec?id=...";
var model = new sap.ui.model.json.JSONModel();
$.ajax({
  url : url,
            dataType: "jsonp",
            type: "GET",
            success: function(data,textStatus,jqXHR) {
            data = JSON.parse(data);
            console.log(data);  
            model.setData(data);

            },
            error: function (data) {
                console.log("error");
                //model.setData(data);
            },
         
      });

  sap.ui.getCore().setModel(model,"myModel");
 
 
   
 
},

Thanks

former_member184238
Participant
0 Kudos

Hi,

We can use oData.read operation to get the filtered output based on input values.

For example In my service I have to pass the Material Number dynamically to get the output from the service.

Now, I have one input filed to pass the Material Number and one Submit button.

When click on submit button in Controller I am getting the value of the Material Number and pass it to the URL like below.

search : function(oEvent) {

var input = sap.ui.getCore().byId("userinput"); // Id of the Input field in View File

  var MaterialNumber = input.getValue();

   OData.read({ requestUri: "http://xxxx:8000/sap/opu/odata/sap/ZMSTOCK_ODATA_SRV/Materials?$filter=Materialumber eq '"

                              + MaterialNumber + "' "},

  function (data) {

              console.log(data);

      }

  );    

}

In Console we can get the filtered data.

For mobile application development register into Open.sap.com and follow the below link.

Course: Introduction to Mobile Solution Development for the Enterprise

Hope It will helpful to you.

Regards

Sridevi

former_member184867
Active Contributor
0 Kudos

You can try

new sap.ui.model.Filter("PROPERTY", sap.ui.model.FilterOperator.EQ, sap.ui.getCore().byId('ID OF INPUT FIELD FROM WHICH YOU WANT TO PICK UP FILTER VALUE').getValue());
former_member193103
Participant
0 Kudos

I am using the same code. But am not getting the required output. I think before I enter value in the input field the second view is loaded and it is showing that result.

Can you please show me how can I get the value of the input field (which is in the first view) in the second view before it is loaded??

Thanks

Himadri

former_member184867
Active Contributor
0 Kudos

method onInit() and onBeforeRendering() can be useful

former_member193103
Participant
0 Kudos

can u please show me some example.