AngularJS分页插件的使用

素颜马尾好姑娘i 2022-04-17 05:17 512阅读 0赞

最终效果:

在这里插入图片描述

先查全部

1.后端

我们需要建立一个实体类,PageResult.java(加上get,set方法和构造方法,实现序列化)
定义 总页数和行数

  1. package entity;
  2. import java.io.Serializable;
  3. import java.util.List;
  4. /** * 分页总结果,总页数和行数 * @author zly * */
  5. public class PageResult implements Serializable {
  6. private long total;
  7. private List rows;
  8. public PageResult(long total, List rows) {
  9. super();
  10. this.total = total;
  11. this.rows = rows;
  12. }
  13. public long getTotal() {
  14. return total;
  15. }
  16. public void setTotal(long total) {
  17. this.total = total;
  18. }
  19. public List getRows() {
  20. return rows;
  21. }
  22. public void setRows(List rows) {
  23. this.rows = rows;
  24. }
  25. }

通过controller,service,serviceimpl查询到所有数据后,最后通过alibaba的fastjson返回的是json数据,格式如下:

  1. [{
  2. "firstChar": "L",
  3. "id": 1,
  4. "name": "联想"
  5. }, {
  6. "firstChar": "Y",
  7. "id": 25,
  8. "name": "优衣库"
  9. }]

2.前台显示

前台展示需要写angularjs指令,主要是在body标签里写,然后和script里的而对应
ng-app=“pinyougou”——可以写成项目名称

ng-controller=“brandController”——可以写成模块名称

ng-init=“findAll()”——作用是随页面加载

ng-repeat=“entity in list”——遍历,相当于jstl表达式中的

ng-click=“findPage()”——事件,相当于js里的onclick

js部分:
  1. <script type="text/javascript" src="../plugins/angularjs/angular.min.js"></script>
  2. <script type="text/javascript">
  3. var app = angular.module('pinyougou', [ '' ]);
  4. app.controller('brandController', function($scope, $http) {
  5. //查询所有的品牌
  6. $scope.findAll = function() {
  7. $http.get('../brand/findAll.do').success(function(response) {
  8. $scope.list = response;
  9. });
  10. }
  11. });
  12. </script>
body部分:
  1. <body class="hold-transition skin-red sidebar-mini" ng-app="pinyougou" ng-controller="brandController" ng-init="findAll()">
  2. <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
  3. <thead>
  4. <tr>
  5. <th class="" style="padding-right: 0px"><input id="selall" type="checkbox" class="icheckbox_square-blue"></th>
  6. <th class="sorting_asc">品牌ID</th>
  7. <th class="sorting">品牌名称</th>
  8. <th class="sorting">品牌首字母</th>
  9. <th class="text-center">操作</th>
  10. </tr>
  11. </thead>
  12. <tbody>
  13. <tr ng-repeat="entity in list">
  14. <td><input type="checkbox"></td>
  15. <td>{
  16. {entity.id}}</td>
  17. <td>{
  18. {entity.name}}</td>
  19. <td>{
  20. {entity.firstChar}}</td>
  21. <td class="text-center">
  22. <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal">修改</button>
  23. </td>
  24. </tr>
  25. </tbody>
  26. </table>
  27. </body>

后处理分页

1.后端

通过传(int pageNum, int pageSize)两个参数(当前第几页,每个个数),通过方法进接口,再进实现类,通过分页插件,得到想要的分页数据。这里仅贴出实现类serviceimpl的代码:

  1. package com.pinyougou.sellergoods.service.impl;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import com.alibaba.dubbo.config.annotation.Service;
  5. import com.github.pagehelper.Page;
  6. import com.github.pagehelper.PageHelper;
  7. import com.pinyougou.mapper.TbBrandMapper;
  8. import com.pinyougou.pojo.TbBrand;
  9. import com.pinyougou.sellergoods.service.BrandService;
  10. import entity.PageResult;
  11. @Service
  12. public class BrandServiceImpl implements BrandService {
  13. @Autowired
  14. private TbBrandMapper brandMapper;
  15. //查询全部品牌
  16. @Override
  17. public List<TbBrand> findAll() {
  18. return brandMapper.selectByExample(null);
  19. }
  20. //分页查询
  21. @Override
  22. public PageResult findPage(int pageNum, int pageSize) {
  23. //mybatis的分页插件
  24. PageHelper.startPage(pageNum, pageSize);
  25. //查数据,返回成Page,强转一下
  26. Page<TbBrand> page = (Page<TbBrand>) brandMapper.selectByExample(null);
  27. //返回值
  28. return new PageResult(page.getTotal(), page.getResult());
  29. }
  30. }

2.前台部分

js部分

先引入angularjs的分页插件
插件下载:https://download.csdn.net/download/qq_37796017/10782452

  1. <!-- 分页组件开始 -->
  2. <script type="text/javascript" src="../plugins/angularjs/pagination.js"></script>
  3. <link rel="stylesheet" href="../plugins/angularjs/pagination.css">
  4. <!-- 分页组件结束 -->
  5. <script type="text/javascript">
  6. var app = angular.module('pinyougou', [ 'pagination' ]);
  7. app.controller('brandController', function($scope, $http) {
  8. //查询所有的品牌
  9. $scope.findAll = function() {
  10. $http.get('../brand/findAll.do').success(function(response) {
  11. $scope.list = response;
  12. });
  13. }
  14. //初始化和点击时调用刷新列表
  15. $scope.paginationConf = {
  16. currentPage : 1,//当前页
  17. totalItems : 10,//总数
  18. itemsPerPage : 10,//每页个数
  19. perPageOptions : [ 10, 20, 30, 40, 50 ],//分页选项
  20. onChange : function() { //当更改页码时,自动触发事件
  21. $scope.reloadlist();
  22. }
  23. };
  24. //刷新列表
  25. $scope.reloadlist = function() {
  26. $scope.findPage($scope.paginationConf.currentPage,
  27. $scope.paginationConf.itemsPerPage);
  28. }
  29. //分页
  30. $scope.findPage = function(pageNum, pageSize) {
  31. $http.get(
  32. '../brand/findPage.do?pageNum=' + pageNum + '&pageSize='
  33. + pageSize + '').success(function(response) {
  34. $scope.list = response.rows;//显示当前页数据
  35. $scope.paginationConf.totalItems = response.total;//更新总记录数
  36. });
  37. }
  38. });
  39. </script>
body部分

1.在table下加上,用来调用和显示分页

  1. <tm-pagination conf="paginationConf"></tm-pagination>

2.把body标签里的 ng-init=“findAll()” 去掉

发表评论

表情:
评论列表 (有 0 条评论,512人围观)

还没有评论,来说两句吧...

相关阅读

    相关 mybatis使用

    现在实现简单的分页查询没有以前手写代码那么复杂了,以前写分页查询,我们需要设置好多的属性,并且还需要计算才能得出我们想要的页面数据,而现在我们只需要引入一个插件就可以了,实现m