2015-09-15 118 views
1

過濾我想創建嵌套對象上的過濾器是這樣的:AngularJS嵌套性

Object 1 : 
property1 
property2 
property3 
children : Child 1 : 
      propertyChild1 
      propertyChild2 
      Child 2 : 

等。一個對象可以有多個孩子。沒有深度限制specified.The問題是,我想搜索只在對象的某些屬性,所以我用:

ng-repeat="groupLevel1 in groupLevel2.children | filter: {lineDescription: searchKeyword}" 

這是在所有層面上搜索,但如果父母不包含searchKeyword,所有不顯示兒童(可能包含搜索)。我想,所有的家長級別顯示,以顯示包含即使父母不匹配搜索的搜索關鍵字的孩子。

我嘗試了一些複雜的腳本,但它不工作:

appReportingHoliday.filter('globalFilter', function(){ 
return function(array, predicate){ 
    return array.filter(function(val){ 
     var formattedObj = parseFloatInternational(predicate); 
     var re = new RegExp(formattedObj, 'i'); 
     var initialArray = []; 
     initialArray.push(val); 
     var childIsNeeded = false; 
     var toReturnTemp; 
     var parents = []; 
     var toReturn = []; 
     while(initialArray!=null){ 
      angular.forEach(initialArray, function (currentVal) { 
       toReturnTemp = false; 
       //We check if the val is concerned by the search 
       toReturnTemp = re.test(currentVal.lineDescription) || re.test(currentVal.acquiredHolidays) || re.test(currentVal.tokenHolidays) || re.test(currentVal.availableHolidays) 
         || re.test(currentVal.dailyCost) || re.test(currentVal.valuation); 

       if (toReturnTemp) { 
        //if it is, we need to add the result to the toReturn array and also the parents that we could have saved in the according array 
        toReturn.push(currentVal); 
        toReturn.push(parents); 
        parents = []; 
       } 
       else { 
        //else we save it in the parents array if a child is needed 
        if(currentVal.children!=null) { 
         parents.push(currentVal); 
        } 
       } 
       var index = initialArray.indexOf(currentVal); 
       initialArray.splice(index, 1); 
       if(currentVal.children!=null) { 
        angular.forEach(currentVal.children, function (currentChild) { 
         initialArray.push(currentChild); 
        }); 

       } 
      }); 
      if(initialArray.length==0) initialArray = null; 
     } 
     return toReturn; 
    }); 
} 
}); 

顯示屏由這樣的:

<tr class="groupReportingTreeDatatable" ng-repeat-start="groupLevel3 in myData | filter: {lineDescription: searchKeyword}" ng-init="$index &lt; 2 ? groupLevel3.hideRows = false : groupLevel3.hideRows = true;" ng-class-even="'dataTable_row1'" ng-class-odd="'dataTable_row2'" spinner-handler-directive=""> 
         ... 
<tr class="groupReportingTreeDatatable" ng-hide="groupLevel3.hideRows" ng-init="groupLevel2.hideRows = true" ng-repeat-start="groupLevel2 in groupLevel3.children | filter: {lineDescription: searchKeyword}" ng-class-even="'dataTable_row1'" ng-class-odd="'dataTable_row2'"> 

         ... 


<tr ng-hide="groupLevel2.hideRows || groupLevel3.hideRows" ng-repeat="groupLevel1 in groupLevel2.children | filter: {lineDescription: searchKeyword}" ng-class-even="'dataTable_row1'" ng-class-odd="'dataTable_row2'" ng-repeat-end=""> 

編輯: 我嘗試別的東西它適用於某些搜索,但不他們全部:(

appReportingHoliday.filter('globalFilter', function() { 
return function (array, predicate) { 
    return array.filter(function (val) { 
     var formattedObj = parseFloatInternational(predicate); 
     var re = new RegExp(formattedObj, 'i'); 
     var found = re.test(val.lineDescription) || re.test(val.acquiredHolidays) || re.test(val.tokenHolidays) || re.test(val.availableHolidays) 
         || re.test(val.dailyCost) || re.test(val.valuation); 
     var child = val.children; 

     while(child!=null && found == false){ 
      angular.forEach(child, function (currentChild) { 
       if(found == false) { 
        console.log(currentChild.lineDescription) 
        found = re.test(currentChild.lineDescription) || re.test(currentChild.acquiredHolidays) || re.test(currentChild.tokenHolidays) || re.test(currentChild.availableHolidays) 
          || re.test(currentChild.dailyCost) || re.test(currentChild.valuation); 
       } 
      }); 
      child = child.children; 
     } 

     return found; 
    }); 
} 
}); 

回答

0

會不會更容易有一個第二個變量,你把第一個孩子壓扁了嗎?你可以定義一個遞歸函數做這件事,像...

var mainObject = ... ;//that's your object 
var flattened = new Array(); 

function flatten(main,flat) { 
    var children = main.children; 
    for (var i=0;i<children.length;i++) { 
     flatten(children[i],flat); // recursive call, depth-first 
     delete children[i].children; // those are already treated 
     flat.push(children[i]); // add children 
    } 
    delete main.children; 
    flat.push(main); 
} 

現在你可以直接過濾屬性。

+0

我用遞歸方法非常糟糕,我試過一次,但沒「T WRK,不上即在頁面上實際顯示的VAR應用過濾器?因爲我不知道怎樣才能使用日e第二個變量,如果它沒有在屏幕上實際使用。 我添加了一個新的代碼下面,因爲我不能在評論部分寫的整個代碼 – user3314017

0

我看不出這在我的代碼整合。我無法更改基本變量,因爲我需要在此結構中將其顯示在屏幕上。我可以創建另一個展平的數組,但是過濾器必須應用於var I顯示,否?所以我不能使用扁平變種。我不得不承認,我有點失落^^

我固定深度3,使其更容易所以現在我有一個這樣的:

appReportingHoliday.filter('globalFilter', function() { 
return function (array, predicate) { 
    return array.filter(function (val) { 
     var formattedObj = parseFloatInternational(predicate); 
     var re = new RegExp(formattedObj, 'i'); 
     var found = re.test(val.lineDescription) || re.test(val.acquiredHolidays) || re.test(val.tokenHolidays) || re.test(val.availableHolidays) 
         || re.test(val.dailyCost) || re.test(val.valuation); 
     var child = val.children; 

     if(child!=null && found == false){ 
      angular.forEach(child, function (currentChild) { 
       if(found == false) { 
        found = re.test(currentChild.lineDescription) || re.test(currentChild.acquiredHolidays) || re.test(currentChild.tokenHolidays) || re.test(currentChild.availableHolidays) 
          || re.test(currentChild.dailyCost) || re.test(currentChild.valuation); 
       } 
       if(currentChild.children!=null && found == false){ 
        angular.forEach(currentChild.children, function (currentGrandChild) { 
         if(found == false) { 
          found = re.test(currentGrandChild.lineDescription) || re.test(currentGrandChild.acquiredHolidays) || re.test(currentGrandChild.tokenHolidays) || re.test(currentGrandChild.availableHolidays) 
            || re.test(currentGrandChild.dailyCost) || re.test(currentGrandChild.valuation); 
         } 
        }); 
       } 

      }); 
      child = child.children; 
     } 
     return found; 
    }); 
    } 
}); 

剩下唯一的問題是,如果搜索對父母,我希望所有的孩子T爲顯示但現在只有匹配的孩子都顯示:■我無法找到孩子的父母,我只有從父母到孩子不是其他方式鏈接:■