Javascript 角度过滤器和限制未按预期工作
我是angular.js的新手,看到了一些我不喜欢的东西。当我同时过滤和限制数据时,限制似乎会影响过滤器,并且无法在原始数组源中搜索。我错过什么了吗 更新: 问题是,我正试图创建一个简单的数据表指令,其中包含分页、筛选和其他一些内容,但这让我抓狂。我已经看到了答案和评论,我想知道我试图达到的过程是否是错误的,但不,从逻辑上讲,限制一定不会影响过滤器(我认为是的),因为可能在某个时候你需要搜索整个源,而不是限制的部分,它们应该是分开的。这不是我在主要datatables框架中看到的默认行为吗 下面是一个描述该问题的示例:Javascript 角度过滤器和限制未按预期工作,javascript,angularjs,angularjs-ng-repeat,angularjs-filter,angularjs-limitto,Javascript,Angularjs,Angularjs Ng Repeat,Angularjs Filter,Angularjs Limitto,我是angular.js的新手,看到了一些我不喜欢的东西。当我同时过滤和限制数据时,限制似乎会影响过滤器,并且无法在原始数组源中搜索。我错过什么了吗 更新: 问题是,我正试图创建一个简单的数据表指令,其中包含分页、筛选和其他一些内容,但这让我抓狂。我已经看到了答案和评论,我想知道我试图达到的过程是否是错误的,但不,从逻辑上讲,限制一定不会影响过滤器(我认为是的),因为可能在某个时候你需要搜索整个源,而不是限制的部分,它们应该是分开的。这不是我在主要datatables框架中看到的默认行为吗 下面
angular.module('ngRepeat',['ngAnimate']).controller('repeatController',function($scope){
$scope.q=“约翰”
$scope.friends=[
{姓名:'John',年龄:25岁,性别:'boy'},
{姓名:'Jessie',年龄:30岁,性别:'girl'},
{姓名:'Johanna',年龄:28岁,性别:'girl'},
{姓名:'Joy',年龄:15岁,性别:'girl'},
{姓名:'Mary',年龄:28岁,性别:'girl'},
{姓名:'Peter',年龄:95岁,性别:'boy'},
{姓名:'Sebastian',年龄:50岁,性别:'boy'},
{姓名:'Erika',年龄:27岁,性别:'girl'},
{姓名:'Patrick',年龄:40岁,性别:'boy'},
{姓名:'Samantha',年龄:60岁,性别:'girl'}
];
});代码>
。容器动画示例{
背景:白色;
边框:1px纯黑;
列表样式:无;
保证金:0;
填充:0 10px;
}
.设置重复动画{
线高:30px;
列表样式:无;
框大小:边框框;
}
.设置动画-重复.ng-移动,
.设置动画-重复.ng-enter,
.设置动画-重复.ng-离开{
过渡:全线性0.5s;
}
.设置动画-重复.ng-left.ng-left-active,
.设置动画-重复.ng-移动,
.设置动画-重复.ng-enter{
不透明度:0;
最大高度:0;
}
.设置动画-重复.ng-离开,
.设置动画-重复.ng-move.ng-move-active,
.设置动画-重复.ng-enter.ng-enter-active{
不透明度:1;
最大高度:30px;
}
下面的示例显示,当同时过滤和限制数据时,无法过滤当前位置之前的项目。
职位:5
限制:5
筛选器:{'$':{{{q}}}
我有很多朋友。他们是:
-
[{{$index+1}}]{{friend.name}}谁是{{friend.age}岁。
-
未找到任何结果…
如您所见,没有结果。
您遇到了问题,因为您的limito
从索引5开始限制了5个项目,搜索时很可能没有5个项目,尤其是没有提供数据的项目
改变
limitTo : 5 : 5 track by $index
到
或
我用这个找到了结果
ng-repeat="friend in friends | filter:q | limitTo : 5 as results track by $index"
按照DataTables.net的原理,当您开始筛选数据时,它会返回到第一页。我希望它能帮助别人
angular.module('ngRepeat',['ngAnimate']).controller('repeatController',function($scope){
$scope.position=5;
$scope.resetPagination=函数(){
如果(this.position!==0)this.position=0;
}
$scope.friends=[
{姓名:'John',年龄:25岁,性别:'boy'},
{姓名:'Jessie',年龄:30岁,性别:'girl'},
{姓名:'Johanna',年龄:28岁,性别:'girl'},
{姓名:'Joy',年龄:15岁,性别:'girl'},
{姓名:'Mary',年龄:28岁,性别:'girl'},
{姓名:'Peter',年龄:95岁,性别:'boy'},
{姓名:'Sebastian',年龄:50岁,性别:'boy'},
{姓名:'Erika',年龄:27岁,性别:'girl'},
{姓名:'Patrick',年龄:40岁,性别:'boy'},
{姓名:'Samantha',年龄:60岁,性别:'girl'}
];
});代码>
。容器动画示例{
背景:白色;
边框:1px纯黑;
列表样式:无;
保证金:0;
填充:0 10px;
}
.设置重复动画{
线高:30px;
列表样式:无;
框大小:边框框;
}
.设置动画-重复.ng-移动,
.设置动画-重复.ng-enter,
.设置动画-重复.ng-离开{
过渡:全线性0.5s;
}
.设置动画-重复.ng-left.ng-left-active,
.设置动画-重复.ng-移动,
.设置动画-重复.ng-enter{
不透明度:0;
最大高度:0;
}
.设置动画-重复.ng-离开,
.设置动画-重复.ng-move.ng-move-active,
.设置动画-重复.ng-enter.ng-enter-active{
不透明度:1;
最大高度:30px;
}
下面的示例显示,当同时过滤和限制数据时,无法过滤当前位置之前的项目。
限制:5
位置:{{position}}
筛选器:{'$':{{{q}}}
我有很多朋友。他们是:
-
[{{$index+1}}]{{friend.name}}谁是{{friend.age}岁。
-
未找到任何结果…
正如您所看到的,这是有结果的。
这可能会对您有所帮助。这是分页
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular-animate.min.js"></script>
<script>
var app=angular.module('ngRepeat', ['ngAnimate']);
app.controller('repeatController', function($scope) {
$scope.q="john";
$scope.currentPage = 0;
$scope.pageSize = 5;
$scope.numberOfPages=function(){
return Math.ceil($scope.friends.length/$scope.pageSize);
}
$scope.friends = [
{name:'John', age:25, gender:'boy'},
{name:'Jessie', age:30, gender:'girl'},
{name:'Johanna', age:28, gender:'girl'},
{name:'Joy', age:15, gender:'girl'},
{name:'Mary', age:28, gender:'girl'},
{name:'Peter', age:95, gender:'boy'},
{name:'Sebastian', age:50, gender:'boy'},
{name:'Erika', age:27, gender:'girl'},
{name:'Patrick', age:40, gender:'boy'},
{name:'Samantha', age:60, gender:'girl'}
];
});
app.filter('startFrom', function() {
return function(input, start) {
start = +start; //parse to int
return input.slice(start);
}
});
</script>
<style>
.example-animate-container {
background: white;
border: 1px solid black;
list-style: none;
margin: 0;
padding: 0 10px;
}
.animate-repeat {
line-height: 30px;
list-style: none;
box-sizing: border-box;
}
.animate-repeat.ng-move,
.animate-repeat.ng-enter,
.animate-repeat.ng-leave {
transition: all linear 0.5s;
}
.animate-repeat.ng-leave.ng-leave-active,
.animate-repeat.ng-move,
.animate-repeat.ng-enter {
opacity: 0;
max-height: 0;
}
.animate-repeat.ng-leave,
.animate-repeat.ng-move.ng-move-active,
.animate-repeat.ng-enter.ng-enter-active {
opacity: 1;
max-height: 30px;
}
</style>
var app=angular.module('ngRepeat',['ngAnimate']);
应用控制器('repeatController',函数($scope){
$scope.q=“约翰
ng-repeat="friend in friends | filter:q | limitTo : 5 as results track by $index"
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular-animate.min.js"></script>
<script>
var app=angular.module('ngRepeat', ['ngAnimate']);
app.controller('repeatController', function($scope) {
$scope.q="john";
$scope.currentPage = 0;
$scope.pageSize = 5;
$scope.numberOfPages=function(){
return Math.ceil($scope.friends.length/$scope.pageSize);
}
$scope.friends = [
{name:'John', age:25, gender:'boy'},
{name:'Jessie', age:30, gender:'girl'},
{name:'Johanna', age:28, gender:'girl'},
{name:'Joy', age:15, gender:'girl'},
{name:'Mary', age:28, gender:'girl'},
{name:'Peter', age:95, gender:'boy'},
{name:'Sebastian', age:50, gender:'boy'},
{name:'Erika', age:27, gender:'girl'},
{name:'Patrick', age:40, gender:'boy'},
{name:'Samantha', age:60, gender:'girl'}
];
});
app.filter('startFrom', function() {
return function(input, start) {
start = +start; //parse to int
return input.slice(start);
}
});
</script>
<style>
.example-animate-container {
background: white;
border: 1px solid black;
list-style: none;
margin: 0;
padding: 0 10px;
}
.animate-repeat {
line-height: 30px;
list-style: none;
box-sizing: border-box;
}
.animate-repeat.ng-move,
.animate-repeat.ng-enter,
.animate-repeat.ng-leave {
transition: all linear 0.5s;
}
.animate-repeat.ng-leave.ng-leave-active,
.animate-repeat.ng-move,
.animate-repeat.ng-enter {
opacity: 0;
max-height: 0;
}
.animate-repeat.ng-leave,
.animate-repeat.ng-move.ng-move-active,
.animate-repeat.ng-enter.ng-enter-active {
opacity: 1;
max-height: 30px;
}
</style>
<div ng-app="ngRepeat" ng-controller="repeatController">
The flollowing example shows when you filter and limit the data at the same time, you can not filter the items before the
current position.
<br/>
<b>position</b> : 5<br/>
<b>limitTo</b> : 5<br/>
<b>filter</b> : { '$' : '{{q}}' }<br/> I have {{friends.length}} friends. They are:
<input type="search" value="john" ng-model="q" placeholder="filter friends..." aria-label="filter friends" />
<ul class="example-animate-container">
<li class="animate-repeat" ng-repeat="friend in friends | filter:q | startFrom:currentPage*pageSize | limitTo:pageSize as results track by $index">
[{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.
</li>
<li class="animate-repeat" ng-if="results.length == 0">
<strong>No results found...</strong>
</li>
</ul>
<button ng-disabled="currentPage == 0" ng-click="currentPage=currentPage-1">
Previous
</button>
{{currentPage+1}}/{{numberOfPages()}}
<button ng-disabled="currentPage >= friends.length/pageSize-1" ng-click="currentPage=currentPage+1;">
Next
</button>
</div>