欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Angular+Bootstrap+Spring Boot实现分页功能实例代码

程序员文章站 2022-06-03 13:57:56
需要用到的js angular.js(用angular.min.js会导致分页控件不显示) ui-bootstrap-tpls.min.js angular-anim...

需要用到的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" style="width: 1900px;"> 
 <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" style="float: left" data-ng-click="deleteitems()">删除</button> 
 <span style="color:#ff0000;"><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 style="color:#ff0000;">$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实现分页功能实例代码,希望对大家有所帮助