Vue實(shí)現(xiàn)簡(jiǎn)單分頁器
提前說明:這原本是一個(gè)Jquery分頁器,因?yàn)楣卷?xiàng)目前后端不分離,以前的分頁用的是基于.net的分頁器。后來我引入了jquery分頁器,在我掌握Vue之后,又自己寫了一些基于Vue的插件(為什么不用成熟的Vue插件庫,還是因?yàn)榍昂蠖藳]分離)但是前后端相對(duì)最開始的混雜已經(jīng)算是分得很開了。
分頁器的樣式是bootstrap風(fēng)格的,是一個(gè)完全自定義樣式的分頁器,這意味著你可以很輕松把它改成你想要的樣子(例子效果圖如下)。

所有的分頁器DEMO,都不會(huì)太簡(jiǎn)單,所以想要真正的掌握(支配)一款好用的分頁插件,請(qǐng)務(wù)必耐心看下面的使用示例代碼(本demo的下載地址,點(diǎn)擊可以作為項(xiàng)目直接打開使用),另外也寫了詳細(xì)的注釋并盡可能的保證簡(jiǎn)單好用。
引用bootstrap(如果你希望是bootstrap風(fēng)格)
或者你完全可以自己寫自己想要的風(fēng)格!因?yàn)榉猪撈鞯膆tml結(jié)構(gòu)是js生成的。
需要引用的腳本文件有(資源均在文章底部下載包里)。
按如下順序引入:
1.jQuery
2.Vue
3.jgPaginator.js
貼代碼,看注釋:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>分頁組件</title>
<link rel="stylesheet" href="../../css/bootstrap.min.css" >
<style>
html,
body {
width: 100%;
height: 100%;
}
.page-container {
width: 1200px;
border: 1px solid #000;
margin: 50px auto 0 auto;
overflow: hidden;
}
.page-content{
margin: 50px 0;
}
/*jqPaginator分頁控件樣式*/
.hiddenPager {
visibility: hidden;
}
.jqPager {
position: relative;
width: 100%;
height: 40px;
padding: 25px 0;
background-color: #FFF;
}
.jqPager .pagination {
float: initial;
display: inline-block;
position: absolute;
top: 0;
left: 0;
right: 0;
margin: 0 auto;
text-align: center;
}
.jqPager .pagination li {
display: inline-block;
}
.jqPager .pagination li a {
padding: 4px 10px;
}
/*跳頁--選用功能*/
.jumpBox {
position: absolute;
top: 0;
right: 105px;
}
.jumppage {
display: block;
width: 42px;
padding-left: 8px;
float: left;
height: 34px;
outline: none;
}
.jumpbtn {
float: left;
display: block;
height: 34px;
line-height: 34px;
margin-left: 5px;
}
</style>
</head>
<body>
<div id="app" class="page-container">
<!--頁面該顯示的內(nèi)容-->
<div class="page-content text-center">{{nova.text}}</div>
<!--分頁-->
<div class="jqPager" v-bind:class="{hiddenPager:hiddenPager}">
<ul id="jqPager" class="pagination cf"></ul>
<!--沒有跳頁功能可以不要下面的jumpBox-->
<div class="jumpBox">
<input type="number" class="jumppage" id="jumpPageIndex" />
<a href="javascript:;" rel="external nofollow" class="jumpbtn" v-on:click="pageSkip()">跳轉(zhuǎn)</a>
</div>
</div>
</div>
</body>
<script src="jquery-2.1.4.min.js"></script>
<script src="vue.js"></script>
<script src="jqPaginator.js"></script>
<script>
//虛擬的數(shù)據(jù)來源
var dataSource = [{
"text": "第一頁數(shù)據(jù),應(yīng)該是一個(gè)數(shù)組,包含了pageSize條數(shù)據(jù)"
}, {
"text": "第二頁數(shù)據(jù)"
}, {
"text": "第三頁數(shù)據(jù)"
},
{
"text": "第四頁數(shù)據(jù)"
},
{
"text": "第五頁數(shù)據(jù)"
},
{
"text": "第六頁數(shù)據(jù)"
}
]
//模擬后臺(tái)返回的數(shù)據(jù)
var backData = {
Data: dataSource,//返回的數(shù)據(jù)
totlaCount: 6,//搜索結(jié)果總數(shù)
Success: true//請(qǐng)求接口是否成功
};
/*每一頁顯示的數(shù)據(jù)條數(shù),按照約定傳給后臺(tái),此例為1。
需要加以說明的是這個(gè)實(shí)例你是看不出來這個(gè)參數(shù)的作用的,正如我返回?cái)?shù)據(jù)中說的那樣,后臺(tái)給你返回的數(shù)據(jù)條數(shù)
應(yīng)該是一個(gè)有 -pagesize-條數(shù)據(jù)的數(shù)組才對(duì)*/
var jqPageSize = 1;
var app = new Vue({
el: "#app",
data: {
//query是查詢條件,這里就是當(dāng)前頁碼和每一頁該顯示的數(shù)據(jù)條數(shù)
query: {
pageIndex: 1,
pageSize: jqPageSize
},
nova: [],
hiddenPager: true,//是否顯示分頁
totalCount: 0,//數(shù)據(jù)總條數(shù),后臺(tái)返回
},
methods: {
//初始化分頁,通過更改生成的html結(jié)構(gòu)(給其添加class或者改變返回的DOM)可以手動(dòng)配置頁器的顯示效果。
initPager: function() {
$('#jqPager').jqPaginator({
visiblePages: 1,
currentPage: 1,
totalPages: 1,
first: '<li id="first"><a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a></li>',
prev: '<li id="prev"><a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上一頁 </a></li>',
next: '<li id="next"><a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一頁</a></li>',
last: '<li id="last"><a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁</a></li>',
page: '<li class="page"><a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{page}}</a></li>',
});
},
//獲取數(shù)據(jù)并根據(jù)結(jié)果配置分頁
getData: function() {
this.nova = backData.Data[this.query.pageIndex - 1];
this.totalCount = backData.Data.length;
this.hiddenPager = false;
//核心配置在此部,根據(jù)后臺(tái)返回?cái)?shù)據(jù)控制分頁器該如何顯示
//想要完全掌握這個(gè)分頁器,你可以研究下jgPaginator.js源碼,很容易修改。
$('#jqPager').jqPaginator('option', {
totalCounts: app.totalCount,//后臺(tái)返回?cái)?shù)據(jù)總數(shù)
pageSize: jqPageSize, //每一頁顯示多少條內(nèi)容
currentPage: app.query.pageIndex, //現(xiàn)在的頁碼
visiblePages: 4, //最多顯示幾頁
//翻頁時(shí)觸發(fā)的事件
onPageChange: function(num) {
//app.query.pageIndex = num;
app.pageChangeEvent(num);//調(diào)用翻頁事件
}
});
},
//翻頁或者跳頁事件
pageChangeEvent: function(pageIndex) {
this.query.pageIndex = Number(pageIndex);
this.getData();
},
//跳頁-選用功能,可有可無
pageSkip: function() {
var maxPage = 1;//默認(rèn)可以跳的最大頁碼
var targetPage = document.getElementById("jumpPageIndex").value;//目的頁面
if(!targetPage) {
alert("請(qǐng)輸入頁碼");
return;
}
//計(jì)算最大可跳頁數(shù)
maxPage = Math.floor(this.totalCount / this.query.pageSize);
if(maxPage<1){
maxPage=1;
}
if(targetPage > maxPage) {
alert('超過最大頁數(shù)了,當(dāng)前最大頁數(shù)是' + maxPage);
return;
}
this.pageChangeEvent(targetPage);
},
},
//這一部分的定時(shí)器是為了此例方便加上的,初始化分頁方法(initPager)要在獲取數(shù)據(jù)之前執(zhí)行就可以了
mounted: function() {
this.initPager();
setTimeout(function() {
app.getData();
}, 50)
}
});
</script>
</html>
以上就是分頁的全部實(shí)現(xiàn)代碼,想要完全掌握,只看DEMO肯定是不夠的,所以這里是DEMO的下載地址,里面包含了所有需要引用的資源文件以及未壓縮的分頁器核心:jqPaginator.js。你需要好好看看它的源碼!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Plotly.js在Vue中創(chuàng)建交互式散點(diǎn)圖的示例代碼
Plotly.js是一個(gè)功能強(qiáng)大的JavaScript庫,用于創(chuàng)建交互式數(shù)據(jù)可視化,它支持各種圖表類型,包括散點(diǎn)圖、折線圖和直方圖,在Vue.js應(yīng)用程序中,Plotly.js可用于創(chuàng)建動(dòng)態(tài)且引人入勝的數(shù)據(jù)可視化,本文介紹了使用Plotly.js在Vue中創(chuàng)建交互式散點(diǎn)圖,需要的朋友可以參考下2024-07-07
使用vue-aplayer插件時(shí)出現(xiàn)的問題的解決
這篇文章主要介紹了使用vue-aplayer插件時(shí)出現(xiàn)的問題的解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
在Vue3.x中實(shí)現(xiàn)類似React.lazy效果的方法詳解
React 的 React.lazy 功能為組件懶加載提供了原生支持,允許開發(fā)者將組件渲染推遲到實(shí)際需要時(shí)再進(jìn)行,雖然 Vue3.x 沒有一個(gè)直接對(duì)應(yīng)的 lazy 函數(shù),但我們可以通過動(dòng)態(tài)導(dǎo)入和 defineAsyncComponent 方法來實(shí)現(xiàn)類似的效果,需要的朋友可以參考下2024-03-03
基于Vue實(shí)現(xiàn)本地消息隊(duì)列MQ的示例詳解
這篇文章為大家詳細(xì)主要介紹了如何基于Vue實(shí)現(xiàn)本地消息隊(duì)列MQ, 讓消息延遲消費(fèi)或者做緩存,文中的示例代碼講解詳細(xì),需要的可以參考一下2024-02-02
vite2打包的時(shí)候vendor-xxx.js文件過大的解決方法
vite2是一個(gè)非常好用的工具,只是隨著代碼的增多,打包的時(shí)候?vendor-xxxxxx.js?文件也越來越大,本文主要介紹了vite2打包的時(shí)候vendor-xxx.js文件過大的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

