一、Vue分頁實現
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <!-- 新 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css"> <style type="text/css"> table thead tr th { text-align: center; } </style> </head> <body> <div style="padding:20px;" id="app"> <div class="panel panel-primary"> <div class="panel-heading">用戶管理</div> <table class="table table-bordered table-striped text-center"> <thead> <tr> <th>用戶名</th> <th>年齡</th> <th>畢業學校</th> <th>備注</th> </tr> </thead> <tbody> <template v-for="(row, index) in rows "> <tr v-if="index>=(curpage-1)*pagesize&&index<curpage*pagesize"> <td>{{row.Name}}</td> <td>{{row.Age}}</td> <td>{{row.School}}</td> <td>{{row.Remark}}</td> </tr> </template> </tbody> </table> </div> <nav style="float:right;"> <ul class="pagination pagination-lg"> <template v-for="page in Math.ceil(rows.length/pagesize)"> <li v-on:click="PrePage()" id="prepage" v-if="page==1" class="disabled"><a href="#">上一頁</a></li> <li v-if="page==1" class="active" v-on:click="NumPage(page, $event)"><a href="#">{{page}}</a></li> <li v-else v-on:click="NumPage(page, $event)"><a href="#">{{page}}</a></li> <li id="nextpage" v-on:click="NextPage()" v-if="page==Math.ceil(rows.length/pagesize)"><a href="#">下一頁</a></li> </template> </ul> </nav> </div> <script src="http://libs.baidu.com/jquery/1.9.0/jquery.min.js"></script> <!-- <script src="vue.js"></script> --> <script src="https://unpkg.com/vue@2.1.6/dist/vue.js"></script> <script type="text/javascript"> //Model var data = { rows: [ { Id: 1, Name: '小明', Age: 18, School: '光明小學', Remark: '三好學生' }, { Id: 2, Name: '小剛', Age: 20, School: '復興中學', Remark: '優秀班干部' }, { Id: 3, Name: '吉姆格林', Age: 19, School: '光明小學', Remark: '吉姆做了汽車公司經理' }, { Id: 4, Name: '李雷', Age: 25, School: '復興中學', Remark: '不老實的家伙' }, { Id: 5, Name: '韓梅梅', Age: 22, School: '光明小學', Remark: '在一起' }, { Id: 6, Name: '韓梅梅', Age: 22, School: '光明小學', Remark: '在一起' }, { Id: 7, Name: '韓梅梅', Age: 22, School: '光明小學', Remark: '在一起' }, { Id: 8, Name: '韓梅梅', Age: 22, School: '光明小學', Remark: '在一起' }, { Id: 9, Name: '韓梅梅', Age: 22, School: '光明小學', Remark: '在一起' }, { Id: 11, Name: '韓梅梅', Age: 22, School: '光明小學', Remark: '在一起' }, ], pagesize: 3, curpage:1,//當前頁的頁碼 }; //ViewModel var vue = new Vue({ el: '#app', data: data, methods: { //上一頁方法 PrePage: function (event) { $(".pagination .active").prev().trigger("click"); }, //下一頁方法 NextPage: function (event) { $(".pagination .active").next().trigger("click"); }, //點擊頁碼的方法 NumPage: function (num, event) { if (this.curpage == num) { return; } this.curpage = num; $(".pagination li").removeClass("active"); if (event.target.tagName.toUpperCase() == "LI") { $(event.target).addClass("active"); } else { $(event.target).parent().addClass("active"); } if (this.curpage == 1) { $("#prepage").addClass("disabled"); } else { $("#prepage").removeClass("disabled"); } if (this.curpage == Math.ceil(this.rows.length / this.pagesize)) { $("#nextpage").addClass("disabled"); } else { $("#nextpage").removeClass("disabled"); } } }, ready:function(){ var s = JSON.stringify(this.rows); console.log(JSON.parse(s)) } }); </script> </body> </html>