Angularjs實(shí)現(xiàn)分頁(yè)和分頁(yè)算法的示例代碼
對(duì)于大多數(shù)web應(yīng)用來說顯示項(xiàng)目列表是一種很常見的任務(wù)。通常情況下,我們的數(shù)據(jù)會(huì)比較多,無法很好地顯示在單個(gè)頁(yè)面中。在這種情況下,我們需要把數(shù)據(jù)以頁(yè)的方式來展示。
頁(yè)面展示效果:

頁(yè)面HTML代碼:
<table class="table table-striped" style="margin: 0px;">
<thead>
<tr>
<td>選擇</td>
<td>企業(yè)名稱</td>
<td>企業(yè)地址</td>
<td>狀態(tài)</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="l in list">
<td><input type="radio" name="id" ng-click="select(l.id)" value="{{l.id}}" /></td>
<td>{{l.name}}</td>
<td>{{l.address}}</td>
<td>{{l.status_str}}</td>
</tr>
</tbody>
</table>
<!-- paging -->
<ul class="pagination" style="margin: 0px;" >
<li ng-class="{true:'disabled'}[p_current==1]"><a href="javascript:void(0);" ng-click="p_index()">首頁(yè)</a></li>
<li ng-repeat="page in pages" ng-class="{true:'active'}[p_current==page]"><a href="javascript:void(0);" ng-click="load_page(page)">{{ page }}</a></li>
<li ng-class="{true:'disabled'}[p_current==p_all_page]"><a href="javascript:void(0);" ng-click="p_last()">尾頁(yè)</a></li>
</ul>
<span style="vertical-align: 12px;"> 共:{{count}} 條</span>
Js代碼:
var app = angular.module("myApp",[]);
app.controller("map_ctrl",function($scope,$http,$location){
//配置
$scope.count = 0;
$scope.p_pernum = 10;
//變量
$scope.p_current = 1;
$scope.p_all_page =0;
$scope.pages = [];
//初始化第一頁(yè)
_get($scope.p_current,$scope.p_pernum,function(){
alert("我是第一次加載");
});
//獲取數(shù)據(jù)
var _get = function(page,size,callback){
$http.get("xxx.html?status=0&page="+page+"&size="+size).success(function(res){
if(res&&res.status==1){
$scope.count=res.count;
$scope.list=res.list;
$scope.p_current = page;
$scope.p_all_page =Math.ceil($scope.count/$scope.p_pernum);
reloadPno();
callback();
}else{
alert("加載失敗");
}
});
}
//單選按鈕選中
$scope.select= function(id){
alert(id);
}
//首頁(yè)
$scope.p_index = function(){
$scope.load_page(1);
}
//尾頁(yè)
$scope.p_last = function(){
$scope.load_page($scope.p_all_page);
}
//加載某一頁(yè)
$scope.load_page = function(page){
_get(page,$scope.p_pernum,function(){ });
};
//初始化頁(yè)碼
var reloadPno = function(){
$scope.pages=calculateIndexes($scope.p_current,$scope.p_all_page,8);
};
//分頁(yè)算法
var calculateIndexes = function (current, length, displayLength) {
var indexes = [];
var start = Math.round(current - displayLength / 2);
var end = Math.round(current + displayLength / 2);
if (start <= 1) {
start = 1;
end = start + displayLength - 1;
if (end >= length - 1) {
end = length - 1;
}
}
if (end >= length - 1) {
end = length;
start = end - displayLength + 1;
if (start <= 1) {
start = 1;
}
}
for (var i = start; i <= end; i++) {
indexes.push(i);
}
return indexes;
};
});
分頁(yè)算法:
current :當(dāng)前頁(yè)碼,length:總頁(yè)碼,displayLength:顯示長(zhǎng)度 @return array[1,2,3,4,5,6,7,8]
var calculateIndexes = function (current, length, displayLength) {
var indexes = [];
var start = Math.round(current - displayLength / 2);
var end = Math.round(current + displayLength / 2);
if (start <= 1) {
start = 1;
end = start + displayLength - 1;
if (end >= length - 1) {
end = length - 1;
}
}
if (end >= length - 1) {
end = length ;
start = end - displayLength + 1;
if (start <= 1) {
start = 1;
}
}
for (var i = start; i <= end; i++) {
indexes.push(i);
}
return indexes;
};
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
angularjs+bootstrap實(shí)現(xiàn)自定義分頁(yè)的實(shí)例代碼
本篇文章主要介紹了angularjs+bootstrap實(shí)現(xiàn)自定義分頁(yè)的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
AngularJS基于MVC的復(fù)雜操作實(shí)例講解
下面小編就為大家分享一篇AngularJS基于MVC的復(fù)雜操作實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
簡(jiǎn)述AngularJS相關(guān)的一些編程思想
這篇文章主要介紹了AngularJS相關(guān)的一些編程思想,AngularJS是一款熱門的JavaScript庫(kù),推薦!需要的朋友可以參考下2015-06-06
AngularJs用戶登錄問題處理(交互及驗(yàn)證、阻止FQ處理)
這篇文章主要為大家詳細(xì)介紹了AngularJs用戶登錄問題處理,包括交互及驗(yàn)證、阻止FQ處理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Angular.js中$resource高大上的數(shù)據(jù)交互詳解
這篇文章主要給大家介紹了關(guān)于Angular.js中$resource高大上的數(shù)據(jù)交互的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用angular.js具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起看看吧。2017-07-07
使用Angular CLI進(jìn)行Build(構(gòu)建)和Serve詳解
這篇文章主要介紹了使用Angular CLI進(jìn)行Build(構(gòu)建)和Serve詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
AngularJS實(shí)現(xiàn)進(jìn)度條功能示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)進(jìn)度條功能,結(jié)合具體完整實(shí)例形式分析了AngularJS實(shí)現(xiàn)進(jìn)度條功能的原理、相關(guān)知識(shí)點(diǎn)與注意事項(xiàng),需要的朋友可以參考下2017-07-07

