91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Angular+Bootstrap+Spring Boot實現分頁功能實例代碼

發布時間:2020-09-22 17:52:22 來源:腳本之家 閱讀:251 作者:大浪中航行 欄目:web開發

需要用到的js

angular.js(用angular.min.js會導致分頁控件不顯示)

ui-bootstrap-tpls.min.js

angular-animate.js

需要用到的css

bootstrap.min.css

由于本項目使用了路由,所以講js以及css文件的應用都放在一個主html,請同學們在html頁面中添加以上文件

在開始之前,我先簡單介紹下分頁的原理。

分頁的實質其實就是一條sql語句,

  比如查找第二頁,即第16到第30條數據

  在MySQL中是select * from table limit 15,15 order by id desc

 Sql server中是select * from (select top 15 * from 
 (select top (30) * from table order by id desc) order by available asc) order by id desc
 Oracle是(oracle中的row從1開始):select * from
  (select a.*,rownum from
  (select * from tablet order by id desc) a
  ) b 
 where b.rownum between 16 and 30

一般情況下,查詢得到的數據采用倒序排序,這樣可以將用戶最新插入的數據放在最前面。

那么這三條sql語句中的這些數值是怎么計算得到的呢?它們就是根據1、CurrentPage 當前在哪一頁 2、PageSize 每頁展示多少條  來的到的,因此后臺需要從前端獲取這兩個數值。又為了告訴用戶一共有多少頁,我們還要3、TotalSize 一共多少條 。

現在有了上面1 2 3值,我們就可以來進行分頁了。在前端我們需要一個Table來幫我們展示數據,還需要一個小控件,讓用戶去選擇第幾頁,而bootstrap就為我們提供了這個小控件(uib-pagination),大大減輕了我們的工作量。在后端Jpa又為我們提供了分頁接口,我們只需要繼承JapRepository即可,零代碼量!

下面就重點看Table、uib-pagination以及JapRepository提供的接口的用法。

html頁面代碼:

<div data-ng-controller="QuestionCtrl" class="container" > 
 <br> 
 <table class="table table-bordered table-hover "> 
  <thead> 
   <tr> 
    <th class="text-center"><input type="checkbox" 
     data-ng-model="allChecked" data-ng-change="checkAll(allChecked)" /></th> 
    <th class="text-center">序號</th> 
    <th class="text-center">題目</th> 
    <th class="text-center">A</th> 
    <th class="text-center">B</th> 
    <th class="text-center">C</th> 
    <th class="text-center">D</th> 
    <th class="text-center">答案</th> 
    <th class="text-center">答題數</th> 
    <th class="text-center">正確數</th> 
    <th class="text-center">正確率</th> 
   </tr> 
  </thead> 
  <tbody> 
   <tr data-ng-repeat="item in items"> 
    <td class="text-center"><input type="checkbox" 
     data-ng-model="item.$checked" data-ng-changed="checkedChange(item.id,item.$checked)"/></td> 
    <td class="text-center"><span data-ng-bind="$index+1"></span></td> 
    <td class="text-center" 
     data-ng-bind="item.test"></td> 
    <td class="text-center" data-ng-bind="item.op1"></td> 
    <td class="text-center" data-ng-bind="item.op2"></td> 
    <td class="text-center" data-ng-bind="item.op3"></td> 
    <td class="text-center" data-ng-bind="item.op4"></td> 
    <td class="text-center" data-ng-bind="item.answer"></td> 
    <td class="text-center" data-ng-bind="item.total"></td> 
    <td class="text-center" data-ng-bind="item.totalCorrect"></td> 
    <td class="text-center"> 
    <span data-ng-if="item.total!=0" data-ng-bind="item.totalCorrect / item.total * 100 | number:2 "></span> 
    <span data-ng-if="item.total==0" data-ng-bind="0"></span> 
    %</td> 
   </tr> 
  </tbody> 
 </table> 
 <div class="text-right"> 
 <button class="btn btn-defualt"  data-ng-click="deleteItems()">刪除</button> 
 <span ><uib-pagination total-items="TotalItems" ng-model="currentPage" items-per-page = "numPerPage" max-size="maxSize" class="pagination" first-text="首頁" previous-text="上一頁" next-text="下一頁" last-text="末頁" boundary-links="true" ng-change="pageChanged()" force-ellipses="false"></uib-pagination></span> 
 </div> 
 </div> 

分頁是通過 uib-pagination 標簽來實現的,用到標簽屬性有:

total-items:表示總共有多少條記錄

items-per-page:每一頁顯示多少條記錄

max-size:決定用戶看到的頁數,即選擇頁面的按鈕,不理解的同學可以調整這個數值查看變化

ng-model:當前所在頁面

以上4個屬性的值與js雙向綁定

boundary-link:顯示“首頁”、“末頁”按鈕

force-ellipses:當值為true時,超過max-size的也會以省略號的形式展現

js代碼如下:

var app = angular.module("APP",['ui.bootstrap', 'ngAnimate']); 
app.controller('QuestionCtrl', function($scope, $uibModal, $http) { 
 <span >$scope.currentPage = 1;//當前頁 
 $scope.numPerPage = 15; 
 $scope.maxSize = 5; 
 $http({ 
  url : '/getExamsByPage', 
  method : 'post', 
  params : { 
   'currentPage' : $scope.currentPage - 1, 
   'numPerPage' : $scope.numPerPage 
  } 
 }).success(function(response) { 
  $scope.TotalItems = response.totalElements; 
  $scope.items = response.content; 
 }); 
 $scope.pageChanged = function() { 
  $http({ 
   url : '/getExamsByPage', 
   method : 'post', 
   params : { 
    'currentPage' : $scope.currentPage - 1, 
    'numPerPage' : $scope.numPerPage 
   } 
  }).success(function(response) { 
   $scope.TotalItems = response.totalElements; 
   $scope.items = response.content; 
  }); 
 }</span> 
 $scope.checkAll = function(checked) { 
  angular.forEach($scope.items, function(item) { 
   item.$checked = checked; 
  }); 
 }; 
 $scope.deleteExam = function(id) { 
  $http({ 
   url : '/deleteexam', 
   method : 'post', 
   params : { 
    'id' : id, 
    'currentPage' : $scope.currentPage - 1, 
    'numPerPage' : $scope.numPerPage 
   } 
  }).success(function(response) { 
   $scope.TotalItems = response.totalElements; 
   $scope.items = response.content; 
  }); 
 } 
 $scope.deleteItems = function() { 
  var checkedlist = new Array(); 
  angular.forEach($scope.items, function(item) { 
   if (item.$checked == true) 
    checkedlist.push(item.id); 
  }); 
  $http({ 
   url : "/deleteexams", 
   method : "post", 
   params : { 
    'ids' : checkedlist, 
    'currentPage' : $scope.currentPage - 1, 
    'numPerPage' : $scope.numPerPage 
   } 
  }).success(function(response) { 
   $scope.TotalItems = response.totalElements; 
   $scope.items = response.content; 
  }); 
 }; 
}); 

每次請求后臺需要將當前頁以及每頁的數量發送到后臺。

前臺接受到的json數據是這樣的

{"content":[{"id":225,"test":"辦公自動化是計算機的一項應用,按計算機應用分類,它屬于____。","op1":"數據處理","op2":"科學計算","op3":"實時控制","op4":"輔助設計","answer":"A","total":2,"totalCorrect":1},{"id":224,"test":"軟件由___和文檔兩部分組成。","op1":"數據","op2":"指令","op3":"程序","op4":"工具","answer":"C","total":2,"totalCorrect":1},{"id":223,"test":"為達到某一目的而編寫的計算機指令序列稱為____。","op1":"軟件","op2":"字符串","op3":"程序","op4":"命令","answer":"C","total":2,"totalCorrect":1},{"id":222,"test":"辦公自動化是計算機的一項應用,按計算機應用分類,它屬于____。","op1":"數據處理","op2":"科學計算","op3":"實時控制","op4":"輔助設計","answer":"A","total":2,"totalCorrect":1},{"id":220,"test":"為達到某一目的而編寫的計算機指令序列稱為____。","op1":"軟件","op2":"字符串","op3":"程序","op4":"命令","answer":"C","total":2,"totalCorrect":1},{"id":219,"test":"辦公自動化是計算機的一項應用,按計算機應用分類,它屬于____。","op1":"數據處理","op2":"科學計算","op3":"實時控制","op4":"輔助設計","answer":"A","total":2,"totalCorrect":1},{"id":218,"test":"軟件由___和文檔兩部分組成。","op1":"數據","op2":"指令","op3":"程序","op4":"工具","answer":"C","total":2,"totalCorrect":1},{"id":217,"test":"為達到某一目的而編寫的計算機指令序列稱為____。","op1":"軟件","op2":"字符串","op3":"程序","op4":"命令","answer":"C","total":2,"totalCorrect":1},{"id":216,"test":"辦公自動化是計算機的一項應用,按計算機應用分類,它屬于____。","op1":"數據處理","op2":"科學計算","op3":"實時控制","op4":"輔助設計","answer":"A","total":2,"totalCorrect":1},{"id":215,"test":"軟件由___和文檔兩部分組成。","op1":"數據","op2":"指令","op3":"程序","op4":"工具","answer":"C","total":2,"totalCorrect":1}],"last":false,"totalPages":9,"totalElements":86,"number":0,"size":10,"sort":[{"direction":"DESC","property":"id","ignoreCase":false,"nullHandling":"NATIVE","ascending":false}],"numberOfElements":10,"first":true}

后臺controller代碼

@RequestMapping(value = "/getExamsByPage") 
 @ResponseBody 
 public Page<Exam> getExamsByPage(@RequestParam(value = "currentPage",defaultValue = "0") Integer page, 
   @RequestParam(value = "numPerPage",defaultValue = "10") Integer pageSize) { 
  Sort sort = new Sort(Direction.DESC, "id");//設置排序方式 
  Pageable pageable = new PageRequest(page, pageSize, sort);//構建Pageable對象,改分頁查詢是通過jpa的PagingAndSortingRepository接口完成的 
  Page<Exam> Exams = examrepository.findAll(pageable); 
  return Exams; 
 } 

repository代碼:

@Transactional 
public interface ExamRepository extends JpaRepository<Exam, Integer> { 
} 

contoller中調用的findAll方法是PagingAndSortingRepository實現的,但是JpaRepository繼承自PagingAndSortingRepository,因此也有findAll方法,不明白的同學可以去查閱改接口的資料。

這樣就完成了分頁顯示,圖片如下

Angular+Bootstrap+Spring Boot實現分頁功能實例代碼

總結

以上所述是小編給大家介紹的Angular+Bootstrap+Spring Boot實現分頁功能實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

孙吴县| 荣昌县| 乾安县| 惠州市| 泰来县| 环江| 海南省| 密山市| 雷州市| 墨竹工卡县| 江阴市| 兴安县| 大港区| 通河县| 大冶市| 诏安县| 邳州市| 浏阳市| 文登市| 木兰县| 二连浩特市| 乌兰察布市| 灌云县| 密云县| 若尔盖县| 盐边县| 新昌县| 安多县| 隆回县| 靖州| 山东省| 陈巴尔虎旗| 桃园市| 电白县| 贡山| 岳西县| 汉源县| 普洱| 抚州市| 丘北县| 永善县|