2014-10-28 72 views
1

如何將搜索篩選器應用於僅使用一個文本框的多個字段。我的JSON包含名稱和IP以及其他一些字段。如果我使用過濾器。$我可以在所有字段上獲得過濾器。如果我使用filter.Name,我只能過濾一個。但我想過濾兩個(名稱和IP)。我應該如何將結果分配給getData函數中的filteredData?在多個字段上使用角度搜索篩選器

我的代碼看起來像這樣。

這將是真正有用的,如果有人在oculd更新embed.plnkr.co/llb5k6/preview的例子

<input class="form-control main-search" type="text" ng-model="filter.Name" placeholder="enter text to search" /><table ng-table="tableParams" class="table tile"> 
      <tr ng-repeat="node in $data"> 

       <td data-title="'Name'" sortable="'Name'"> 
        {{node.Name}} 
       </td> 
       <td data-title="'IP'" sortable="'IP'"> 
        {{node.IP }} 
       </td> 
      </tr> 
     </table> 

我的js看起來像

$http.get('/api/nodesapi').success(function (nodes) { 
    $scope.data = nodes; 
    $scope.$watch("filter.Name", function() { 
     $scope.tableParams.reload(); 
    }); 
    $scope.tableParams = new ngTableParams({ 
     page: 1,   // show first page 
     count: 10,   // count per page 
     sorting: { 
      name: 'asc' 
     } 
    }, { 

     getData: function ($defer, params) { 
      debugger; 
      var filteredData = $filter('filter')($scope.data, $scope.filter); 

      var orderedData = params.sorting() ? 
           $filter('orderBy')(filteredData, params.orderBy()) : 
           filteredData; 

      $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count())); 
     }, 
     $scope: $scope 
    }); 
}).error(function() { 
    $scope.error = "An Error has occured while loading nodes!";}); 

回答

0

你可以這樣做:

<input ng-model="query"> 

then

<tr ng-repeat="node in $data | filter:filterData "> 

和你的JS

$scope.filterData = function (item){ 
    if (item.name.indexOf($scope.query)!=-1 || item.IP.indexOf($scope.query)!=-1) { 
    return true; 
    } 
    return false; 
}; 
+0

但它搜索的所有字段。我需要使用一個文本框只搜索其中兩個。像embed.plnkr.co/llb5k6/preview但只在兩個字段。 – 2014-10-28 19:42:04

+0

看看我的編輯 – 2014-10-28 19:56:34