angularJS实现分页

引入分页组件:

 

<!-- 分页组件开始-->

<script src="../plugins/angularjs/pagination.js"></script>

<link rel="stylesheet" href="../plugins/angularjs/pagination.css">

<!-- 分页组件结束 -->

 

构建app模块时引入pagination模块

var app=angular.module('myapp',['pagination']);//定义模块

 

页面的表格下放置分页组件

<tm-pagination     conf="paginationConf"></tm-pagination>

 

在brandController中添加如下代码

//重新加载列表 数据

$scope.reloadList=function(){

     //切换页码 

$scope.findPage($scope.paginationConf.currentPage, $scope.paginationConf.itemsPerPage);

}

//分页控件配置

$scope.paginationConf = {

         currentPage: 1,

         totalItems: 10,

         itemsPerPage: 10,

         perPageOptions: [10,20, 30, 40, 50],

         onChange: function(){

                 $scope.reloadList();//重新加载

         }

};

//分页

$scope.findPage=function(page,rows){

 

$http.get('../brand/findPage.do?page='+page+'&rows='+rows).success(

            function(response){

                $scope.list=response.rows; 

                $scope.paginationConf.totalItems=response.total;//更新总记录数

            }          

    );

}

 

paginationConf 变量各属性的意义:

currentPage:当前页码

totalItems:总条数

itemsPerPage:

perPageOptions:页码选项

onChange:更改页面时触发事件

 

AngularJS实现分页通常需要使用到 ng-repeat 指令和自定义过滤器,以下是一个示例代码: HTML: ``` <div ng-app="myApp" ng-controller="myCtrl"> <ul> <li ng-repeat="item in filteredItems = (items | startFrom:(currentPage-1)*pageSize | limitTo:pageSize)">{{item}}</li> </ul> <div> <button ng-disabled="currentPage == 1" ng-click="currentPage=1">First</button> <button ng-disabled="currentPage == 1" ng-click="currentPage=currentPage-1">Previous</button> <button ng-disabled="currentPage == totalPages" ng-click="currentPage=currentPage+1">Next</button> <button ng-disabled="currentPage == totalPages" ng-click="currentPage=totalPages">Last</button> </div> <div> Current Page: {{currentPage}} </div> </div> ``` JavaScript: ``` var app = angular.module('myApp', []); app.filter('startFrom', function() { return function(input, start) { start = +start; return input.slice(start); } }); app.controller('myCtrl', function($scope) { $scope.items = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8', 'item9', 'item10', 'item11', 'item12', 'item13', 'item14', 'item15']; $scope.currentPage = 1; $scope.pageSize = 5; $scope.totalPages = Math.ceil($scope.items.length / $scope.pageSize); }); ``` 在上面的代码中,我们定义了一个自定义过滤器 startFrom,它接收一个起始位置参数 start,返回从该位置开始的数组切片。我们还定义了一个控制器 myCtrl,其中 $scope.items 是我们需要分页的数组,$scope.currentPage 是当前页数,$scope.pageSize 是每页显示的数据量,$scope.totalPages 是总页数。在 HTML 中,我们使用 ng-repeat 指令和自定义过滤器来实现分页效果,并通过按钮来控制页码的变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值