This is a migrated thread and some comments may be shown as answers.

DataSource filtering not working

5 Answers 450 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Ollie
Top achievements
Rank 1
Iron
Ollie asked on 23 Apr 2021, 03:53 PM

I have this JSON that I am displaying in a kendo template. The format is as follows:

[
    {
        "Pending": [
            {"FieldA": "SomeValue", "FieldB", "SomeValue"},
            {"FieldA": "SomeValue", "FieldB", "SomeValue"},
            {"FieldA": "SomeValue", "FieldB", "SomeValue"},
            {"FieldA": "SomeValue", "FieldB", "SomeValue"},
            {"FieldA": "SomeValue", "FieldB", "SomeValue"}
        ],
        "Submitted": [
            {"FieldA": "SomeValue", "FieldB", "SomeValue"},
            {"FieldA": "SomeValue", "FieldB", "SomeValue"},
            {"FieldA": "SomeValue", "FieldB", "SomeValue"},
            {"FieldA": "SomeValue", "FieldB", "SomeValue"},
            {"FieldA": "SomeValue", "FieldB", "SomeValue"}
        ]
    }
]

 

It always return an array with 1 object that has 2 fields: Pending and Submitted. Now each field contains an array of objects shown above.

What I am trying to do is to filter Pending and Submitted thru Field A but its always nothing.

Here is my code:

var filtersArray = [
    {
        field: "Pending",
        operator: function(itemValue, value) {                           
            return itemValue && itemValue.find(function (item) {                               
                return item.FieldA == value;
            });
        },
        value: "searchTextHere"
    },
    {
        field: "Submitted",
        operator: function(itemValue, value) {                           
            return itemValue && itemValue.find(function (item) {                               
                return item.FieldA == value;
            });
        },
        value: "searchTextHere"
    }
];
 
var filter = {
    logic: "or",
    filters: filtersArray
};
 
formDataSource.filter(filter);

5 Answers, 1 is accepted

Sort by
0
Ollie
Top achievements
Rank 1
Iron
answered on 23 Apr 2021, 03:55 PM

Sorry, I accidentally hit the submit button and I can't find an Edit one.

My solution above always return/display nothing.

Anyways, i patterened my solution from here https://www.telerik.com/forums/filter-fields-from-nested-json-object

Thanks in advance!

Ollie dG.

 

0
Ollie
Top achievements
Rank 1
Iron
answered on 23 Apr 2021, 04:23 PM
Correction again, the solution above always returns the original data and its not being filtered.
0
Nikolay
Telerik team
answered on 28 Apr 2021, 01:36 PM

Hello Ollie,

The DataSource filtering works on the dataItem level. In the current case, we have only one data item:

{
      "Pending": [
              ...
       ],
      "Submitted": [
              ....
       ]
  },

And if we filter Pending.FieldA to match only "searchTextHere" dataSource.view() will return only the above dataItem as it passes the filter. However, if we have 2 dataItems and only one matches the filter criteria the filter will return only one dataItem:

Let me know if you have any questions.

Regards,
Nikolay
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Ollie
Top achievements
Rank 1
Iron
answered on 28 Apr 2021, 02:42 PM

Hi Nikolay,

I understand now that it will return the whole dataItem that passed the filter, that is why when I was filtering, it always return the original data (because the dataItem passed the filter).

Is there anyway or anything that you can suggest to achieve what I am trying to do?

Given this

[
    {
        "Pending": [
            {"FieldA": "SomeValue1", "FieldB", "SomeValue"},
            {"FieldA": "SomeValue2", "FieldB", "SomeValue"},
            {"FieldA": "SomeValue3", "FieldB", "SomeValue"},
            {"FieldA": "SomeValue4", "FieldB", "SomeValue"},
            {"FieldA": "SomeValue5", "FieldB", "SomeValue"}
        ],
        "Submitted": [
            {"FieldA": "SomeValue1", "FieldB", "SomeValue"},
            {"FieldA": "SomeValue2", "FieldB", "SomeValue"},
            {"FieldA": "SomeValue3", "FieldB", "SomeValue"},
            {"FieldA": "SomeValue4", "FieldB", "SomeValue"},
            {"FieldA": "SomeValue5", "FieldB", "SomeValue"}
        ]
    }
]

and If I filtered FieldA with "SomeValue3", the result will be

[
    {
        "Pending": [
            {"FieldA": "SomeValue3", "FieldB", "SomeValue"}
        ],
        "Submitted": [
            {"FieldA": "SomeValue3", "FieldB", "SomeValue"}
        ]
    }
]
Thanks!

 

0
Nikolay
Telerik team
answered on 03 May 2021, 12:41 PM

Hi Ollie,

I am afraid, what you are trying to achieve is not possible with the built-in DataSorce functionality. The DataSource filters the data items and not the arrays that are set as properties to the data items.

I can suggest extracting the data of the data source, traverse it and get the data that matches the desired value.

Regards,
Nikolay
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Data Source
Asked by
Ollie
Top achievements
Rank 1
Iron
Answers by
Ollie
Top achievements
Rank 1
Iron
Nikolay
Telerik team
Share this question
or