EQ Filter Get All Results

10 posts, 1 answers
  1. Ashleigh L
    Ashleigh L avatar
    113 posts
    Member since:
    Jun 2012

    Posted 05 Aug 2015 Link to this post

    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.

  2. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 07 Aug 2015 Link to this post

    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!
     
  3. Kendo UI is VS 2017 Ready
  4. Ashleigh L
    Ashleigh L avatar
    113 posts
    Member since:
    Jun 2012

    Posted 17 Aug 2015 in reply to Daniel Link to this post

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

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

  5. Answer
    Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 19 Aug 2015 Link to this post

    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!
     
  6. Ashleigh L
    Ashleigh L avatar
    113 posts
    Member since:
    Jun 2012

    Posted 19 Aug 2015 in reply to Daniel Link to this post

    Perfect, thanks Daniel!
  7. Ashleigh L
    Ashleigh L avatar
    113 posts
    Member since:
    Jun 2012

    Posted 24 Aug 2015 in reply to Daniel Link to this post

    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).

  8. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 26 Aug 2015 Link to this post

    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!
     
  9. Ashleigh L
    Ashleigh L avatar
    113 posts
    Member since:
    Jun 2012

    Posted 26 Aug 2015 in reply to Daniel Link to this post

    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.
  10. Ashleigh L
    Ashleigh L avatar
    113 posts
    Member since:
    Jun 2012

    Posted 26 Aug 2015 in reply to Ashleigh L Link to this post

    Wait, how does this get applied to the datasource?
  11. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 28 Aug 2015 Link to this post

    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!
     
Back to Top
Kendo UI is VS 2017 Ready