Vue Cli與BootStrap結(jié)合實(shí)現(xiàn)表格分頁功能
1、首先需要在vue-cli項(xiàng)目中配置bootstrap,jquery
2、 然后新建vue文件,如index.vue,index.vue內(nèi)容如下:
3、配置路由即可運(yùn)行實(shí)現(xiàn)。
<template> <div class="tTable container body-content"> <div class="form-group"> <div class="form-group"> <div class="page-header"> 表格 </div> <table class="table table-bordered table-responsive table-striped"> <thead> <tr> <th>時(shí)間</th> <th>點(diǎn)擊數(shù)</th> <th>點(diǎn)擊數(shù)</th> </tr> </thead> <tbody> <tr v-for="item in arrayData"> <td>{{item.timestamp}}</td> <td>{{item.count}}</td> <td>{{item.count}}</td> </tr> </tbody> </table> <div class="pager" id="pager"> <span class="form-inline"> <select class="form-control" v-model="pagesize" v-on:change="showPage(pageCurrent,$event,true)" number> <option value="10">10</option> <option value="20">20</option> <option value="30">30</option> <option value="40">40</option> </select> </span> <span v-for="item in pageCount+1"> <span v-if="item==1" class="btn btn-default" v-on:click="showPage(1,$event)" :class="{'disabled':fDisabled}"> 首頁 </span> <span v-if="item==1" class="btn btn-default" v-on:click="showPage(pageCurrent-1,$event)" :class="{'disabled':fDisabled}"> 上一頁 </span> <span v-if="item==1" class="btn btn-default" v-on:click="showPage(item,$event)"> {{item}} </span> <span v-if="item==1&&item<showPagesStart-1" class="btn btn-default disabled"> ... </span> <span v-if="item>1&&item<=pageCount-1&&item>=showPagesStart&&item<=showPageEnd&&item<=pageCount" class="btn btn-default" v-on:click="showPage(item,$event)"> {{item}} </span> <span v-if="item==pageCount&&item>showPageEnd+1" class="btn btn-default disabled"> ... </span> <span v-if="item==pageCount" class="btn btn-default" v-on:click="showPage(item,$event)" > {{item}} </span> <span v-if="item==pageCount" class="btn btn-default" v-on:click="showPage(pageCurrent+1,$event)" :class="{'disabled':lDisabled}"> 下一頁 </span> <span v-if="item==pageCount" class="btn btn-default" v-on:click="showPage(pageCount,$event)" :class="{'disabled':lDisabled}"> 尾頁 </span> </span> <span>{{pageCurrent}}/{{pageCount}}</span> </div> </div> </div> </div> </template> <script > export default { data(){ return{ //為第一頁或者最后一頁時(shí),首頁,尾頁不能點(diǎn)擊 fDisabled:false, lDisabled:false, //總項(xiàng)目數(shù) totalCount: 200, //分頁數(shù) pageCount: 20, //當(dāng)前頁面 pageCurrent: 1, //分頁大小 pagesize: 10, //顯示分頁按鈕數(shù) showPages: 11, //開始顯示的分頁按鈕 showPagesStart: 1, //結(jié)束顯示的分頁按鈕 showPageEnd: 100, //分頁數(shù)據(jù) arrayData: [] } }, methods:{ showPage(pageIndex, $event, forceRefresh){ if (pageIndex > 0) { if (pageIndex > this.pageCount) { pageIndex = this.pageCount; } //判斷數(shù)據(jù)是否需要更新 var currentPageCount = Math.ceil(this.totalCount / this.pagesize); if (currentPageCount != this.pageCount) { pageIndex = 1; this.pageCount = currentPageCount; } else if (this.pageCurrent == pageIndex && currentPageCount == this.pageCount && typeof (forceRefresh) == "undefined") { console.log("not refresh"); return; } //處理分頁點(diǎn)中樣式 var buttons = $("#pager").find("span"); for (var i = 0; i < buttons.length; i++) { if (buttons.eq(i).html() != pageIndex) { buttons.eq(i).removeClass("active"); } else { buttons.eq(i).addClass("active"); } } //測(cè)試數(shù)據(jù) 隨機(jī)生成的 var newPageInfo = []; var time=new Date(); for (var i = 0; i < this.pagesize; i++) { newPageInfo[newPageInfo.length] = { timestamp: time, count: (i + (pageIndex - 1) * 20) }; } this.pageCurrent = pageIndex; this.arrayData = newPageInfo; //如果當(dāng)前頁首頁或者尾頁,則上一頁首頁就不能點(diǎn)擊,下一頁尾頁就不能點(diǎn)擊 if(this.pageCurrent===1){ this.fDisabled=true; }else if(this.pageCurrent===this.pageCount){ this.lDisabled=true; }else{ this.fDisabled=false; this.lDisabled=false; } //計(jì)算分頁按鈕數(shù)據(jù) if (this.pageCount > this.showPages) { if (pageIndex <= (this.showPages - 1) / 2) { this.showPagesStart = 1; this.showPageEnd = this.showPages - 1; console.log("showPage1") } else if (pageIndex >= this.pageCount - (this.showPages - 3) / 2) { this.showPagesStart = this.pageCount - this.showPages + 2; this.showPageEnd = this.pageCount; console.log("showPage2") } else { console.log("showPage3") this.showPagesStart = pageIndex - (this.showPages - 3) / 2; this.showPageEnd = pageIndex + (this.showPages - 3) / 2; } } console.log("showPagesStart:" + this.showPagesStart + ",showPageEnd:" + this.showPageEnd + ",pageIndex:" + pageIndex); } } }, mounted(){ this.showPage(this.pageCurrent, null, true); }, computed:{ } } </script>
總結(jié)
以上所述是小編給大家介紹的Vue Cli與BootStrap結(jié)合實(shí)現(xiàn)表格分頁功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
vue 自動(dòng)檢測(cè)手機(jī)端響應(yīng)式布局的五種實(shí)現(xiàn)
本文主要介紹了vue自動(dòng)檢測(cè)手機(jī)端響應(yīng)式布局,可以通過結(jié)合 CSS 媒體查詢、Vue 的動(dòng)態(tài)數(shù)據(jù)綁定、適當(dāng)?shù)牡谌綆?、PostCSS 插件以及正確的視口設(shè)置實(shí)現(xiàn),感興趣的可以了解一下2024-07-07Vue?中如何使用?el-date-picker?限制只能選擇當(dāng)天、當(dāng)天之前或當(dāng)天之后日期的方法詳解
在Vue前端開發(fā)中,使用 el-date-picker 組件進(jìn)行日期選擇是常見的需求,有時(shí)候我們需要限制用戶只能選擇當(dāng)天、當(dāng)天之前或當(dāng)天之后的日期,本文將詳細(xì)介紹如何使用 el-date-picker 組件實(shí)現(xiàn)這些限制,讓你能夠輕松應(yīng)對(duì)各種日期選擇場(chǎng)景,需要的朋友可以參考下2023-09-09vue2實(shí)現(xiàn)provide inject傳遞響應(yīng)式
在看element-ui的源碼的時(shí)候,注意到源碼里面有很多地方使用provide和inject的屬性,本文主要介紹了vue2實(shí)現(xiàn)provide inject傳遞響應(yīng)式,分享給大家,感興趣的可以了解一下2021-05-05vue通過style或者class改變樣式的實(shí)例代碼
這篇文章主要介紹了vue通過style或者class改變樣式的實(shí)例代碼,在文中給大家提到了vue的一些樣式(class/style)綁定,需要的朋友可以參考下2018-10-10uniapp Vue3中如何解決web/H5網(wǎng)頁瀏覽器跨域的問題
存在跨域問題的原因是因?yàn)闉g覽器的同源策略,也就是說前端無法直接發(fā)起跨域請(qǐng)求,同源策略是一個(gè)基礎(chǔ)的安全策略,但是這也會(huì)給uniapp/Vue開發(fā)者在部署時(shí)帶來一定的麻煩,這篇文章主要介紹了在uniapp Vue3版本中如何解決web/H5網(wǎng)頁瀏覽器跨域的問題,需要的朋友可以參考下2024-06-06Vue3+Ts實(shí)現(xiàn)父子組件間傳值的兩種方式
這篇文章主要給大家介紹了基于Vue3+Ts實(shí)現(xiàn)父子組件間傳值的兩種方式,使用v-model+emit傳值和使用v-bind+emit傳值兩種方式,文章通過代碼示例介紹的非常詳細(xì),感興趣的同學(xué)可以參考閱讀2023-09-09