1 新建 pager.js 文件
/**
* [pagination 分頁組件]
* @param {Number} total [數據總條數]
* @param {Number} display [每頁顯示條數 default:10]
* @param {Number} current [當前頁碼 default:1]
* @param {Number} pagegroup [分頁條數(奇數) default:5]
* @param {Event} pagechange [頁碼改動時 dispatch ]
* @return {[type]} [description]
*/
Vue.component('pagination', {
template: '#template_pagination',
props: {
total: { // 數據總條數
type: Number,
default: 0
},
display: { // 每頁顯示條數
type: Number,
default: 10
},
current: { // 當前頁碼
type: Number,
default: 1
},
pagegroup: { // 分頁條數 -- 奇數
type: Number,
default: 5,
coerce: function (v) {
v = v > 0 ? v : 5;
return v % 2 === 1 ? v : v + 1;
}
}
},
computed: {
page: function () { // 總頁數
return Math.ceil(this.total / this.display);
},
grouplist: function () { // 獲取分頁頁碼
var len = this.page, temp = [], list = [], count = Math.floor(this.pagegroup / 2), center = this.current;
if (len <= this.pagegroup) {
while (len--) { temp.push({ text: this.page - len, val: this.page - len }); };
return temp;
}
while (len--) { temp.push(this.page - len); };
var idx = temp.indexOf(center);
(idx < count) && (center = center + count - idx);
(this.current > this.page - count) && (center = this.page - count);
temp = temp.splice(center - count - 1, this.pagegroup);
do {
var t = temp.shift();
list.push({
text: t,
val: t
});
} while (temp.length);
if (this.page > this.pagegroup) {
(this.current > count + 1) && list.unshift({ text: '...', val: list[0].val - 1 });
(this.current < this.page - count) && list.push({ text: '...', val: list[list.length - 1].val + 1 });
}
return list;
}
},
methods: {
setCurrent: function (idx) {
if (this.current != idx && idx > 0 && idx < this.page + 1) {
this.current = idx;
this.$emit('pagechange', this.current);
}
}
}
});
2 前端:
@{
ViewBag.Title = "About";
}
<script src="~/Scripts/vue.min.js"></script>
<script src="~/Scripts/pager/Pager.js"></script>
<!-- 模板 -->
<script type="text/template" id="template_pagination">
<nav>
<ul class="pagination">
<li :class="{'disabled': current == 1}"><a href="javascript:;" v-on:click="setCurrent(1)"> 首頁 </a></li>
<li :class="{'disabled': current == 1}"><a href="javascript:;" v-on:click="setCurrent(current - 1)"> 上一頁 </a></li>
<li v-for="p in grouplist" :class="{'active': current == p.val}"><a href="javascript:;" v-on:click="setCurrent(p.val)"> {{ p.text }} </a></li>
<li :class="{'disabled': current == page}"><a href="javascript:;" v-on:click="setCurrent(current + 1)"> 下一頁</a></li>
<li :class="{'disabled': current == page}"><a href="javascript:;" v-on:click="setCurrent(page)"> 尾頁 </a></li>
</ul>
<ul class="pagination pull-right">
<li><span> 共 {{ total }} 條數據 </span></li>
<li><span> 每頁顯示 {{ display }} 條數據 </span></li>
<li><span> 共 {{ page }} 頁 </span></li>
<li><span> 當前第 {{ current }} 頁 </span></li>
</ul>
</nav>
</script>
<div>
<div id="app">
<div class="container">
<h1> Vue 分頁組件 </h1>
<pagination :total="total" :current.sync="current" v-on:pagechange="pagechange"></pagination>
<pre>{{ $data|json }}</pre>
<pre>{{ current }}</pre>
</div>
</div>
<div id="app01">
<div class="container">
<h1> Vue 分頁組件 </h1>
<pagination :total="total" :current.sync="current" v-on:pagechange="pagechange"></pagination>
<pre>{{ $data|json }}</pre>
<pre>{{ current }}</pre>
<code>sasasasas</code>
</div>
</div>
</div>
<script>
new Vue({
el: '#app',
data: {
total: 81, // 記錄總條數
display: 10, // 每頁顯示條數
current: 1 // 當前第n頁 , 也可以 watch current 的變化
},
methods: {
pagechange: function (p) {
this.current = p;// 頁碼改變event , p 為新的 current
console.log('pagechange', p);
}
}
});
new Vue({
el: '#app01',
data: {
total: 81, // 記錄總條數
display: 10, // 每頁顯示條數
current: 1 // 當前第n頁 , 也可以 watch current 的變化
},
methods: {
pagechange: function (p) {
this.current = p;// 頁碼改變event , p 為新的 current
console.log('pagechange', p);
}
}
});
</script>
展示:

