简体   繁体   English

在角度ui网格中使用外部过滤两次触发

[英]Twice firing with external filtering in angular ui-grid

I have finished this issue , however, I am faced with the problem of a weird triggering of applied filters. 我已经解决了这个问题 ,但是,我遇到了一个奇怪的问题,即触发了应用过滤器。 Let me try to explain to you. 让我尝试向您解释。 The first filter request looks like: 第一个过滤器请求如下所示:

?$orderby=id-&pageSize=25&pageNbr=1&$filter=eventTypeId==7  OPTIONS 200 xhr angular.js:11881    505 B   17 ms   
?$orderby=id-&pageSize=25&pageNbr=1&$filter=eventTypeId==7  GET 200 xhr Other   15.3 KB 92 ms   

We have options and we have get, it is fine. 我们有选择,也有得到,很好。 Next we apply the second filter and request looks like: 接下来,我们应用第二个过滤器,请求看起来像:

?$orderby=id-&pageSize=25&pageNbr=1&$filter=eventTypeId==7  GET 200 xhr angular.js:11881    15.3 KB 94 ms   
?$orderby=id-&pageSize=25&pageNbr=1&$filter=eventTypeId==7;studyId==231 OPTIONS 200 xhr angular.js:11881    505 B   21 ms   
?$orderby=id-&pageSize=25&pageNbr=1&$filter=eventTypeId==7;studyId==231 GET 200 xhr Other   15.8 KB 48 ms   
?$orderby=id-&pageSize=25&pageNbr=1&$filter=eventTypeId==7  OPTIONS 200 xhr angular.js:11881    505 B   22 ms   

For some reason the request fired twice. 由于某种原因,该请求被触发了两次。 Next we apply the third filter and the request looks like: 接下来,我们应用第三个过滤器,请求如下所示:

?$orderby=id-&pageSize=25&pageNbr=1&$filter=eventTypeId==7;studyId==231 OPTIONS 200 xhr angular.js:11881    505 B   18 ms   
?$orderby=id-&pageSize=25&pageNbr=1&$filter=eventTypeId==7;studyId==231;statusId==5 OPTIONS 200 xhr angular.js:11881    505 B   19 ms   
?$orderby=id-&pageSize=25&pageNbr=1&$filter=eventTypeId==7;studyId==231 GET 200 xhr Other   15.8 KB 43 ms   
?$orderby=id-&pageSize=25&pageNbr=1&$filter=eventTypeId==7;studyId==231;statusId==5 GET 200 xhr Other   1.1 KB  27 ms   
?$orderby=id-&pageSize=25&pageNbr=1&$filter=eventTypeId==7  GET 200 xhr Other   15.3 KB 95 ms

And we can see how the request fired twice again but in the final it sends one more additional request with only the first filter. 我们可以看到请求如何再次触发两次,但最终,它仅通过第一个过滤器发送了另一个请求。

Can anybody explain to me where my mistake is? 有人可以向我解释我的错误在哪里吗? Thanks in advance. 提前致谢。 My code for filters: 我的过滤器代码:

  gridApi.core.on.filterChanged( $scope, function() {
                // Declare vars
                var grid = this.grid;
                var columns = grid.columns;
            $scope.columnTitle = grid.columns[1].filters[0].term;
            $scope.columnDesc = grid.columns[2].filters[0].term;
            var columnType = grid.columns[3].filters[0].term;
            var columnStudy = grid.columns[4].filters[0].term;
            var columnPriority = grid.columns[5].filters[0].term;
            var columnSeverity = grid.columns[6].filters[0].term;
            var columnStatus = grid.columns[7].filters[0].term;
            var columnCreatedDate = grid.columns[8].filters[0].term;
            var columnModifiedDate = grid.columns[9].filters[0].term;

            // Create request for selectable filters
            var query = [];
            var string;
            function request (id, param) {

                if(param === "title==" || param === "description=="){
                    query.push(param + "*" + id + "*")
                } else {
                    query.push(param + id);
                }

                if (query.length <= 1){
                    return query
                } else {
                    string = query.join(";");
                    return string;
                }
            }

            // Define behavior for cancel filtering
            $scope.isfilterclear = true;

            angular.forEach(columns, function( col ) {
                if(col.filters[0].term){
                    $scope.isfilterclear = false;
                }
            });
            if($scope.isfilterclear) {
                $timeout(function() {
                    $rootScope.refresh()
                },500);
            }

            // Filter behavior for columns
            if($scope.columnTitle) {
                $scope.$watch('columnTitle', function (newVal, oldVal) {
                    // filterOptions.filterParam = 'title==*' + newVal + "*";
                    filterOptions.filterParam =  request(newVal, 'title==*');
                }, true);
                getData()
            }
            if($scope.columnDesc) {
                $scope.$watch('columnDesc', function (newVal, oldVal) {
                    // filterOptions.filterParam = 'description==*' + newVal + "*";
                    filterOptions.filterParam =  request(newVal, 'description==');
                }, true);
                getData()
            }
            if(columnType) {
                filterOptions.filterParam = request(columnType, 'eventTypeId==');
                getData();
            }
            if(columnStudy) {
                filterOptions.filterParam = request(columnStudy, 'studyId==');
                getData();
            }
            if(columnPriority) {
                filterOptions.filterParam = request(columnPriority, 'priorityId==');
                getData();
            }
            if(columnSeverity) {
                filterOptions.filterParam = request(columnSeverity, 'severityId==');
                getData();
            }
            if(columnStatus) {
                filterOptions.filterParam = request(columnStatus, 'statusId==');
                getData();
            }
            if(columnCreatedDate){
                filterOptions.filterParam = request($rootScope.setFilterDate, 'occuredDate>=');
                getData()
            }
            if(columnModifiedDate){
                filterOptions.filterParam = request($rootScope.setFilterDate, 'occuredDate>=');
                getData()
            }


        });

You are calling getData() inside each column filter, that's why your request firing two or more times. 您正在每个列过滤器内调用getData() ,这就是为什么您的请求会触发两次或更多次的原因。 Take out your function like this: 像这样取出您的函数:

 // Filter behavior for columns
                if($scope.columnTitle) {
                    $scope.$watch('columnTitle', function (newVal, oldVal) {
                        filterOptions.filterParam =  request(newVal, 'title==*');
                    }, true);
                }
                if($scope.columnDesc) {
                    $scope.$watch('columnDesc', function (newVal, oldVal) {
                        filterOptions.filterParam =  request(newVal, 'description==');
                    }, true);
                }
                if(columnType) {
                    filterOptions.filterParam = request(columnType, 'eventTypeId==');
                }
                if(columnStudy) {
                    filterOptions.filterParam = request(columnStudy, 'studyId==');
                }
                if(columnPriority) {
                    filterOptions.filterParam = request(columnPriority, 'priorityId==');
                }
                if(columnSeverity) {
                    filterOptions.filterParam = request(columnSeverity, 'severityId==');
                }
                if(columnStatus) {
                    filterOptions.filterParam = request(columnStatus, 'statusId==');
                }
                if(columnCreatedDate){
                    filterOptions.filterParam = request($rootScope.setFilterDate, 'occuredDate>=');
                }
                if(columnModifiedDate){
                    filterOptions.filterParam = request($rootScope.setFilterDate, 'occuredDate>=');
                }

                getData();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM