2016-06-08 68 views
0

我想在離子中做一個搜索欄,如果它類似於這個example在離子中有多個關鍵字的過濾器列表

這是app.html

<ion-content class="has-header" padding="true" ng-controller="appCtrl"> 
//Search Bar 
<label class="item item-input"> 
    <i class="icon ion-search placeholder-icon"></i> 
    <input type="search" placeholder="Search" ng-model="searchText"> 
</label> 

//This is the list that we want to search 
<form style="" class="list"> 
    <ion-list> 
     <ion-item ng-repeat="item in items | filter: userFilter() "> 
     <div class="row responsive-sm"> 
      <div class="col"> 
      <div>Item Id {{item.id}}</div> 
      <div>Item detail{{item.detail}}</div> 
      <div>Date{{item.date}}</div> 
      </div> 
     </div> 
     </ion-item> 
    </ion-list> 
    </form> 
</ion-content> 

這是controller.js

.controller('appCtrl', function($scope,$state,$location,$ionicModal,$filter) { 
$scope.items = [    
          { 
           id  : 1 , 
           detail : "A book about ghost", 
           date  : "20 March 1999" 
          }, 
          { 
           id  : 2, 
           detail : "A Book about famous person", 
           date  : "20 March 1999" 
          }, 
          { 
           id  : 3, 
           detail : "A Map to a house", 
           date  : "20 March 1999" 
          }, 
          { 
           id  : 4, 
           detail : "A famous horror Novel", 
           date  : "20 March 1999" 
          }, 
          { 
           id  : 5, 
           detail : "A story about the haunted house", 
           date  : "20 March 1999" 

          }]; 
//The filter that is used 
$scope.userFilter = function(item) { 
    // default to no match 
    var isMatch = false; 

    if ($scope.searchText) { 
    // split the input by space 
    var parts = $scope.searchText.split(' '); 

    // iterate each of the words that was entered 
    parts.forEach(function(part) { 
     // if the word is found in the post, a set the flag to return it. 
     if (new RegExp('part').test(item)) { 
     isMatch = true; 
     } 
    }); 
    } else { 
    // if nothing is entered, return all posts 
    isMatch = true; 
    } 

    return isMatch;};}) 

如果代碼工作,它應該顯示項目沒有2項沒有4當我們寫「一個著名的」。相反,過濾器會導致列表中的所有項目消失。無論如何解決它?

回答

0

看來你已經在ng-repeat過濾器中調用函數了| userFilter()沒有參數,你有函數帶有item參數,請確認這是否沒有問題我可以嘗試你的plunkr也是你的示例鏈接丟失。

好運

+0

這是鏈接http://plnkr.co/edit/d17dZDrlhY2KIfWCbKyZ?p=preview。我認爲item參數是ng-repeat項目中的項目參數。 – LearningDummy