Kendo DataSource过滤具有多种条件

时间:2013-01-07 20:27:04

标签: datasource kendo-ui

我想为具有多个条件的数据源构建过滤器。我找到了这个线程(HERE),这些人几乎有同样的问题,但我想在我的两个操作之间做一个OR而不是AND。

到目前为止我有代码:

        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 });

实际上我想要做的是:(filterProduct)OR(filterSelect)但是如果我在变量过滤器中没有使用“逻辑”,则过滤器将是“AND”。

非常感谢

编辑:有一个jsFiddle接近我想做的但是使用“AND”:Example在这个例子中,过滤器是: [{[运费= 11.61或运费= 51.30]}和{[City startswith“Charleroi”]}]

但我希望有一些像: [{[运费= 11.61或运费= 51.30]} {[City startswith“Charleroi”]}]

2 个答案:

答案 0 :(得分:10)

最终代码正常运行:

关键字AND和OR必须是小写的,我不需要使用变量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 });

答案 1 :(得分:7)

谢谢forumma,你的榜样帮助了我。对于试图解读Kendo UI的其他人(因为有多种无法记录的方法),我已经将我的例子包含在两个不同的字段上设置过滤器,一个字段有一个复合OR过滤器:

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