AngularJS and BootStrap imitate Baidu's paging method
This article mainly introduces the sample code of AngularJS and BootStrap to imitate Baidu paging. Pagination can be used many times. Now I share it with you and give it as a reference.
Imitates Baidu’s display of 10 pieces of data per page, and implements the algorithm of centering the current page.
##
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>BootStrap+AngularJS分页显示 </title> <script type="text/javascript" src="../js/jquery.js"></script> <script type="text/javascript" src="../js/bootstrap.js"></script> <link rel="stylesheet" href="../css/bootstrap/bootstrap.css" rel="external nofollow" /> <script type="text/javascript" src="../js/angular.min.js"></script> </head> <body ng-app="paginationApp" ng-controller="paginationCtrl"> <table class="table table-bordered"> <tr> <th>序号</th> <th>商品编号</th> <th>名称</th> <th>价格</th> </tr> <tr ng-repeat="product in products"> <td>{{$index+1}}</td> <td>{{product.id}}</td> <td>{{product.name}}</td> <td>{{product.price}}</td> </tr> </table> <p> <ul class="pagination pull-right"> <li> <a href ng-click="prev()">上一页</a> </li> <li ng-repeat="page in pageList" ng-class="{active: isActivePage(page)}"> <a href ng-click="selectPage(page)">{{page}}</a> </li> <li> <a href ng-click="next()">下一页</a> </li> </ul> </p> </body> <script type="text/javascript "> var paginationApp = angular.module("paginationApp", []); paginationApp.controller("paginationCtrl", ["$scope", "$http", function($scope, $http) { // 分页组件 必须变量 $scope.currentPage = 1; // 当前页 (请求数据) $scope.pageSize = 4; // 每页记录数 (请求数据) $scope.totalCount = 0; // 总记录数 (响应数据) $scope.totalPages = 0; // 总页数 (根据 总记录数、每页记录数 计算 ) // 要在分页工具条显示所有页码 $scope.pageList = new Array(); // 加载上一页数据 $scope.prev = function(){ $scope.selectPage($scope.currentPage-1); } // 加载下一页数据 $scope.next = function(){ $scope.selectPage($scope.currentPage+1); } // 加载指定页数据 $scope.selectPage = function(page) { // page 超出范围 if($scope.totalPages != 0 && (page < 1 || page > $scope.totalPages)){ return ; } $http({ method: 'GET', url: '6_'+page+'.json', params: { "page" : page , // 页码 "pageSize" : $scope.pageSize // 每页记录数 } }).success(function(data, status, headers, config) { // 显示表格数据 $scope.products = data.products; // 根据总记录数 计算 总页数 $scope.totalCount = data.totalCount; $scope.totalPages = Math.ceil($scope.totalCount / $scope.pageSize); // 更新当前显示页码 $scope.currentPage = page ; // 显示分页工具条中页码 var begin ; // 显示第一个页码 var end ; // 显示最后一个页码 // 理论上 begin 是当前页 -5 begin = $scope.currentPage - 5 ; if(begin < 1){ // 第一个页码 不能小于1 begin = 1 ; } // 显示10个页码,理论上end 是 begin + 9 end = begin + 9; if(end > $scope.totalPages ){// 最后一个页码不能大于总页数 end = $scope.totalPages; } // 修正begin 的值, 理论上 begin 是 end - 9 begin = end - 9; if(begin < 1){ // 第一个页码 不能小于1 begin = 1 ; } // 要在分页工具条显示所有页码 $scope.pageList = new Array(); // 将页码加入 PageList集合 for(var i=begin ; i<= end ;i++){ $scope.pageList.push(i); } }).error(function(data, status, headers, config) { // 当响应以错误状态返回时调用 alert("出错,请联系管理员 "); }); } // 判断是否为当前页 $scope.isActivePage = function(page) { return page === $scope.currentPage; } // 初始化,选中第一页 $scope.selectPage(1); } ]); </script> </html>
{ "totalCount":100, "products":[ {"id":"1001","name":"苹果手机","price":"5000"}, {"id":"1002","name":"三星手机","price":"6000"} ] }
{ "totalCount":100, "products":[ {"id":"1001","name":"华为手机","price":"5000"}, {"id":"1002","name":"vivo手机","price":"6000"} ] }
// 将页码加入 PageList集合 for(var i=begin ; i<= end ;i++){ $scope.pageList.push(i); }
<li ng-repeat="page in pageList" ng-class="{active: isActivePage(page)}"> <a href ng-click="selectPage(page)">{{page}}</a> </li>
Introduction to the get and post methods based on js native and ajax and the native writing method of jsonp
How to use MIME types in the native implementation of Ajax
The above is the detailed content of AngularJS and BootStrap imitate Baidu's paging method. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to use Bootstrap to get the value of the search bar: Determines the ID or name of the search bar. Use JavaScript to get DOM elements. Gets the value of the element. Perform the required actions.

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

Use Bootstrap to implement vertical centering: flexbox method: Use the d-flex, justify-content-center, and align-items-center classes to place elements in the flexbox container. align-items-center class method: For browsers that do not support flexbox, use the align-items-center class, provided that the parent element has a defined height.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text
