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

EQ Filter Get All Results

9 Answers 39 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Ashleigh L
Top achievements
Rank 1
Ashleigh L asked on 05 Aug 2015, 05:33 PM

I've got a page that displays courses, which can have tags. For example, my "Javascript" course may have the tag "web". I want to filter the datasource based on the course tags, but if no tag value is passed through (ie. blank string), I want it to ignore the filter.

Right now what's happening is that passing through a "eq" filter w/ an empty string is pulling back only courses that don't have tags - which does make sense. But if I don't pass through a specific tag, I need to get all courses, tagged or not. I also can't switch from "eq", since it has to be a full match, if a tag is passed in. 

Appreciate any help here.

9 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 07 Aug 2015, 11:21 AM
Hello,

How are you filtering the dataSource? If you are using the filter method then you could check if the value is empty string and clear the filter.

Regards,
Daniel
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Ashleigh L
Top achievements
Rank 1
answered on 17 Aug 2015, 02:53 PM

I've got it set up on the datasource config right now:

filter: {          
    field: "Tag",
    operator: "eq",
    value: filter_field
}

0
Accepted
Daniel
Telerik team
answered on 19 Aug 2015, 11:53 AM
Hi,

You can also use a condition in this case:
filter: filter_field ? {
    field: "Tag",
    operator: "eq",
    value: filter_field
} : []


Regards,
Daniel
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Ashleigh L
Top achievements
Rank 1
answered on 19 Aug 2015, 02:04 PM
Perfect, thanks Daniel!
0
Ashleigh L
Top achievements
Rank 1
answered on 24 Aug 2015, 04:16 PM

Is it possible to do the same check w/ multiple filters? I've tried this:

filter: {
    logic: "and",
    filters: [
        filter_val ? { 
            field: "Tag",
            operator: "eq",
            value: filter_val
        } : [],
        search ? { 
            field: "Name",
            operator: "contains",
            value: search
        } : [] 
    ]
}

Where "search" is the value of a search field (ie. looking for matches in a huge list based on an item's name), but it always returns zero results (the function populating the datasource does return data, so I know it's the filter).

0
Daniel
Telerik team
answered on 26 Aug 2015, 01:29 PM
Hi,

You should check the values before adding them to the array:
var filter = {
    logic: "and",
    filters: []
};
if (filter_val) {
    filter.filters.push({
        field: "Tag",
        operator: "eq",
        value: filter_val
    });
}
if (search) {
    filter.filters.push({
        field: "Name",
        operator: "contains",
        value: search
    });
}

The filters array should contain only valid filters.

Regards,
Daniel
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Ashleigh L
Top achievements
Rank 1
answered on 26 Aug 2015, 03:40 PM
Which is what I had asked for originally, but you suggested the condition instead. If this is the more "correct" way to do it, I'll switch. Thank you.
0
Ashleigh L
Top achievements
Rank 1
answered on 26 Aug 2015, 03:40 PM
Wait, how does this get applied to the datasource?
0
Daniel
Telerik team
answered on 28 Aug 2015, 09:47 AM
Hello,

I am not sure if I understand the question but a condition is used in both cases: You can apply the filter by setting the result to the dataSource:
var filter = {
    logic: "and",
    filters: []
};
if (filter_val) {
    filter.filters.push({
        field: "Tag",
        operator: "eq",
        value: filter_val
    });
}
if (search) {
    filter.filters.push({
        field: "Name",
        operator: "contains",
        value: search
    });
}
 
var dataSource = new kendo.DataSource({
    filter: filter,
    ...
})


Regards,
Daniel
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Data Source
Asked by
Ashleigh L
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Ashleigh L
Top achievements
Rank 1
Share this question
or