Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kendo DataSource Filtering with multiple conditions

I want to build a filter for a datasource with multiple condition. I found this thread (HERE) where the guys have almost the same problem, but me I want to make a OR and not a AND between my two operation.

There is the code I have so far :

        var filter = { logic: "OR", filters: [] };

        var filterProduct = { logic: "AND", filters: [] };
        var supplierValue = dropdownSupplier.value();
        if (supplierValue != "00000000-0000-0000-0000-000000000000") {
            filterProduct.filters.push({ field: "SupplierId", operator: "eq", value: supplierValue });
        }

        var categoryValue = dropdownCategory.value();
        if (categoryValue != "00000000-0000-0000-0000-000000000000") {
            filterProduct.filters.push({ field: "CategoryId", operator: "eq", value: categoryValue });
        }

        var typeValue = dropdownType.value();
        if (typeValue != "00000000-0000-0000-0000-000000000000") {
            filterProduct.filters.push({ field: "TypeId", operator: "eq", value: typeValue });
        }


        var filterSelect = { logic: "OR", filters: [] };
        filterSelect.filters.push({ field: "Id", operator: "eq", value: "00000000-0000-0000-0000-000000000000" });

        filter.filters.push(filterProduct);
        filter.filters.push(filterSelect);

        dropdownProduct.dataSource.query({ filter: filter });

In fact what I want to do it's to have : (filterProduct) OR (filterSelect) but if i'm not using "logic" in the variable filter, the filter will be an "AND".

Thanks alot

edit: There is a jsFiddle close to what I want to do but with the "AND" : Example In this example the filter is : [ {[ Freight = 11.61 OR Freight = 51.30 ]} AND {[ City startswith "Charleroi" ]} ]

But I want to have somethign like : [ {[ Freight = 11.61 OR Freight = 51.30 ]} OR {[ City startswith "Charleroi" ]} ]

like image 243
Marc-André Bilodeau-Lamontagne Avatar asked Dec 02 '22 20:12

Marc-André Bilodeau-Lamontagne


2 Answers

There is the final code working properly :

The keyword AND and OR need to be lowercase and I don't need to use the variable filterSelect

    var filter = { logic: "or", filters: [] };
    var filterProduct = { logic: "and", filters: [] };
    var supplierValue = dropdownSupplier.value();
    if (supplierValue != "00000000-0000-0000-0000-000000000000") {
        filterProduct.filters.push({ field: "SupplierId", operator: "eq", value: supplierValue });
    }
    var categoryValue = dropdownCategory.value();
    if (categoryValue != "00000000-0000-0000-0000-000000000000") {
        filterProduct.filters.push({ field: "CategoryId", operator: "eq", value: categoryValue });
    }
    var typeValue = dropdownType.value();
    if (typeValue != "00000000-0000-0000-0000-000000000000") {
        filterProduct.filters.push({ field: "TypeId", operator: "eq", value: typeValue });
    }
    if (filterProduct.filters.length > 0) {
        filter.filters.push(filterProduct);
    }
    filter.filters.push({ field: "Id", operator: "eq", value: "00000000-0000-0000-0000-000000000000" });
    dropdownProduct.dataSource.query({ filter: filter });
like image 153
Marc-André Bilodeau-Lamontagne Avatar answered Dec 15 '22 11:12

Marc-André Bilodeau-Lamontagne


Thanks forumma, your example helped me. For others trying to decipher the Kendo UI (since there are multiple, undocumented ways to do things), I have included my example to set filters on two different fields and one field has a compound OR filter:

var initialFilter = { logic: "or", filters: [] };
var filterStatus = [
    { field: "impStatus", operator: "neq", value: "Complete" }
    ];
var filterDeveloper = { logic: "or", filters: [
      { field: "developer", operator: "eq", value: "Unassigned" }
    ] };

filterDeveloper.filters.push( { field: "developer", operator: "eq", value: "Bob" } );

initialFilter.filters.push( filterStatus );
initialFilter.filters.push( filterDeveloper );

dataSource.filter( initialFilter );
like image 22
Gravitoid Avatar answered Dec 15 '22 09:12

Gravitoid