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


Posted in Javascript onNovember 15, 2020

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

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

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

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组件学习教程进行学习。

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

Javascript 相关文章推荐
JavaScript自定义DateDiff函数(兼容所有浏览器)
Mar 01 Javascript
输入自动提示搜索提示功能的使用说明:sugggestion.txt
Sep 02 Javascript
js中通过split函数分割字符串成数组小例子
Sep 21 Javascript
Javascript中call的两种用法实例
Dec 13 Javascript
jQuery鼠标事件总结
Oct 13 Javascript
利用Query+bootstrap和js两种方式实现日期选择器
Jan 10 Javascript
JavaScript编程设计模式之构造器模式实例分析
Oct 25 Javascript
postman+json+springmvc测试批量添加实例
Mar 31 Javascript
微信小程序开发之tabbar图标和颜色的实现
Oct 17 Javascript
教你如何用Node实现API的转发(某音乐)
Sep 20 Javascript
vuex存值与取值的实例
Nov 06 Javascript
记一次用ts+vuecli4重构项目的实现
May 21 Javascript
使用BootStrap实现用户登录界面UI
Aug 10 #Javascript
基于JSON格式数据的简单jQuery幻灯片插件(jquery-slider)
Aug 10 #Javascript
BootStrap Typeahead自动补全插件实例代码
Aug 10 #Javascript
BootStrap中Datepicker控件带中文的js文件
Aug 10 #Javascript
JS获取鼠标选中的文字
Aug 10 #Javascript
浅析JavaScript函数的调用模式
Aug 10 #Javascript
JavaScript实现刷新不重记的倒计时
Aug 10 #Javascript
You might like
对text数据类型不支持代码页转换 从: 1252 到: 936
2011/04/23 PHP
PHP页面间参数传递的四种方法详解
2013/06/09 PHP
yii框架通过控制台命令创建定时任务示例
2014/04/30 PHP
PHP中array_map与array_column之间的关系分析
2014/08/19 PHP
Redis使用Eval多个键值自增的操作实例
2016/11/04 PHP
PHP实现的贪婪算法实例
2017/10/17 PHP
TP5框架实现的数据库备份功能示例
2020/04/05 PHP
兼容FireFox 的 js 日历 支持时间的获取
2009/03/04 Javascript
添加JavaScript重载函数的辅助方法2
2010/07/04 Javascript
jQuery结合PHP+MySQL实现二级联动下拉列表[实例]
2011/11/15 Javascript
Jquery自定义button按钮的几种方法
2014/06/11 Javascript
基于jQuery.Hz2Py.js插件实现的汉字转拼音特效
2015/05/07 Javascript
js中对函数设置默认参数值的3种方法
2015/10/23 Javascript
JavaScript计划任务后台运行的方法
2015/12/18 Javascript
解析浏览器端的AJAX缓存机制
2016/06/21 Javascript
jquery一键控制checkbox全选、反选或全不选
2017/10/16 jQuery
详解使用React全家桶搭建一个后台管理系统
2017/11/04 Javascript
JavaScript实现的超简单计算器功能示例
2017/12/23 Javascript
使用json-server简单完成CRUD模拟后台数据的方法
2018/07/12 Javascript
Vue父子组件双向绑定传值的实现方法
2018/07/31 Javascript
jQuery轮播图实例详解
2018/08/15 jQuery
Vue表单控件绑定图文详解
2019/02/11 Javascript
[00:44]2016完美“圣”典 风云人物:Mikasa宣传片
2016/12/07 DOTA
windows系统中python使用rar命令压缩多个文件夹示例
2014/05/06 Python
python服务器端收发请求的实现代码
2014/09/29 Python
python3中关于excel追加写入格式被覆盖问题(实例代码)
2020/01/10 Python
python程序文件扩展名知识点详解
2020/02/27 Python
深入理解Python变量的数据类型和存储
2021/02/01 Python
HTML5+Canvas+CSS3实现齐天大圣孙悟空腾云驾雾效果
2016/04/26 HTML / CSS
女孩每月服装订阅盒:kidpik
2019/04/17 全球购物
成教毕业生自我鉴定
2013/10/23 职场文书
大学生村官典型材料
2014/01/12 职场文书
高等教育学专业自荐书
2014/06/17 职场文书
党员检讨书
2014/10/13 职场文书
入党介绍人意见范文
2015/06/01 职场文书
2015年幼儿教育工作总结
2015/07/24 职场文书