1 vue实现分页组件

paginate组件

  1. <template>
  2. <div class="pagination-wrap" v-cloak v-if="totalPage!=0">
  3. <ul class="pagination">
  4. <li :class="currentPage==1?'disabled':''"><a href="javascript:;" @click="turnToPage(1,pageInfo.pagesize)">首页</a></li>
  5. <li :class="currentPage==1?'disabled':''"><a @click="turnToPage(currentPage-1,pageInfo.pagesize)" href="javascript:;">上一页</a></li>
  6. <li><a href="javascript:;" @click="turnToPage(currentPage-2,pageInfo.pagesize)" v-text="currentPage-2" v-if="currentPage-2>0"></a></li>
  7. <li><a href="javascript:;" @click="turnToPage(currentPage-1,pageInfo.pagesize)" v-text="currentPage-1" v-if="currentPage-1>0"></a></li>
  8. <li class="active"><a href="javascript:;" @click="turnToPage(currentPage,pageInfo.pagesize)" v-text="currentPage">3</a></li>
  9. <li><a href="javascript:;" @click="turnToPage(currentPage+1,pageInfo.pagesize)" v-text="currentPage+1" v-if="currentPage+1<=totalPage"></a></li>
  10. <li><a href="javascript:;" @click="turnToPage(currentPage+2,pageInfo.pagesize)" v-text="currentPage+2" v-if="currentPage+2<=totalPage"></a></li>
  11. <li :class="currentPage==totalPage?'disabled':''"><a href="javascript:;" @click="turnToPage(currentPage+1,pageInfo.pagesize)" >下一页</a></li>
  12. <li :class="currentPage==totalPage?'disabled':''"><a href="javascript:;" @click="turnToPage(totalPage,pageInfo.pagesize)">尾页</a></li>
  13. <li class="curPagesize">
  14. <select v-model="pageInfo.pagesize">
  15. <option value="10">10</option>
  16. <option value="15">15</option>
  17. <option value="20">20</option>
  18. <option value="50">50</option>
  19. </select>
  20. </li>
  21. </ul>
  22. <small class="small nowrap"> 当前第 <span class="text-primary" v-text="currentPage"></span> 页,共有 <span class="text-primary" v-text="totalPage"></span></small>
  23. <div class="go">
  24. <div :class="isPageNumberError?'input-group error':'input-group'">
  25. <input class="form-control" type="number" min="1" max="totalPage" v-model="goToPage"><a href="javascript:;" class="input-group-addon" @click="turnToPage(goToPage,pageInfo.pagesize)">Go</a>
  26. </div>
  27. </div>
  28. </div>
  29. </template>
  30.  
  31. <script type="text/javascript">
  32. export default {
  33. props: {
  34. //传入总条数,默认0,可以对传入的值进行自定义的验证,具体方式如下
  35. total: {
  36. type: Number,
  37. default: 0,
  38. validator(value) {
  39. return value >= 0
  40. }
  41. },
  42. //传入每页的条数,默认为10
  43. currentPagesize:{
  44. type: Number,
  45. default: 10,
  46. validator(value) {
  47. return value >= 0
  48. }
  49. },
  50. //传入当前页码,默认为1
  51. fatherCurrentPage:{
  52. type: Number,
  53. default: 1,
  54. validator(value) {
  55. return value >= 0
  56. }
  57. }
  58. },
  59. data(){
  60. return {
  61. isPageNumberError: false,
  62. goToPage:'',
  63. totalPage:'',
  64. childCurrentPage:this.fatherCurrentPage,
  65. pageInfo:{
  66. pagesize:this.currentPagesize,
  67. pageNum:this.fatherCurrentPage
  68. }
  69. }
  70. },
  71. computed:{
  72. // prop不应该在组件内部做改变
  73. // 所以我们这里设置一个内部计算属性currentPage来代替props中的myCurrentPage
  74. currentPage(){
  75. return this.childCurrentPage;
  76. },
  77. pagesizeCp(){
  78. return this.pageInfo.pagesize
  79. }
  80. },
  81. created:function(){
  82. this.totalPage = Math.ceil(this.total/this.pageInfo.pagesize);
  83. },
  84. watch:{
  85. pagesizeCp:function(curVal,oldVal){
  86. console.log("new:"+curVal+",old:"+oldVal);
  87. this.childCurrentPage = Math.ceil(this.childCurrentPage*oldVal/curVal);
  88. this.totalPage = Math.ceil(this.total/curVal);
  89. if(this.childCurrentPage > this.totalPage){
  90. this.childCurrentPage = this.totalPage;
  91. }
  92. this.turnToPage(this.pageInfo.pageNum,this.pageInfo.pagesize);
  93. }
  94.  
  95. },
  96. methods:{
  97. //turnToPage为跳转到某页
  98. //传入参数pageNum为要跳转的页数,pagesize为每页多少条信息
  99. turnToPage( pageNum ,pagesize){
  100. var _self = this;
  101. var pageNum = parseInt(pageNum);
  102. var pagesize = parseInt(pagesize);
  103. //页数不合法则退出
  104. if (!pageNum || pageNum > _self.totalPage || pageNum < 1) {
  105. // console.log('页码输入有误!');
  106. _self.isPageNumberError = true;
  107. return false;
  108. }else{
  109. _self.isPageNumberError = false;
  110. }
  111. //更新当前页
  112. _self.childCurrentPage = pageNum;
  113. //数据变化时将页码信息传回给父组件
  114. this.pageInfo.pageNum = pageNum;
  115. this.pageInfo.pagesize =pagesize;
  116. // 将最终获取到的页码信息传递个父组件的getInfo函数
  117. this.$emit('getInfo',this.pageInfo);
  118. }
  119. }
  120. }
  121. </script>
  122.  
  123. <style type="text/css">
  124. .pagination-wrap{
  125. margin: 0 auto;
  126. }
  127. .pagination {
  128. display: inline-block;
  129. padding-left: 0;
  130. border-radius: 4px;
  131. }
  132. .small {
  133. margin: 0 10px;
  134. position: relative;
  135. }
  136. .nowrap {
  137. white-space: nowrap;
  138. }
  139. .input-group {
  140. position: relative;
  141. display: table;
  142. border-collapse: separate;
  143. }
  144. .input-group-addon {
  145. padding: 6px 12px;
  146. font-size: 14px;
  147. font-weight: 400;
  148. line-height: 1;
  149. color: #555;
  150. text-align: center;
  151. background-color: #eee;
  152. border: 1px solid #ccc;
  153. border-radius: 0 4px 4px 0;
  154. }
  155.  
  156. .input-group-addon, .input-group-btn {
  157. width: 1%;
  158. white-space: nowrap;
  159. vertical-align: middle;
  160. }
  161. .input-group-addon, .input-group-btn, .input-group .form-control {
  162. box-sizing: border-box;
  163. display: table-cell;
  164. }
  165. .input-group .form-control:first-child, .input-group-addon:first-child, .input-group-btn:first-child>.btn, .input-group-btn:first-child>.btn-group>.btn, .input-group-btn:first-child>.dropdown-toggle, .input-group-btn:last-child>.btn:not(:last-child):not(.dropdown-toggle), .input-group-btn:last-child>.btn-group:not(:last-child)>.btn {
  166. border-top-right-radius: 0;
  167. border-bottom-right-radius: 0;
  168. }
  169. .input-group-addon, .input-group-btn, .input-group .form-control {
  170. display: table-cell;
  171. }
  172. .input-group .form-control {
  173. position: relative;
  174. z-index: 2;
  175. float: left;
  176. width: 100%;
  177. margin-bottom: 0;
  178. }
  179. .go .error .form-control{
  180. border: 1px solid #d95656;
  181. }
  182. .form-control {
  183. display: block;
  184. width: 100%;
  185. height: 34px;
  186. padding: 6px 12px;
  187. font-size: 14px;
  188. line-height: 1.42857143;
  189. color: #555;
  190. background-color: #fff;
  191. background-image: none;
  192. border: 1px solid #ccc;
  193. border-radius: 4px;
  194. -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
  195. box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
  196. -webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;
  197. -o-transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
  198. transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
  199. }
  200. .text-primary {
  201. color: #428bca;
  202. }
  203. .pagination>li:first-child>a, .pagination>li:first-child>span {
  204. margin-left: 0;
  205. border-top-left-radius: 4px;
  206. border-bottom-left-radius: 4px;
  207. }
  208. .go {
  209. display: inline-block;
  210. max-width: 120px;
  211. top: 16px;
  212. position: relative;
  213. }
  214. .input-group-addon:last-child {
  215. display: table-cell;
  216. text-decoration: none;
  217. border-left: 0;
  218. }
  219. .pagination>.disabled>span, .pagination>.disabled>span:hover, .pagination>.disabled>span:focus, .pagination>.disabled>a, .pagination>.disabled>a:hover, .pagination>.disabled>a:focus {
  220. color: #777;
  221. cursor: not-allowed;
  222. background-color: #fff;
  223. border-color: #ddd;
  224. }
  225. .pagination>li:last-child>a, .pagination>li:last-child>span {
  226. border-top-right-radius: 4px;
  227. border-bottom-right-radius: 4px;
  228. }
  229. .pagination>.active>a, .pagination>.active>span, .pagination>.active>a:hover, .pagination>.active>span:hover, .pagination>.active>a:focus, .pagination>.active>span:focus {
  230. z-index: 2;
  231. color: #fff;
  232. cursor: default;
  233. background-color: #428bca;
  234. border-color: #428bca;
  235. }
  236. .pagination>li>a, .pagination>li>span{
  237. position: relative;
  238. float: left;
  239. padding: 6px 12px;
  240. margin-left: -1px;
  241. line-height: 1.42857143;
  242. color: #428bca;
  243. text-decoration: none;
  244. background-color: #fff;
  245. border: 1px solid #ddd;
  246. }
  247. .curPagesize{
  248. width:42px;
  249. color:blue;
  250. margin-left:5px;
  251. }
  252. .pagination>li>select{
  253. width:50px;
  254. height:33px !important;
  255. line-height:31px;
  256. padding:0 5px;
  257. margin-bottom:6px;
  258. border-color: #ddd;
  259. border-radius:5px;
  260.  
  261. }
  262. .pagination>li {
  263. display: inline;
  264. }
  265. </style>

在父组件中进行引用

  1. <template>
  2. <div id = "outletapp">
  3. <div class="data-body">
  4. <paginate :total='parentTotal' :currentPagesize="parentPagesize" :fatherCurrentPage="parentCurrentPage" @getInfo="getPageInfo"></paginate>
  5. </div>
  6. </div>
  7. </template>
  8. <script>
  9. //引入需要的组件
  10. import paginate from '../components/paginate.vue'
  11. var vm = {
  12. name:'**',
  13. data:function(){
  14. return {
  15. parentTotal:0,
  16. parentCurrentPage:1,
  17. parentPagesize:10,
  18. queryParams:{
  19. otherParams:'',
  20. pagesize:10,
  21. pageNum:1
  22. }
  23. }
  24. },
  25. watch:{
  26. queryParams:{
  27. handler:function(newVal,oldVal){
  28. this.parentCallback(newVal);
  29. },
  30. deep:true
  31. }
  32. },
  33. methods:{
  34. querySearch:function(){
  35. console.log('search');
  36. },
  37. // 更新pageNum和pagesize重新渲染页面
  38. parentCallback:function(objParams){
  39. // 此处为获取的参数信息,包括查询信息和页码信息,可以根据这些参数信息进行请求数据
  40. console.log(objParams);
  41. // this.$http.post('url地址',objParams,{emulateJSON:true}).then(function(res){
  42. var res= jsontest2;
  43. this.parentTotal = parseInt(res.total);
  44. },function(res){
  45. var tem = JSON.parse(res.bodyText);
  46. alert(tem.desc);
  47. })
  48. },
  49. getPageInfo:function(obj){
  50. this.queryParams.pagesize = obj.pagesize;
  51. this.queryParams.pageNum = obj.pageNum;
  52. // parentCallback(this.queryParams);
  53. }
  54. },
  55. beforeCreate:function(){
  56. // 初始化请求一下数据
  57. var res= jsontest2;
  58. var tem = res;
  59. this.itemList = tem.rows;
  60. this.parentTotal = parseInt(tem.total);
  61. },
  62. created:function(){
  63. var _self = this;
  64. _self.parentCallback(_self.queryParams);
  65. },
  66. components:{
  67. paginate
  68. }
  69. }
  70. export default vm
  71. </script>
  72. <style>
  73. </style>

  

angularjs实现分页指令

  1. .directive('paginate', ['$http', function($http){
  2. return {
  3. scope: {
  4. query: '&',
  5. params: '=',
  6. result: '=',
  7. page: '=',
  8. loading: '='
  9. },
  10. restrict: 'E',
  11. template: [
  12. '<div style="margin-bottom: 40px;margin-left: 20px; margin-top: 20px;" ng-cloak>',
  13. '<button ng-click="go(1)" class="p-btn p-btn-active margin_r10">首页</button>',
  14. '<button ng-click="pre()" title="上一页" class="p-btn p-btn-active margin_r10"><<</button>',
  15. '<button class="p-btn margin_r10" title="{{code}}" ng-click="go(code)" ng-class="{\'p-btn-active\': code == params.pageNum}" ng-repeat="code in codes">{{code}}</button>',
  16. '<button ng-click="next()" title="下一页" class="p-btn p-btn-active margin_r10">>></button>',
  17. '<input type="number" ng-model="goPage" min=1 max="{{pageCount}}" style="height: 24px;width: 50px;text-align: center;" />',
  18. '<button class="p-btn p-btn-active margin_r10" title="跳转至" ng-click="go()">Go</button>',
  19. '<span>共 {{pageCount}} 页, </span>',
  20. '<span>共 {{result.total}} 条记录</span>',
  21. '</div>'
  22. ].join(''),
  23. link: function($scope, iElm, iAttrs) {
  24. $scope.codeNum = 5
  25. $scope.codes = []
  26. $scope.$watch('result', function() {
  27. $scope.pageCount = getPageCount($scope.result.total, $scope.params.pageSize)
  28. $scope.codes = getCodes($scope.pageCount, parseInt($scope.params.pageNum) ,$scope.codeNum)
  29. })
  30. // 计算总页数
  31. function getPageCount(total, pageSize) {
  32. var a = total%pageSize
  33. if(a == 0) {
  34. return parseInt(total/pageSize)
  35. } else {
  36. return parseInt(total/pageSize) + 1
  37. }
  38. }
  39. // 计算页码显示
  40. function getCodes(pageCount, index, codeNum){
  41. if(pageCount <= codeNum) {
  42. return getArray(1, pageCount)
  43. } else {
  44. if(index <= (codeNum + 1)/2) {
  45. return getArray(1, codeNum)
  46. }
  47. if(index >= (pageCount- (codeNum - 1)/2)) {
  48. return getArray(pageCount-codeNum+1, pageCount)
  49. }
  50. return getArray(index-(codeNum - 1)/2, index+(codeNum - 1)/2)
  51. }
  52. }
  53.  
  54. function getArray(start, end) {
  55. var arr = []
  56. for (var i = start; i <= end; i++) {
  57. arr.push(i)
  58. }
  59. return arr
  60. }
  61.  
  62. $scope.go = function(goPage) {
  63. if(!goPage) {
  64. if($scope.goPage)
  65. goPage = $scope.goPage
  66. }
  67. if(goPage == $scope.params.pageNum) {
  68. return
  69. }
  70. if(goPage == '0' || !goPage || goPage > $scope.pageCount || goPage < 0) {
  71. return
  72. } else {
  73. $scope.page.index = goPage
  74. }
  75. $scope.query()
  76. }
  77. $scope.pre = function() {
  78. if($scope.params.pageNum < 2) {
  79. return
  80. } else {
  81. $scope.page.index = $scope.params.pageNum -1
  82. }
  83. $scope.query()
  84. }
  85. $scope.next = function() {
  86. if($scope.pageCount == 0){
  87. return
  88. }
  89. if($scope.params.pageNum == $scope.pageCount) {
  90. return
  91. } else {
  92. $scope.page.index = $scope.params.pageNum + 1
  93. }
  94. $scope.query()
  95. }
  96. }
  97. };
  98. }])

  引用时将对应的改指令文件引入,在html里面写入

  1. <paginate params="queryParams" result="queryResult" query="query()" page="paginate"></paginate>  

  对应的样式设置

  1. .p-btn {
  2. border: none;
  3. cursor: pointer;
  4. color: #333;
  5. padding: 6px 14px;
  6. font-size: 12px;
  7. display: inline;
  8. background: #fff;
  9. border: solid 1px #30363e;
  10. height:30px;
  11. }
  12. .p-btn:hover {
  13. background-color: rgba(63, 74, 89, 0.5);
  14. }
  15. .p-btn-active {
  16. background-color: #3f4a59;
  17. color: #fff;
  18. }

  

vue angular 分别实现分页的更多相关文章

  1. Vue && Angular 双向绑定检测不到对象属性的添加和删除

    由于ES5的限制 Vue  && Angular 双向绑定检测不到对象属性的添加和删除  还有数组增加索引.这些改变不会触发change事件.Vue是因为实例化的时候已经把各个属性都s ...

  2. django rest_framework vue 实现用户列表分页

    django rest_framework vue 实现用户列表分页 后端 配置urls # 导入view from api.appview.userListView import userListV ...

  3. 关于vue+element-ui项目的分页,返回默认显示第一页的问题解决

    关于vue+element-ui项目的分页,返回默认显示第一页的问题解决     问题描述 当前页面如下: 然后点击页码跳到第3页,然后在第三页点击页面链接跳转到新的页面 然后在新页面点击返回按钮,返 ...

  4. vue修改elementUI的分页组件视图没更新问题

    转: vue修改elementUI的分页组件视图没更新问题 今天遇到一个小问题平时没留意,el-pagination这个分页组件有一个属性是current-page当前页.今天想在methods里面手 ...

  5. 基于Vue.js的表格分页组件

    有一段时间没更新文章了,主要是因为自己一直在忙着学习新的东西而忘记分享了,实在惭愧. 这不,大半夜发文更一篇文章,分享一个自己编写的一个Vue的小组件,名叫BootPage. 不了解Vue.js的童鞋 ...

  6. Vue.js的表格分页组件

    转自:http://www.cnblogs.com/Leo_wl/p/5522299.html 有一段时间没更新文章了,主要是因为自己一直在忙着学习新的东西而忘记分享了,实在惭愧. 这不,大半夜发文更 ...

  7. Jenkins gitlab vue,angular,react 自动化构建【原】

    大致思路,(本篇主要讲vue ,当然了 angular react 也是一样配置) ,转发请注明原链接,谢谢 :) 1. 服务器上面配置jenkins (安装配置,不介绍) 2.新建item 自由风格 ...

  8. Angular.js实现分页

    一.编写angularJS实现的分页js(网上搜)和样式表(pagination),并在页面引入 二.编写变量和方法 //分页控件控制 $scope.paginationConf={ currentP ...

  9. 在angular中利用分页插件进行分页

    必需:angular分页js和css  当然还有angular.js   还需要bootstrap的css angular.min.js (下面我直接把插件粘贴上去了,以免有的同学还要去找.是不是很贴 ...

随机推荐

  1. PHP类名获取的几种方式及单例模式实现

    参考:https://www.cnblogs.com/water0729/p/5803217.html <?php class foo { static public function test ...

  2. Nginx或Apache通过反向代理配置wss服务

    nginx配置参考 前提条件及准备工作: 1.假设ws服务监听的是8282端口(websocket协议) 2.已经申请了证书(pem/crt文件及key文件)放在了/etc/nginx/conf.d/ ...

  3. Tensorboard简介

    Tensorflow官方推出了可视化工具Tensorboard,可以帮助我们实现以上功能,它可以将模型训练过程中的各种数据汇总起来存在自定义的路径与日志文件中,然后在指定的web端可视化地展现这些信息 ...

  4. Hybrid设计--账号体系的建设

    前后端分离:开发效率高,没有SEO 现在是重客户端设计:交互和业务逻辑是前端来写,适合做前后端分离.对前端更友好,提高了效率. 传统模式开发:整个业务逻辑是server端写,不适合做前后端分离.ser ...

  5. color xml arm相关

    #-------------------------------------------------------------------------- # C O L O R S #--------- ...

  6. Unable to update the EntitySet 'T_JsAPI' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.

    前几天使用EF6的Db First模式改造了支付中心的数据访问层,废弃了ado.net. 同时,使用T4把实体类生成到了model层的PO目录下. 今天在db里新建了一张表,在edmx文件里更新模型( ...

  7. php背景图片上生成二维码,二维码上带logo 代码示例 (原)

    依赖库文件 phpqrcode.php (下载地址://www.jb51.net/codes/189897.html :或者在官网下载:http://phpqrcode.sourceforge.net ...

  8. InstallShield2015制作安装包----------安装过程中修改文件内容

    //修改安装目录下autostart.vbs里的路径 //打开文件 OpenFileMode(FILE_MODE_NORMAL); strPath=INSTALLDIR+"centerAut ...

  9. HDU 1757 A Simple Math Problem(矩阵)

    A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...

  10. JavaScript-合同到期续约案例

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...