cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Apply Filter on JSON Model

Former Member
0 Likes
12,076

Hi,

I got a JSON model as follows

{

    "items": [

        {

            "name": "Test1,

            "description": "Description1"

        },

        {

            "name": "Test2",

            "description": "Description2"

        }

    ]

}

Binding:

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

oModel.loadData("./json/items.json",null,false);

Is there any way I can filter at this spot by name name=Test1

The reason is that I have form layout in which I want to only show this filtered value

Accepted Solutions (0)

Answers (4)

Answers (4)

venkatachala_ck
Active Participant
0 Likes

Hi,

checkout this link

Former Member
0 Likes

Hi,

Actually I had to loop accross to get the items and that worked with a charm. $grp was not working so i decided to loop it

former_member182372
Active Contributor
0 Likes

sorry, grep code has a problem, should be

oModel.attachEvent("requestCompleted", function() {

  var data = oModel.getData();

  data = $.grep(data.items, function(elementOfArray, indexInArra){ return elementOfArray.name === 'Test1' });

  oModel.setData( {items : data });

}, this);

Former Member
0 Likes

Hi Usman

you can test this

JS Bin - Collaborative JavaScript Debugging

Thanks

-D

Former Member
0 Likes

another example that you can do binding on the fly

JS Bin - Collaborative JavaScript Debugging

former_member182372
Active Contributor
0 Likes

there is a side effect of this approach, when you have to reaplly a filter, you would need to clean application filters

var binding = oList.getBinding("items");

binding.aApplicationFilters = [];

binding.filter(...);

Former Member
0 Likes

yes. good catch

corrected the jsbin

JS Bin - Collaborative JavaScript Debugging

Thanks

-D

former_member182372
Active Contributor
0 Likes

filter is a binding thing, not model. you can do grep once data is loaded

Former Member
0 Likes

Do you have example for such as I have a FormLayout where I am attaching this JSON Model

oFormLayout.setModel(oModel);

and here I want to put my filter so that Form Element should display only the filtered values from the JSON.

former_member182372
Active Contributor
0 Likes

what aggregation do you bind it to?

Former Member
0 Likes

this is complete code listing:

var oModelPrDetails = new sap.ui.model.json.JSONModel();
oModelPrDetails.loadData("./json/PR_Workitems.json",null,false);
var oGridLayout = new sap.ui.layout.form.ResponsiveGridLayout("PRIGridLayout");
var oFormLayout = new sap.ui.layout.form.Form("PRIFormLayout", {
layout: oGridLayout
})
var oFContainer = new sap.ui.layout.form.FormContainer("TaskData",{
title: new sap.ui.core.Title({
        text: "Task Information",
        tooltip: "Task Info"
})
});
var oFETask = new sap.ui.layout.form.FormElement("Tasks", {
label: "Task Information",
fields: new sap.m.Input({
type: sap.m.InputType.Text,
            value: "{name}",
})
});
oFormLayout.setModel(oModelPrDetails);
oFContainer.addFormElement(oFETask);
oFormLayout.addFormContainer(oFContainer);
former_member182372
Active Contributor
0 Likes

oModel.attachEvent("requestCompleted", function() {

  var data = oModel.getData();

  data = $.grep(data.items, function(elementOfArray, indexInArra){ return elementOfArray.name === 'Test1' });

  oModel.setData(data);

}, this);