<!DOCTYPE html>
<html ng-app='app'>
<head>
<script src="../ajax/jquery-ui-1.9.0/js/jquery-1.8.2.js"></script>
<script src="angular.js"></script>
<style>
tr, td{
cursor:pointer;
text-align: left;
padding-top: 2px !important;
padding-right: 2px !important;
padding-bottom: 2px !important;
padding-left: 2px !important;
vertical-align: top;
}
tr.title{
color: #ffffff;
border-bottom-color: #ffffff;
font-weight: normal;
border-bottom-width: 1px;
border-bottom-style: solid;
background-color: rgb(153,173,194);
}
.odd {
color: #000000;
//border-bottom-color: #ffffff;
border-bottom-width: 1px;
border-bottom-style: solid;
background-color: rgb(219,228,237);
}
.even{
color: #000000;
//border-bottom-color: #ffffff;
border-bottom-width: 5px;
border-bottom-style: solid;
background-color: rgb(191,204,217);
}
a:link{
color: #003399;
background-color: transparent;
}
body{
color: #000000;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
background-color: white;
}
pageDiv{
width: 100%;
height: 20px;
text-align: left;
line-height: 20px;
padding-right: 0px;
padding-left: 0px;
float: right;
background-image: none;
background-attachment: scroll;
background-repeat: repeat;
background-position-x: 0%;
background-position-y: 0%;
background-size: auto;
background-origin: padding-box;
background-clip: border-box;
background-color: rgb(219, 228, 238);
}
a, a:visited, a:hover{
text-decoration: none;
}
}
</style>
</head>
<body ng-controller="MainCtrl">
<table style="width:100%;" >
<tr >
<td colSpan="4" style="width:100%;background-color: rgb(219, 228, 238);">
<pagediv row-page="rowPage" page-Num-per-line='pageNumPerLine' total-row="datas.length" current-page="currentPage" ></pagediv>
</td>
</tr>
<tr class="title">
<td><a href="#" ng-click="orderProp = 'id'; reverse=!!!reverse">ID</a></td>
<td><a href="#" ng-click="orderProp = 'name'; reverse=!reverse">Name</a></td>
<td><a href="#" ng-click="orderProp = 'email'; reverse=!reverse">Email</a></td>
<td><a href="#" ng-click="orderProp = 'phone'; reverse=!reverse">Phone</td></tr>
<tr ng-class-odd="'odd'" ng-class-even="'even'" ng-repeat=" data in ( (datas | orderBy:orderProp:reverse).slice(currentPage * rowPage - rowPage ,currentPage * rowPage )) " >
<td style="width:20%;">{{data.id}}</td>
<td style="width:20%;">{{data.name}}</td>
<td style="width:40%;">{{data.email}}</td>
<td style="width:20%;">{{data.phone}}</td>
</tr>
<tr >
<td colSpan="4" style="width:100%;background-color: rgb(219, 228, 238);">
<pagediv row-page="rowPage" page-Num-per-line='pageNumPerLine' total-row="datas.length" current-page="currentPage" ></pagediv>
</td>
</tr>
<!--
<tr>
<td colSpan="4" style="width:100%;background-color: rgb(219, 228, 238);">
<div pagediv row-page="rowPage" page-Num-per-line='pageNumPerLine' total-row="datas.length" current-page="currentPage" ></div>
</td>
</tr>
<tr>
<td colSpan="4" style="width:100%;background-color: rgb(219, 228, 238);">
<div class="pagediv" row-page="rowPage" page-Num-per-line='pageNumPerLine' total-row="datas.length" current-page="currentPage" ></div>
</td>
</tr> -->
</table>
Current Page:<input type="text" name="dd" ng-model="currentPage" />
Rows per Page:<input type="text" name="dds" ng-model="rowPage" />
<script>
var app = angular.module('app', []);
var ctrl = app.controller('MainCtrl', function($scope,$log) {
try{
$scope.rowPage = 20;// how many rows are shown in one page
$scope.pageNumPerLine = 8; //max page is 10 on one line, ie. << < 1, 2,3,..., 10 > >>
$scope.currentPage =1 ; // initinal value is 1
$scope.datas =[];
for (var i=0; i <1000; i++){
$scope.datas[i]={id: i+1, name:'test '+(i+1), email:'test'+(i+1)+"@test.com", phone:(i+1)+''+(i+1)+''+(i+1)};
}
//$scope.orderProp = '+id'; //+ order by asc, - order by desc
}catch(e){alert(e);}
});
app.directive('pagediv', function($timeout) {
return {
restrict: 'EAC', //E-elemetn , A-attribute, C-class, M-comment
replace: false,
scope: //false, can access by other controller
/*
1.默认 scope: false – directive 不会创建新的 Scope,所以没有原型继承。这看上去很简单,但也很危险,因为你会以为 directive 在 Scope 中创建了一个新的属性,而实际上它只是用到了一个已存在的属性。这对编写可复用的模块和组件来说并不好。
2.scope: true – 这时 directive 会创建一个新的子 scope 并继承父 scope。如果在同一个 DOM 节点上有多个 directive 都要创建新 scope,则只有一个新 Scope 会创建。因为有正常的原型继承,所以和 ng-include, ng-switch 一样要注意基础类型数据的双向绑定,子 Scope 属性会覆盖父 Scope 同名属性。
3.scope: { ... } – 这时 directive 创建一个独立的 scope,没有原型继承。这在编写可复用的模块和组件时是比较好的选择,因为 directive 不会不小心读写父 scope。然而,有时候这类 directives 又经常需要访问父 scope 的属性。对象散列(object hash)被用来建立这个独立 Scope 与父 Scope 间的双向绑定(使用 ‘=’)或单向绑定(使用 ‘@’)。还有一个 ‘&’ 用来绑定父 Scope 的表达式。这些统统从父 Scope 派生创建出本地的 Scope 属性。注意,HTML 属性被用来建立绑定,你无法在对象散列中引用父 Scope 的属性名,你必须使用一个 HTML 属性。例如,<div my-directive> 和scope: { localProp: '@parentProp' } 是无法绑定父属性 parentProp 到独立 scope的,你必须这样指定:<div my-directive the-Parent-Prop=parentProp> 以及 scope: { localProp: '@theParentProp' }。
*/
{
pages: '@paginatePages',
localRowPage: '=rowPage',
localTotalRow: '=totalRow',
localCurrentPage: '=currentPage',
localPageNumPerLine: '=pageNumPerLine' // localModel = attr name
},
template: '<div></div>',
controller: function($scope, $log, $element, $attrs) {
$scope.localPageNumPerLine = $scope.localPageNumPerLine || 8;
$scope.currentStart = 1;
$scope.currentEnd = $scope.localPageNumPerLine;
$scope.localCurrentPage = 1;
try{
$scope.totalPage = $scope.localTotalRow % $scope.localRowPage == 0 ? Math.floor($scope.localTotalRow / $scope.localRowPage) : Math.ceil($scope.localTotalRow / $scope.localRowPage) ;
$scope.currentStart = $scope.totalPage == 1 ? 1 : $scope.currentStart;
$scope.currentEnd = $scope.totalPage == 1 ? 1 : $scope.currentEnd;
}catch(e){$log.info(e);}
$scope.selectPage = function(pageIndex) {
$scope.localCurrentPage = pageIndex;
$scope.rightPageNum = (Math.floor($scope.localPageNumPerLine / 2) - 1) || 1 ;
$scope.currentEnd = Math.floor(pageIndex) +$scope.rightPageNum < $scope.totalPage ? (Math.floor(pageIndex) +$scope.rightPageNum < $scope.localPageNumPerLine? $scope.localPageNumPerLine : Math.floor(pageIndex) +$scope.rightPageNum ): $scope.totalPage;
$scope.currentStart = $scope.currentEnd - $scope.localPageNumPerLine + 1;
$scope.currentStart = $scope.currentStart < 1 ? 1 : $scope.currentStart;
try{
$scope.draw();
$scope.dataBind();
}catch(e){}
}
$scope.appendItem = function(pageIndex, txt) {
var link;
var prev=false, last=false;
try{
prev = pageIndex > 1 || $scope.localCurrentPage > 1 ?true: false;
last = pageIndex > 1 && pageIndex < $scope.totalPage ? true:false;
txt = txt || pageIndex+ (pageIndex == $scope.currentEnd? " " : ", ");
if (prev || last ){
if ( pageIndex == $scope.localCurrentPage){
link = $("<span >" + txt + "</span>");
}else{
link = $("<a href='#' ng-click='selectPage("+pageIndex+")' >" + txt + "</a>");
}
}else{
link = $("<span >" + txt + "</span>");
}
}catch(e){alert(e);}
$element.append(link);
} //end appendItem
$scope.dataBind = function(){
try{
var $injector = angular.injector(['ng']); //modules['ng','ngLocal','app'] ng-app='app'
$injector.invoke(function($compile) {
$compile($element)($scope);
$scope.$digest();
});
}catch(e){
log("dataBind error: "+e);
throw e;
}
}
$scope.draw = function() {
$($element).empty();
$log.info("start:"+$scope.currentStart+" end:"+$scope.currentEnd+" current:"+$scope.localCurrentPage);
// Generate Prev link
if ($scope.totalPage > 1 && $scope.localPageNumPerLine > 1) {
$scope.appendItem(1 ,"<< ");
$scope.appendItem(Math.ceil($scope.localCurrentPage) - 1 , "< ");
}
// Generate interval links
for (var i = $scope.currentStart; i <= $scope.currentEnd; i++) {
$scope.appendItem(i);
}
// Generate Next link
if ($scope.totalPage > 1 && $scope.localPageNumPerLine > 1) {
$scope.appendItem($scope.localCurrentPage == $scope.totalPage ? $scope.totalPage : Math.ceil($scope.localCurrentPage)+ 1, "> ");
$scope.appendItem($scope.totalPage, ">>");
}
}
}, //end controller
link: function(scope, element, attrs, controller) {
scope.$watch('localCurrentPage',function(){scope.selectPage(scope.localCurrentPage);});
}
}
});
</script>
</body>
</html>