1

我有四個過濾,例如JobType,經驗,DateAdded和工資我想要做的過濾爲個人和團體。對於它,我在我的控制器中定義的過濾器過濾式,並添加到模板像波紋管。怎麼做的angularjs過濾條件最簡單的羣體?

在我的控制器

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

在我的模板

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

過濾工作,我曾預計,該問題是假設,如果我想用一組過濾的過濾我是否需要寫它的一組if/else語句。它似乎像一個漫長的工作..

任何人都可以提出解決的最佳途徑????????

回答

0
.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; 
    } 
})  
1

你可以做這樣的:

.filter('filterType', function($filter) { 
    return function(jobs,workType,experience,dateAdded,salary) { 
    var filtered = Array.prototype.slice.apply(jobs); // make a copy of the array 
    if(!workType && !experience && !dateAdded && !salary){ 
    return filtered; 
    } 

    if(workType){ 
    filtered = $filter('filter')(filtered, {type: workType}); 
    } 

    if(experience){ 
    //filter by experience, like workType 
    } 

    if(dateAdded){ 
    // filter by date, like workType 
    } 

    //  ... other filters 

    return filtered; 
} 
}) 
+0

過濾= $過濾器( '過濾器')(過濾,{類型:workType});如何在過濾器中使用此參數? – user3391137

+0

「重複在中繼器是不允許使用‘軌道由’表達式指定唯一鍵轉發:作業崗位|過濾式:」這個錯誤是發生 – user3391137

+0

你是什麼意思「藉此參數過濾器?」另外,請創建plunkr/jsbin,再現您所看到的問題。 – sirrocco

相關問題