欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue分页组件table-pagebar使用实例解析

程序员文章站 2023-10-26 17:46:34
之前一直接触都是原始的前端模型,jquery+bootstrap,冗杂的dom操作,繁琐的更新绑定。接触vue后,对前端mvvm框架有了全新的认识。本文是基于webpack...

之前一直接触都是原始的前端模型,jquery+bootstrap,冗杂的dom操作,繁琐的更新绑定。接触vue后,对前端mvvm框架有了全新的认识。本文是基于webpack+vue构建,由于之前的工作主要是基于java的服务端开发工作,对前端框架和组件的理解,不够深入,借此来记录在前端框架使用和构建中的点点滴滴。

此分页组件参照于bootstrap-datatable底部分页开发完成,相关参数增加自定义功能。

最终使用展现效果图如下,数据来源于cnodejs【】 

vue分页组件table-pagebar使用实例解析

底部分页组件主要由左侧当前数据项数量显示和右侧分页页码两部分组成。组件代码如下: 

<template xmlns:v-on="http://www.w3.org/1999/xhtml"
 xmlns:v-bind="http://www.w3.org/1999/xhtml">
 <div class="page-bar">
 <div class="page-size">
 <div>
 <select v-on:change="menuchange()" v-model="limit">
 <option v-for="item in menu" v-bind:value="item">{{item}}</option>
 </select>
 记录/页,显示第 {{start}} 至 {{end}} 项记录,共 {{totalsize}} 项
 </div>
 </div>
 <div class="page-con">
 <ul>
 <li><a v-on:click="firstclick()" v-bind:class="{ 'disabled': cur == 1}">首页</a></li>
 <li><a v-on:click="preclick()" v-bind:class="{ 'disabled': cur == 1}">上一页</a></li>
 <li v-for="per in pages" v-bind:class="{ 'active': cur == per}">
 <a v-on:click="pageclick(per)">{{ per }}</a>
 </li>
 <li><a v-on:click="nextclick()" v-bind:class="{ 'disabled': cur == totalpage}">下一页</a></li>
 <li><a v-on:click="lastclick()" v-bind:class="{ 'disabled': cur == totalpage}">尾页</a></li>
 <li><a>共<i>{{totalpage}}</i>页</a></li>
 </ul>
 </div>
 <div class="clear-both"></div>
 </div>
</template>

<script>
 import ajax from '../ajax'

 export default{
 props: ['page-model'],
 data () {
 return {
 // 初始页
 cur: 1,
 // 请求地址
 url: this.pagemodel.url ? this.pagemodel.url : "",
 // 请求参数
 param: this.pagemodel.param ? this.pagemodel.param : {},
 // 请求方法 默认为get请求
 method: this.pagemodel.method ? this.pagemodel.method : 'get',
 // 每页显示数量 默认每页显示10条
 limit: this.pagemodel.menu ? this.pagemodel.menu[0] : 10,
 // 底部分页基数 默认5
 persize: this.pagemodel.persize ? this.pagemodel.persize : 5,
 // 每页显示数量 下拉选项
 menu: this.pagemodel.menu ? this.pagemodel.menu : [5, 10, 50],
 // 分页参数 自定义名称
 pageparamname: this.pagemodel.pageparamname ? this.pagemodel.pageparamname : ['page', 'limit'],
 // 当前列表显示记录起始数
 start: 0,
 // 当前列表显示记录结束数
 end: 0,
 // 总页数
 totalpage: 0,
 // 记录总数
 totalsize: 0,
 // 分页请求返回数据
 datalist: []
 }
 },
 ready () {
 this.getdata();
 },
 methods: {
 // 首页
 firstclick: function () {
 if (this.cur > 1) {
 this.cur = 1;
 this.getdata();
 }
 },
 // 尾页
 lastclick: function () {
 if (this.cur < this.totalpage) {
 this.cur = this.totalpage;
 this.getdata();
 }
 },
 // 上一页
 preclick: function () {
 if (this.cur > 1) {
 this.cur--;
 this.getdata();
 }
 },
 // 下一页
 nextclick: function () {
 if (this.cur < this.totalpage) {
 this.cur++;
 this.getdata();
 }
 },
 // 页码
 pageclick: function (data) {
 if (data != this.cur) {
 this.cur = data;
 this.getdata();

 }
 },
 // 刷新显示记录数
 refreshpagecon: function () {
 this.start = (this.cur - 1) * this.limit + 1;
 if (this.totalsize >= this.limit * this.cur) {
 this.end = this.limit * this.cur;
 } else {
 this.end = this.totalsize;
 }
 },
 // 分页请求
 getdata: function () {
 let _this = this;
 this.param[this.pageparamname[0]] = this.cur;
 this.param[this.pageparamname[1]] = this.limit;
 ajax({
 url: _this.url,
 method: _this.method,
 data: _this.param,
 callback: function (res) {
 // 返回结果数据集
 this.datalist = res.data;
 // 返回总记录数
 _this.totalsize = 25;

 _this.totalpage = math.ceil(_this.totalsize / _this.limit);
 _this.refreshpagecon();
 _this.$dispatch('refresh', this.datalist);
 }
 });
 },
 // 每页显示记录数 下拉
 menuchange: function () {
 this.getdata();
 },
 getpage: function (curpage, totalpage, pagenum) {
 var leftpage, rightpage;
 curpage = curpage > 1 ? curpage : 1;
 curpage = curpage > totalpage ? totalpage : curpage;
 totalpage = curpage > totalpage ? curpage : totalpage;
 // 左侧页数
 leftpage = curpage - math.floor(pagenum / 2);
 leftpage = leftpage > 1 ? leftpage : 1;

 // 右侧页数
 rightpage = curpage + math.floor(pagenum / 2);
 rightpage = rightpage > totalpage ? totalpage : rightpage;

 var curpagenum = rightpage - leftpage + 1;
 // 左侧调整
 if (curpagenum < pagenum && leftpage > 1) {
 leftpage = leftpage - (pagenum - curpagenum);
 leftpage = leftpage > 1 ? leftpage : 1;
 curpagenum = rightpage - leftpage + 1;
 }

 // 右侧调整
 if (curpagenum < pagenum && rightpage < totalpage) {
 rightpage = rightpage + (pagenum - curpagenum);
 rightpage = rightpage > totalpage ? totalpage : rightpage;
 }

 var arr = [];
 for (var i = leftpage; i <= rightpage; i++) {
 arr.push(i);
 }
 return arr;
 }
 },
 computed: {
 pages: function () {
 return this.getpage(this.cur, this.totalpage, this.persize);
 }
 }
 }
</script>

<style>
 ul, li {
 margin: 0px;
 padding: 0px;
 }

 li {
 list-style: none;
 display: inline;
 }

 .page-bar li:first-child > a {
 margin-left: 0px
 }

 .page-bar a {
 border: 1px solid #ddd;
 text-decoration: none;
 position: relative;
 float: left;
 padding: 6px 12px;
 margin-left: -1px;
 line-height: 1.42857143;
 color: #337ab7;
 cursor: pointer;
 }

 .page-bar a:hover {
 background-color: #eee;
 }

 .page-bar .active a {
 color: #fff;
 cursor: default;
 background-color: #337ab7;
 border-color: #337ab7;
 }

 .page-bar i {
 font-style: normal;
 color: #d44950;
 margin: 0px 4px;
 font-size: 12px;
 }

 .page-bar .page-con, .page-size {
 width: 50%;
 display: block;
 height: 30px;
 float: left;
 line-height: 30px;
 }

 .page-bar .page-con ul {
 float: right;
 padding-left: 15px;
 padding-right: 15px;
 display: inline-block;
 padding-left: 0;
 }

 .page-bar .page-size div {
 padding-left: 15px;
 padding-right: 15px;
 font-size: 14px;
 }

 a.disabled {
 color: #777;
 background-color: #fff;
 border-color: #ddd;
 cursor: not-allowed;
 }

 a.disabled:hover {
 background-color: #fff;
 }

 .clear-both {
 clear: both;
 }

 select {
 border: solid 1px #ddd;
 appearance: none;
 -moz-appearance: none;
 -webkit-appearance: none;
 background: url("../assets/images/arrow.png") no-repeat scroll right center transparent;
 padding-right: 15px;
 padding-left: 15px;
 padding-top: 2px;
 padding-bottom: 2px;
 }

 select::-ms-expand {
 display: none;
 }
</style>

组建模块使用, 

<template>
 <navbar></navbar>
 <div class="row">
 <table class="table">
 <thead>
 <tr>
 <th>标题</th>
 <th width="20%">发布时间</th>
 <th width="10%">作者</th>
 <th width="10%">回复数</th>
 <th width="10%">访问数</th>
 </tr>
 </thead>
 <tbody>
 <tr v-show="!tabelempty" v-for="item in datalist">
 <td>{{item.title}}</td>
 <td>{{item.create_at}}</td>
 <td>{{item.author.loginname}}</td>
 <td>{{item.reply_count}}</td>
 <td>{{item.visit_count}}</td>
 </tr>
 <tr v-show="tabelempty" class="empty">
 <td colspan="5">没有匹配的记录</td>
 </tr>
 </tbody>
 </table>
 </div>
 <pagebar :page-model="pagemodel"></pagebar>
</template>
<script>
 import navbar from '../components/navbar.vue'
 import pagebar from '../components/table-pagebar.vue'

 export default {//这里是官方的写法,默认导出,es6
 components: {
 navbar,
 pagebar
 },
 data () {
 return {
 allarticle: "",
 datalist: [],
 pagemodel: {
 url: 'https://cnodejs.org/api/v1/topics',
 menu: [5, 10, 20]
 },
 }
 },
 computed: {
 tabelempty: function () {
 if (this.datalist) {
 return false;
 } else {
 return true;
 }
 }
 },
 events: {
 refresh: function (e) {
 this.datalist = e;
 }
 }
 }
</script>
<style>
 body, table {
 font-size: 12px;
 }

 table {
 table-layout: fixed;
 empty-cells: show;
 border-collapse: collapse;
 width: 100%;
 margin: 10px 0;
 }

 td {
 height: 30px;
 }

 div.row {
 margin-left: 15px;
 margin-right: 15px;
 }

 h1, h2, h3 {
 font-size: 12px;
 margin: 0;
 padding: 0;
 }

 .table {
 border: 1px solid #ddd;
 background: #fff;
 }

 .table thead tr {
 background: #eee;
 }

 .table th {
 background-repeat: repeat-x;
 height: 30px;
 text-align: left;
 vertical-align: middle;
 }

 .table tr.empty {
 text-align: center;
 }

 .table td, .table th {
 border: 1px solid #ddd;
 padding: 0 1em 0;
 }

 .table tr:nth-child(odd) td {
 background-color: #f5f5f5;

 }
</style>

本文已被整理到了《vue.js前端组件学习教程》,欢迎大家学习阅读。

关于vue.js组件的教程,请大家点击专题vue.js组件学习教程进行学习。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。