AngularJS實現(xiàn)分頁顯示數(shù)據(jù)庫信息
接著第一篇《》AngularJS內(nèi)建服務(wù)$location及其功能詳解》,進行學(xué)習(xí)
Section 2:實現(xiàn)分頁顯示效果
那么再隱身一下,通過location的setter方法設(shè)置當(dāng)前的url信息。在這里為了能夠讓演示看到更好的效果,在這個比較完整的實例中,我引入了angularJS的多路由技術(shù)、嵌套的控制器之間傳遞數(shù)據(jù)、scope的繼承、 http通信、內(nèi)鏈接傳遞變量等。
首先建立一個首頁模板
<!DOCTYPE html>
<html ng-app="turnPageApp">
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="lib/angular.js"></script>
<script src="lib/angular-route.min.js"></script>
<style type="text/css">
.turnPageButtonArea {
background-color: #f07a13;
width: 500px;
height: 30px;
line-height: 30px;
text-align: center;
margin: 10px auto;
padding: 20px;
}
button {
margin-right: 20px;
width: 100px;
height: 30px;
font-size: 15px;
}
.pageNum {
width: 50px;
height: 23px;
text-align: center;
}
body {
font-family: 微軟雅黑;
}
h1 {
text-align: center;
}
.totalPages {
color: #ffffff
}
</style>
</head>
<body ng-controller="indexController">
<h1>AngularJS TurnPage By $location Service</h1>
<div ng-view></div>
<div class="turnPageButtonArea">
<button ng-click="previous()">Previous</button>
<button ng-click="next()">Next</button>
<input type="number" ng-model="currentPage" class="pageNum">
<button ng-click="goToPage()">Go</button>
<span class="totalPages">共 {{allPage}} 頁</span>
</div>
</body>
</html>
通過一些簡單的CSS樣式將頁面的布局修飾了一下。
然后在首頁的元素里設(shè)置了一些ngApp和controller。
在屬性為ngView的div元素中,將嵌入其他HTML的模板。
緊接著下方,我擺放了三個按鈕,其中前兩個是上一頁和下一頁的翻頁按鈕;一個輸入框可供用戶輸入他想跳轉(zhuǎn)到的頁碼,旁邊的按鈕作為頁碼的提交按鈕,點擊后頁面立即翻頁。
這三個按鈕里面都有一個ngClick屬性,表示當(dāng)用戶點擊按鈕后,便會執(zhí)行對應(yīng)的函數(shù)。
在講解angularJS的js代碼前,先截圖看看文件的目錄結(jié)構(gòu)。

上面的index.html是之前兩個例子的,可以不去理會。
為了簡單期間,我把script腳本都放在了turnPage.html文件的下方了。下面就是全這個文件的完整代碼:
turnPage.html
<!DOCTYPE html>
<html ng-app="turnPageApp">
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="lib/angular.js"></script>
<script src="lib/angular-route.min.js"></script>
<style type="text/css">
.turnPageButtonArea {
background-color: #f07a13;
width: 500px;
height: 30px;
line-height: 30px;
text-align: center;
margin: 10px auto;
padding: 20px;
}
button {
margin-right: 20px;
width: 100px;
height: 30px;
font-size: 15px;
}
.pageNum {
width: 50px;
height: 23px;
text-align: center;
}
body {
font-family: 微軟雅黑;
}
h1 {
text-align: center;
}
.totalPages {
color: #ffffff
}
</style>
</head>
<body ng-controller="indexController">
<h1>AngularJS TurnPage By $location Service</h1>
<div ng-view></div>
<div class="turnPageButtonArea">
<button ng-click="previous()">Previous</button>
<button ng-click="next()">Next</button>
<input type="number" ng-model="currentPage" class="pageNum">
<button ng-click="goToPage()">Go</button>
<span class="totalPages">共 {{allPage}} 頁</span>
</div>
<script>
//實例化用戶自己的ngApp對象。這里因為用到了路由機制,所以在依賴注入中寫入ngRoute這個模塊
var turnPageApp = angular.module('turnPageApp', ['ngRoute']);
/*
設(shè)置對于不同的url,啟用不同的模板和控制器。
本例由于只用到了一個模板,所以遇到的路由的情況就只有一種,即 “/id”
*/
turnPageApp.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/:id', { //這里的id其實表示的是翻頁中的頁碼
templateUrl: 'view/student.html',
controller: 'StudentController'
})
.otherwise({redirectTo: '/1'});//如果沒法匹配到url時,直接跳轉(zhuǎn)會第一頁
}]);
//注冊父控制器indexController,這里由于需要將模板里的子控制器的值傳遞給父控制器,所以需要這個根域$rootScope來幫忙,在返回函數(shù)里需要傳入這個根域?qū)ο蟆?
//而且,本例是基于對url的操作來實現(xiàn)翻頁,所以這個內(nèi)建的$location服務(wù)是一定要傳入的
turnPageApp.controller('indexController', function ($rootScope, $scope, $location) {
//給父scope定義函數(shù)
$scope.previous = function () {
//從瀏覽器的地址欄獲取路徑,即turnPage.html#/1中井號后面的內(nèi)容:“ /1 ”
//然后通過JavaScript的silce函數(shù)取出斜杠后面的字符,并轉(zhuǎn)換成數(shù)字。
//加 1 還是減 1 要看是在定義的是哪個按鈕的功能函數(shù)了
var pageNum = parseInt($location.path().slice(1)) - 1;
//頁碼是有限的,需要做一些判斷
if (pageNum < 1) {
alert('This is the first page');
} else {
//如果現(xiàn)在沒有處在第一頁,則path屬性減去1,即向前翻一頁。這個翻頁的效果就是通過設(shè)置url中的path來實現(xiàn)
$location.path(pageNum);
}
};
//和上面的previous()函數(shù)類似
$scope.next = function () {
var pageNum = parseInt($location.path().slice(1)) + 1;
if (pageNum > $rootScope.allPage) {
alert('This is the last page');
} else {
$location.path(pageNum);
}
};
//這是一個直接跳轉(zhuǎn)到那個頁碼的函數(shù),在判斷的地方稍微繁瑣些
$scope.goToPage = function () {
//從input輸入框綁定的currentPage變量中獲取用戶輸入的值
var pageNum = $scope.currentPage;
//為了程序的嚴(yán)密和可用性,需要先判斷輸入是否為空
if (pageNum == null || pageNum == undefined || pageNum == "") {
alert("try to input a page number");
//如果不是空,再判斷用戶輸入的頁碼是否超出了頁碼的范圍
//這里出現(xiàn)了$rootScope這個根域及其屬性allPage,該屬性的值是頁碼的總數(shù)
} else if (!(pageNum >= 1 && pageNum <= $rootScope.allPage)) {
alert("The page number is beyond the scope of the number of the students")
} else {
//如果都沒問題了,則修改URL,此時angularJS會捕捉地址欄的變化,然后跳轉(zhuǎn),細(xì)節(jié)將在下面講解。
$location.path(pageNum);
}
};
});
//這里實在注冊嵌入到首頁的模板頁的控制器。
//由于子域和父域的通信需要借助根域,所以在依賴中傳入$rootScope對象
//$scope是模板自己的作用域,它繼承了父控制器indexController生成的作用域
//在模板中需要與服務(wù)端的文件進行交互,所以需要引入內(nèi)建的$http服務(wù)。為了控制實例的復(fù)雜度,我直接寫了一個json文件,做了一些假數(shù)據(jù)。
//這里$routeParams是一個對象,這個對象里有一個屬性,就是我們之前在config()函數(shù)里看到的那個id(/:id),這個id是個變量,地址欄里的path是幾,這個id就是幾。id的值需要通過$routeParams這個對象來取得。
turnPageApp.controller('StudentController', ['$rootScope', '$scope', '$http', '$routeParams', function ($rootScope, $scope, $http, $routeParams) {
//$http的get方法與遠(yuǎn)程的一個文件發(fā)出請求,如果成功,則執(zhí)行一個回調(diào)函數(shù),函數(shù)的參數(shù)就是從遠(yuǎn)端文件里拿到的數(shù)據(jù),這個數(shù)據(jù)可以是個數(shù)組,也可以是個對象。
//那么我們這次拿到的是一個json數(shù)組,數(shù)組的元素是一個個的對象。
$http.get('data/students.json').success(function (data) {
//把數(shù)組里的一個元素取出來,賦給模板子作用域?qū)ο蟮膶傩陨?。由于json數(shù)組的id是從1開始寫的,而返回的數(shù)據(jù)是個數(shù)組,下標(biāo)從0開始,所以要減去1
$scope.student = data[$routeParams.id - 1];
//這里順便把這個數(shù)組的元素個數(shù)取出來,每個元素就代表以頁。那么元素總個數(shù)就代表共有多少頁。
//注意要傳遞給最高級別的根域?qū)ο?,因為子域能覆寫父域的同名屬性;子域如果沒有直接賦值,那么子域的同名屬性將繼承父域同名屬性的值;
/*我們在回到本文件代碼上面的“共 {{allPage}} 頁”處,這個就是根域$rootScope的屬性。而且在父控制器中并沒有特別的賦值。而這個span元素恰好就在父控制器的作用域內(nèi),那么這個元素里的allPage屬性就會繼承父作用域的同名屬性allPage的值,也就間接的顯示出了總頁數(shù)。
*/
$rootScope.allPage = data.length;
});
}]);
</script>
</body>
</html>
view/student.html
<table cellspacing="0">
<tr>
<td class="title">ID</td>
<td>{{student.id}}</td>
</tr>
<tr>
<td class="title">Name</td>
<td>{{student.name}}</td>
</tr>
<tr>
<td class="title">Sex</td>
<td>{{student.sex}}</td>
</tr>
<tr>
<td class="title">Age</td>
<td>{{student.age}}</td>
</tr>
<tr>
<td class="title">Courses</td>
<td>
<ul>
<li ng-repeat="course in student.courses">{{course}}</li>
</ul>
</td>
</tr>
</table>
<style type="text/css">
table {
border: solid 1px #000000;
margin: 0px auto;
}
td {
padding: 10px 10px 10px 20px;
margin: 0px;
border: solid 1px #000000;
}
tr {
margin: 0px;
padding: 0px;
}
.title {
background-color: #207ef0;
text-align: center;
color: #ffffff;
}
ul {
list-style: none;
margin: 0px;
padding: 0px;
}
li {
float: left;
margin: 10px;
}
</style>
data/students.json
[
{
"id": 1,
"name": "Frank Li",
"sex": "male",
"age": "30",
"courses": [
"HTML",
"CSS",
"Javascript",
"Java",
"PHP",
"MySQL",
"Ubuntu",
"MongoDB",
"NodeJS",
"AngularJS",
"Photoshop",
"LESS",
"Bootstrap"
]
},
{
"id": 2,
"name": "Cherry",
"sex": "female",
"age": "27",
"courses": [
"Anderson's Fairy Tales",
"Pride and Prejudice",
"Vanity Fair",
"Oliver Twist"
]
},
{
"id": 3,
"name": "John Liu",
"sex": "male",
"age": "29",
"courses": [
"iology and medical science",
"pplied biology",
"medicine",
"cell biology"
]
},
{
"id": 4,
"name": "Lucy Mu",
"sex": "female",
"age": "22",
"courses": [
"Introduction to ART ",
"sketch",
"Composition",
"sculpture"
]
}
]

這是剛開始的樣子,地址欄中默認(rèn)的path是/1,所以直接顯示了第一個學(xué)生的信息。頁碼總數(shù)也能獲得。

點擊了Previous按鈕后的效果。不能再往前翻頁了

處于第4頁時,點擊Next按鈕時的效果。不能再往后翻頁了。

在頁碼范圍內(nèi)翻頁是沒有問題的!

鑒于篇幅,我就不演示輸入的頁碼超出范圍的情況了。上面的截圖是輸入正確范圍的頁碼,點擊Go按鈕的效果。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Angular.JS內(nèi)置服務(wù)$http對數(shù)據(jù)庫的增刪改使用教程
- 三種AngularJS中獲取數(shù)據(jù)源的方式
- AngularJS實現(xiàn)數(shù)據(jù)列表的增加、刪除和上移下移等功能實例
- 基于AngularJS實現(xiàn)頁面滾動到底自動加載數(shù)據(jù)的功能
- Angularjs實現(xiàn)多個頁面共享數(shù)據(jù)的方式
- 詳解angularJs中自定義directive的數(shù)據(jù)交互
- Angular.js如何從PHP讀取后臺數(shù)據(jù)
- Angularjs 滾動加載更多數(shù)據(jù)
- 深入學(xué)習(xí)AngularJS中數(shù)據(jù)的雙向綁定機制
- 在 Angular6 中使用 HTTP 請求服務(wù)端數(shù)據(jù)的步驟詳解
- Angular.JS讀取數(shù)據(jù)庫數(shù)據(jù)調(diào)用完整實例
相關(guān)文章
Angularjs自定義指令實現(xiàn)分頁插件(DEMO)
由于最近的一個項目使用的是angularjs1.0的版本,涉及到分頁查詢數(shù)據(jù)的功能,后來自己就用自定義指令實現(xiàn)了該功能,下面小編把實例demo分享到腳本之家平臺,需要的朋友參考下2017-09-09
AngularJS實現(xiàn)自定義指令及指令配置項的方法
這篇文章主要介紹了AngularJS實現(xiàn)自定義指令及指令配置項的方法,結(jié)合實例形式簡單總結(jié)分析了AngularJS自定義指令及指令配置項的實現(xiàn)技巧,需要的朋友可以參考下2017-11-11
Web開發(fā)使用Angular實現(xiàn)用戶密碼強度判別的方法
這篇文章主要介紹了Web開發(fā)使用Angular實現(xiàn)用戶密碼強度判別的方法,需要的朋友可以參考下2017-09-09
Angular組件庫ng-zorro-antd實現(xiàn)radio單選框選擇
這篇文章主要為大家介紹了Angular組件庫ng-zorro-antd實現(xiàn)radio單選框取消選擇實現(xiàn)問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05
AngularJS基礎(chǔ) ng-click 指令示例代碼
本文介紹AngularJS ng-click 指令,這里整理了ng-click指令的基礎(chǔ)知識并且附有簡單示例代碼和實現(xiàn)效果圖,有需要的小伙伴參考下2016-08-08
angular + express 實現(xiàn)websocket通信
最近需要實現(xiàn)一個功能,后端通過TCP協(xié)議連接雷達硬件的控制器,前端通過websocket連接后端,當(dāng)控制器觸發(fā)消息的時候,把信息通知給所以前端,本文給的大家講解angular + express 實現(xiàn)websocket通信的思路,感興趣的朋友一起看看吧2023-09-09

