如何仅使用一个文本框在多个字段上应用搜索过滤器。我的JSON包含名称和IP以及其他一些字段。如果我使用过滤器。$ i可以在所有字段上进行过滤。如果我使用filter.Name我只能过滤一个。但我想过滤两个(名称和IP)。我应该如何将结果分配给getData函数中的filteredData?
我的代码看起来像这样。
如果有人在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 :(得分:0)
你可以这样做:
<input ng-model="query">
然后
<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;
};