4

我想为具有多个条件的数据源构建过滤器。我发现了这个线程(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)或(filterSelect)但如果我没有在变量过滤器中使用“逻辑”,过滤器将是一个“AND”。

非常感谢

编辑:有一个 jsFiddle 接近我想要做的,但使用“AND”:示例在此示例中,过滤器是:[ {[ Freight = 11.61 OR Freight = 51.30 ]} AND {[ City startswith "Charleroi" ]} ]

但我想要一些类似的东西:[ {[ Freight = 11.61 OR Freight = 51.30 ]} OR {[ City startswith "Charleroi" ]} ]

4

2 回答 2

11

最终代码正常工作:

关键字 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 });
于 2013-01-08T14:13:21.947 回答
8

谢谢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 );
于 2013-05-29T15:28:39.450 回答