如何添加搜索,例如按类别
How to add a search for example by category?
如何按类别添加搜索??它是如何连接到filteredUsers的??请帮帮我。。。如果只是添加一个过滤器,分页就不能正常工作。我是这样做的。但我不知道在哪里添加过滤器:
var app = angular.module('appTelDirectory', []);
app.controller('directoryList', function($scope, $filter) {
$scope.currentPage = 0;
$scope.pageSize = 10;
$scope.users = [];
$scope.category = [{'name':'cat1'},{'name':'cat2'}]
// Using a separate list of filtered users
$scope.filteredUsers = [{}];
$scope.numberOfPages = function() {
return Math.ceil($scope.filteredUsers.length / $scope.pageSize);
}
for (var i = 0; i < 45; i++) {
if(i % 2 == 0) {
cat = 'cat1'
}
else cat= 'cat2';
$scope.users.push({'name':'user'+i,'category':''+cat});
}
$scope.filteredUsers = angular.copy($scope.users);
$scope.$watch('searchAll', function(newValue) {
// Manually filtering here instead doing in the view
$scope.filteredUsers = $filter('filter')($scope.users, {$: newValue});
});
});
app.filter('startFrom', function() {
return function(input, start) {
start = +start; //parse to int
return input.slice(start);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="appTelDirectory" ng-controller="directoryList">
<input placeholder="Поиск..." ng-model="searchAll" class="form-control">
<span> Категория </span>
<select>
<option ng-repeat="category in category | filter:answer">{{category.name}}</option>
</select>
<ul>
<li ng-repeat="item in filteredUsers | startFrom:currentPage*pageSize | limitTo:pageSize">{{item.name}}---->>{{item.category}}</li>
</ul>
<table>
<tr ng-repeat="item in users | startFrom:currentPage*pageSize | limitTo:pageSize">
</table>
<button ng-disabled="currentPage == 0" ng-click="currentPage=currentPage-1">Previous</button>
{{currentPage+1}}/{{numberOfPages()}}
<button ng-disabled="currentPage >= filteredUsers.length/pageSize - 1" ng-click="currentPage=currentPage+1">
Next
</button>
</div>
如果有任何帮助,我将不胜感激。
将ng-model
添加到您的选择中。
<select ng-model="selectedCat">
<option ng-repeat="category in category | filter:answer">{{category.name}} </option>
</select>
在watcher
函数、中也添加了基于类别进行过滤的代码
$scope.$watch('searchAll', function(newValue) {
// Manually filtering here instead doing in the view
$scope.filteredUsers = $filter('filter')($scope.users, {$: newValue});
$scope.filteredUsers = $filter('filter')($scope.filteredUsers, {$: $scope.selectedCat});
});
// Another watcher to listen change in the selected category
$scope.$watch('selectedCat', function(newValue) {
// Manually filtering here instead doing in the view
$scope.filteredUsers = $filter('filter')($scope.users, {$: newValue});
$scope.filteredUsers = $filter('filter')($scope.filteredUsers, {$: $scope.searchAll});
});
相关文章:
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 如何在映射数组中添加换行符
- 正在将数据主题添加到所有项目
- ZeroClipboard-在复制之前添加到值
- 我可以在json对象中添加一个函数吗
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 正在SharePoint 2013母版页中添加JQuery移动文件
- 如何添加浮动和非浮动,其他
- 添加文字和评论功能更新Div
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 在函数中添加数组元素的数值
- 如何将歌曲添加到jPlayer
- JavaScript/jQuery-添加添加和删除类与下一个函数之间的延迟
- wooccommerce登录页:如何添加添加到购物车按钮
- 如何添加添加边框的无限制点击
- 我如何添加添加到一个javascript对象
- 得到我的tabpanel组件,但不能使用添加添加一个选项卡
- 如何在yuidoc中添加添加外部链接(grunt contrib yuidoc)
- 在队列jQuery中添加添加/删除类点击图片