user3391137 user3391137 -4 years ago 74
AngularJS Question

how to do easiest group of filtering conditions in angularjs?

I have four filtering such as JobType,Experience,DateAdded and Salary i want to do filtering as individual and group. For it i defined a filter as filterType in my controller and added to template like bellow.

in my controller

.filter('filterType', function($filter) {

// Create the return function
// set the required parameter name to **params**
return function(jobs,workType,experience,dateAdded,salary) {
var filtered =[];
var wT = angular.isUndefined(workType);
var exp = angular.isUndefined(experience);
var dA = angular.isUndefined(dateAdded);
var sal = angular.isUndefined(salary);

if(wT && exp && dA && sal)
return jobs;


angular.forEach(jobs,function(job){
if(exp && dA && sal){
if(job.type == workType)
filtered.push(job)
}
else if(dA && sal && wT){
var exp_val = $filter('split')(experience,',',0);
if(job.experience == exp_val)
filtered.push(job)
}
else if(sal && wT && exp){
var date_field = $filter('pastDay')(job.created_at);
console.log(job.created_at);
console.log(date_field);
if(dateAdded == date_field){
filtered.push(job);
}

}
else if(wT && exp && dA){
var exp_val = $filter('split')(experience,',',0);
if(job.created_at == exp_val)
filtered.push(job)
}

});
return filtered;
}
})


in my template

<div flex="20" flex-sm="100" ng-repeat="job in jobs | filterType: workType:experience:dateAdded:salary">


filtering is working as i had expected, The problem is suppose if i want to filter with set of filtering do i need to write the set of if/else statements for it. it seems as like a long work..

Can anyone suggest the best way to solve????????

Answer Source
.filter('filterType', function($filter) {

// Create the return function
// set the required parameter name to **params**
  return function(jobs,wT,exp,dA,sal) {
    var filtered =[];
    var multiple = false;

  if(!wT && !exp && !dA && !sal)
    return jobs;


    if(wT){
         var val = multiple ? filtered : jobs;
         var filtered_wT = [];  
         angular.forEach(val,function(job){
          if(job.type == wT)
            filtered_wT.push(job);
         });
            filtered = filtered_wT;
            multiple = true;
    }
    if(exp){
          var exp_val = $filter('split')(exp,',',0);
          var filtered_exp = [];
          var val = multiple ? filtered : jobs;
            angular.forEach(val,function(job){
             if(job.experience ==  exp_val)
               filtered_exp.push(job);
             });
            filtered = filtered_exp;
            multiple = true;
    }
    if(dA){
         var val = multiple ? filtered : jobs;
         var filtered_dA = [];
         angular.forEach(val,function(job){
          var date_field = $filter('pastDay')(job.created_at);                       
           if(dA == date_field)
            filtered_dA.push(job);
           });
        filtered = filtered_dA;
        multiple = true;
    }
    if(sal){
        var filtered_sal = [];
        angular.forEach(jobs,function(job){
          var min_sal = $filter('split')(sal,'-',0);  
          var max_sal = $filter('split')(sal,'-',1);  
          if(min_sal >= job.minsalary && max_sal <= job.maxsalary)    
            filtered_sal.push(job);
        });
        filtered = filtered_sal;
        multiple = true;
    }

  return filtered;
  }
})    
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download