如果给定字段/列有多个值(值数组),则从剑道网格添加和删除过滤器

时间:2019-03-30 06:41:08

标签: jquery kendo-ui kendo-grid kendo-multiselect

我创建了一个自定义弹出窗口,其中包含4个剑道多选控件和一个“应用”按钮。当我按下“应用”按钮时,所选值应在kendogrid中使用并过滤结果。每次我打开弹出窗口,并且每次将相同的过滤器添加到kendogrid odata查询时,即使我从multiselect控件中删除了该值也是如此。

我尝试从kendo网格数据源过滤器中删除过滤器(如果已存在)。但由于kendo multiselect控件为我提供了一组值,因此我无法做到这一点,当我们将这些值添加到过滤器时,它会创建一组过滤器

在弹出式窗口中单击“应用”按钮时,将调用以下代码

var filter = {   logic: "AND",  filters: [] };           
                var controllerValue = $("#controller").data("kendoMultiSelect").dataItems();
                var reconciledByValue = $("#reconciledBy").data("kendoMultiSelect").dataItems();
                var reviewedByValue = $("#reviewedBy").data("kendoMultiSelect").dataItems();
                var approvedByValue = $("#approvedBy").data("kendoMultiSelect").dataItems();

                if (controllerValue.length > 0) {
                    filter.filters.push(createFilterGroup(controllerValue, "OR", "Controller"));
                }
                if (reconciledByValue.length > 0) {
                    filter.filters.push(createFilterGroup(reconciledByValue, "OR", "ReconciledByA"));
                }
                if (reviewedByValue.length > 0) {
                    filter.filters.push(createFilterGroup(reviewedByValue, "OR", "ReviewedByA"));
                }
                if (approvedByValue.length > 0) {
                    filter.filters.push(createFilterGroup(approvedByValue, "OR", "ApprovedByA"));
                }
if (filter.filters.length > 0) {
var gridDatasource = $("#" + gridid).data("kendoGrid").dataSource;
var allFilters = [];

allFilters = gridDatasource.filter().filters; //previous filters
allFilters.push(filter); //Add filters
 gridDatasource.filter(allFilters);
}


function createFilterGroup(filterData, operator, field) {
   var filterGroup = {
    logic: operator,
    filters: []
   };

 $.each(filterData, function (key, value) {
   filterGroup.filters.push({ field: field, operator: "eq", value: value[field] });
   });

return filterGroup;
 }




I expect the output of odata query to remove already added filter condition first and than add new condition

below URL contains Controller+eq+%27denekewj%27+OR+Controller+eq+%27jabbott%27
in the first search
https://localhost:44335/odata/UserReconciliationAccounts/GetDistinctRoleCountForAccountBase?%24format=json&%24top=50&%24filter=((((FiscalYear+lt+2019+OR+(FiscalPeriod+lt+8+AND+FiscalYear+eq+2019))+AND+(IsReconLateAndIncomplete+eq+true+OR+IsReviewLateAndIncomplete+eq+true+OR+IsApprovalLateAndIncomplete+eq+true))+OR+(FiscalPeriod+eq+8+AND+FiscalYear+eq+2019))+and+(Controller+eq+%27denekewj%27+OR+Controller+eq+%27jabbott%27))&%24count=true

next time if I remove one controller value than query is forming like below

https://localhost:44335/odata/UserReconciliationAccounts/GetDistinctRoleCountForAccountBase?%24format=json&%24top=50&%24filter=((((FiscalYear+lt+2019+OR+(FiscalPeriod+lt+8+AND+FiscalYear+eq+2019))+AND+(IsReconLateAndIncomplete+eq+true+OR+IsReviewLateAndIncomplete+eq+true+OR+IsApprovalLateAndIncomplete+eq+true))+OR+(FiscalPeriod+eq+8+AND+FiscalYear+eq+2019))+and+(Controller+eq+%27denekewj%27+OR+Controller+eq+%27jabbott%27)+and+Controller+eq+%27jabbott%27)&%24count=true

Now I remove all conroller value from kendo multiselect and added value in other multiselect than below query formed
https://localhost:44335/odata/UserReconciliationAccounts/GetDistinctRoleCountForAccountBase?%24format=json&%24top=50&%24filter=((((FiscalYear+lt+2019+OR+(FiscalPeriod+lt+8+AND+FiscalYear+eq+2019))+AND+(IsReconLateAndIncomplete+eq+true+OR+IsReviewLateAndIncomplete+eq+true+OR+IsApprovalLateAndIncomplete+eq+true))+OR+(FiscalPeriod+eq+8+AND+FiscalYear+eq+2019))+and+(Controller+eq+%27denekewj%27+OR+Controller+eq+%27jabbott%27)+and+Controller+eq+%27jabbott%27+and+ReconciledByA+eq+%27sihuda%27)&%24count=true

1 个答案:

答案 0 :(得分:0)

最后花很多时间。我能够找到解决方案。

第1步:添加递归函数以清除已添加的过滤器(嵌套过滤器)

function removeFilter(filter, searchFor) {
                if (filter == null)
                    return [];

                for (var x = 0; x < filter.length; x++) {

                    if (filter[x].filters != null && filter[x].filters.length >= 0) {
                        if (filter[x].filters.length == 0) {
                            filter.splice(x, 1);
                            return removeFilter(filter, searchFor);
                        }
                        filter[x].filters = removeFilter(filter[x].filters, searchFor);
                    }
                    else {
                        if (filter[x].field == searchFor) {
                            filter.splice(x, 1);
                            return removeFilter(filter, searchFor);
                        }
                    }
                }

                return filter;
            }

            function clearRootLevelArray(filter) {
                for (var x = 0; x < filter.length; x++) {
                    if ((filter[x].filters !== undefined || filter[x].filters != null)
                        && filter[x].filters.length == 0) {
                        filter.splice(x, 1);
                        return clearRootLevelArray(filter);
                    }
                }
                return filter;
            }
            function clearInnerLevelArray(filter) {
                for (var x = 0; x < filter.length; x++) {
                    if (filter[x].filters !== undefined || filter[x].filters != null) {
                        for (var y = 0; y < filter[x].filters.length; y++) {
                            if ((filter[x].filters[y].filters !== undefined || filter[x].filters[y].filters != null)
                                && filter[x].filters[y].filters.length == 0) {
                                filter[x].filters.splice(y, 1);
                                return clearInnerLevelArray(filter);
                            }
                        }
                    }
                }
                clearRootLevelArray(filter)//once again clear root level after clearing innerlevel
                return filter;
            }

//Last step how/when calling these function 
 var gridDatasource = $("#gridid").data("kendoGrid").dataSource;
 var allFilters = [];

 allFilters = gridDatasource.filter().filters; //previous filters
 //before pushing new filters, first check if filters are already added, //if yes than remove them and add again
allFilters = removeFilter(allFilters, "status");
allFilters = clearRootLevelArray(allFilters);
allFilters = clearInnerLevelArray(allFilters);
allFilters.push(filter); //Add filters
gridDatasource.filter(allFilters);