2015-04-28 120 views
0

我已經創建了一個過濾器,但是此過濾器不能與數組內部的數組一起工作。Angularjs中的json數組複選框過濾器

'http://plnkr.co/edit/oygy79j3xyoGJmiPHm4g?p=info' 

以上plkr鏈接正在演示。

app.filter('checkboxFilter', function($parse) { 
    var cache = { //create an cache in the closure 
     result: [], 
     checkboxData: {} 
    }; 

    function prepareGroups(checkboxData) { 
     var groupedSelections = {}; 
     Object.keys(checkboxData).forEach(function(prop) { 

      //console.log(prop); 

      if (!checkboxData[prop]) { 
       return; 
      } //no need to create a function 

      var ar = prop.split('='); 

      //console.log("ar is - "+ar); 
      if (ar[1] === 'true') { 
       ar[1] = true; 
      } //catch booleans 
      if (ar[1] === 'false') { 
       ar[1] = false; 
      } //catch booleans 

      /* replacing 0 with true for show all offers */ 
      if(ar[0]=='SplOfferAvailable.text'){ 
       ar[1]='true'; 
      }else{ 

      } 

      //make sure the selection is there! 
      groupedSelections[ar[0]] = groupedSelections[ar[0]] || []; 
      //at the value to the group. 

      groupedSelections[ar[0]].push(ar[1]); 
     }); 
     return groupedSelections; 
    } 

    function prepareChecks(checkboxData) { 

     var groupedSelections = prepareGroups(checkboxData); 

     var checks = []; 

     //console.log(groupedSelections); 
     Object.keys(groupedSelections).forEach(function(group) { 
      //console.log("groupedSelections- "+groupedSelections); 
      //console.log("group- "+group); 
      var needToInclude = function(item) { 
       //console.log("item- "+item); 
       // use the angular parser to get the data for the comparson out. 
       var itemValue = $parse(group)(item); 

       var valueArr = groupedSelections[group]; 
       //console.log("valueArr- "+valueArr); 

       function checkValue(value) { //helper function 
        return value == itemValue; 
       } 
       //check if one of the values is included. 
       return valueArr.some(checkValue); 
      }; 
      checks.push(needToInclude); //store the function for later use 
     }); 
     return checks; 
    } 

    return function(input, checkboxData, purgeCache) { 

     if (!purgeCache) { //can I return a previous 'run'? 
      // is the request the same as before, and is there an result already? 
      if (angular.equals(checkboxData, cache.checkboxData) && cache.result.length) { 
       return cache.result; //Done! 
      } 
     } 
     cache.checkboxData = angular.copy(checkboxData); 


     var result = []; // this holds the results 


     //prepare the checking functions just once. 
     var checks = prepareChecks(checkboxData); 



     input.every(function(item) { 
      if (checks.every(function(check) { 
        return check(item); 

       })) { 
       result.push(item); 
      } 
      return result.length < 10000000; //max out at 100 results! 
     }); 

     cache.result = result; //store in chache 
     return result; 
    }; 
}); 

上面的代碼是用於複選框過濾器的。

當我點擊名爲「可用性」的複選框時,它不會過濾結果。

請幫我一把。

謝謝。

+0

您的鏈接不能正常工作 –

+0

請將您的一些代碼放在這裏。 –

+0

嘿傢伙再次請求chk,抱歉造成不便。 –

回答

0

我認爲你是通過JSON導航的方式是錯誤的,因爲如果你把這樣它的工作原理

"Location": "Riyadh", 
"AvlStatus": "AVAILABLE" 
"Rooms": {..... 

你必須以某種方式要經過酒店的客房和現在我覺得你不這樣做

+0

首先非常感謝你。是的,你是絕對正確的,但我不能這樣做。我從第三方服務獲取Xml並轉換該xml信息json。 –