基于Vue的简单通用分页组件
分页组件是每一个系统里必不可少的一个组件,分页组件分为两部分。第一部分是模版部分,用于显示当前分页组件的状态,例如正在获取数据、没有数据、没有下一页等等;第二部分是分页数据对象,用于封装一个分页组件的属性和方法,例如获取数据的 url、当前第几页(page)、每次加载条数(count)、一共有多少页(totalPage)等等,方法可能会有上一页、下一页、处理数据等等。
分页数据对象
import base from '@/api/base'
export default class Pagination {
constructor({ url, processFunc, processExt, count = 10, isMock = false }) {
// 数据访问地址
this.url = url
// 数据集合
this.list = []
// 第几页
this.page = 1
// 一共几页
this.totalPage = 1
// 加载数据条数
this.count = count
// 数据处理函数
this.processFunc = processFunc
// 错误处理函数
this.processExt = processExt
// 正在加载中
this.loading = false
// 参数
this.params = {}
// 是否底部
this.reachBottom = false
// 是否为空
this.empty = true
// 是否需要清除
this.toClear = false
// 是否为mock数据
this.isMock = isMock
}
/**
* 加载下一页数据
*/
async next(args) {
if (this.loading) {
// console.warn('page loading!')
return this
}
const param = {
pageNo: this.page,
pageSize: this.count
}
// 附加参数
this.loading = true
try {
Object.assign(param, args)
let res
let data
try {
res = await base.get(this.url, param)
data = res.data
} catch (e) {
if (typeof this.processExt === 'function') {
data = this.processExt(e)
} else {
throw new Error(e)
}
}
// 底部判断
if (data === null || data.length < 1) {
if (this.toClear) {
this.clear()
} else {
this.reachBottom = true
}
return this
}
this.empty = false
// 处理数据
this._processData(data)
// 设置数据
if (this.toClear) {
this.list = data
this.toClear = false
} else {
this.list = this.list.concat(data)
}
++this.page
this.totalPage = res.page.totalPages
if (
(res.page && res.page.page === res.page.totalPages) ||
data.length < this.count
) {
this.reachBottom = true
}
return this
} finally {
this.loading = false
}
}
/**
* 恢复到第一页
*/
reset() {
this.empty = true
this.toClear = true
this.page = 1
this.reachBottom = false
}
clear() {
this.toClear = false
this.page = 1
this.list = []
}
/**
* 处理数据(私有)
*/
_processData(data) {
if (this.processFunc) {
for (let i in data) {
const result = this.processFunc(data[i])
if (result) {
data[i] = result
}
}
}
}
}
分页模版
<template>
<div class="z-page-stat">
<p v-show="page.loading" class="page-loading">
<span class="ign-loading"></span>
</p>
<div
class="u-more-btn"
v-show="showLoadMore && !page.reachBottom && !page.loading && !page.empty"
@click="$emit('nextPage')"
>
<span>查看更多</span>
</div>
<p class="reach-btm" v-show="showBtmTx && !page.empty && page.reachBottom">
到底了~
</p>
<div class="page-empty" v-show="!page.loading && page.empty">
<div class="empty-inner">
<div class="img-bg" v-if="emptyImg">
<img
v-if="!emptyImg || emptyImg == 1"
src="../../img/empty-page.png"
alt=""
/>
</div>
<p class="tx">{{emptyText}}</p>
<div class="empty-ctn">
<slot name="empty"></slot>
</div>
</div>
</div>
<slot name="other"></slot>
</div>
</template>
<script>
export default {
name: 'pageStatus',
data() {
return {}
},
props: {
page: {},
emptyImg: {},
emptyText: {
type: String,
default: '暂时没有数据'
},
showLoadMore: {
// 是否显示加载更多按钮
type: Boolean,
default: false
},
showBtmTx: {
// 到底了文字要不要显示
type: Boolean,
default: true
}
},
components: {},
created: function() {},
mounted: function() {},
methods: {}
}
</script>
<style lang="stylus" rel="stylesheet/stylus">
.z-page-stat
text-align center
letter-spacing 2px
color #757575
line-height 60px
.page-loading
.ign-loading
border-radius 100%
margin 16px 0
animation-fill-mode both
border 2px solid #e8473f /* no */
border-bottom-color transparent
height 25px /* no */
width 25px /* no */
background transparent !important
display inline-block
animation rotate 1s 0s linear infinite
.page-empty
position absolute
left 0
width 100%
top 50%
transform translate(0, -50%)
.empty-inner
width 320px
margin 0 auto
.img-bg
position relative
display inline-block
width 254px
height 254px
background #d6d6d6
border-radius 50%
margin-bottom 20px
img
width 94px
margin-top 28px
.tx
color #8c8c8c
.empty-ctn
.u-btn
margin-top 90px
margin-left 20px
border 2px solid #464646 /* no */
box-shadow none
width 168px
height 62px
line-height 62px
</style>
使用组件
<template>
<div>
<div class="card-content" v-for="act in page.list" :key="act.id">
<p>
{{act.title}}
</p>
</div>
<p-status :page="page"></p-status>
</div>
</template>
<script>
import { mainList } from '@/api/activity'
import PageStatus from 'comps/pageStatus.vue'
export default {
data() {
return {
page: mainList()
}
},
mixins: [appPage],
computed: {},
created: async function() {
this.page.next({
/*传参*/
})
},
components: { 'p-status': PageStatus }
}
</script>
// @/api/activity
import Pagination from '@/utils/Pagination'
/**
* 列表
*/
export function mainList() {
const url = `/activity/activity/list.do`
return new Pagination({
url: url
})
}
基于Vue的简单通用分页组件的更多相关文章
- 基于Vue.js的表格分页组件
有一段时间没更新文章了,主要是因为自己一直在忙着学习新的东西而忘记分享了,实在惭愧. 这不,大半夜发文更一篇文章,分享一个自己编写的一个Vue的小组件,名叫BootPage. 不了解Vue.js的童鞋 ...
- Vue.js的表格分页组件
转自:http://www.cnblogs.com/Leo_wl/p/5522299.html 有一段时间没更新文章了,主要是因为自己一直在忙着学习新的东西而忘记分享了,实在惭愧. 这不,大半夜发文更 ...
- 发布自己第一个npm 组件包(基于Vue的文字跑马灯组件)
一.前言 总结下最近工作上在移动端实现的一个跑马灯效果,最终效果如下: 印象中好像HTML标签的'marquee'的直接可以实现这个效果,不过 HTML标准中已经废弃了'marquee'标签 既然HT ...
- ReactJS实现的通用分页组件
大家多少都自己写过各种版本的分页工具条吧,像纯服务版的,纯jsWeb板的,Angular版的,因为这个基础得不能再基础的功能太多地方都会用到,下面我给出以个用ReactJS实现的版本,首先上图看下效果 ...
- vue-awesome-swipe 基于vue使用的轮播组件 使用(改)
npm install vue-awesome-swiper --save //基于vue使用的轮播组件 <template> <swiper :options="swi ...
- 基于Vue开发的tab切换组件
github地址:https://github.com/MengFangui/VueTabSwitch 1.index.html <!DOCTYPE html> <html lang ...
- asp.net MVC通用分页组件 使用方便 通用性强
asp.net MVC通用分页组件 使用方便 通用性强 该分页控件的显示逻辑: 1 当前页面反色突出显示,链接不可点击 2 第一页时首页链接不可点击 3 最后一页时尾页链接不可点击 4 当前页面左 ...
- vue修改elementUI的分页组件视图没更新问题
转: vue修改elementUI的分页组件视图没更新问题 今天遇到一个小问题平时没留意,el-pagination这个分页组件有一个属性是current-page当前页.今天想在methods里面手 ...
- 基于vue2.0的一个分页组件
分页组件在项目中经常要用到之前一直都是在网上找些jq的控件来用(逃..),最近几个项目用上vue了项目又刚好需要一个分页的功能.于是百度发现几篇文章介绍的实在方式有点复杂, 没耐心看自己动手造轮子写了 ...
随机推荐
- Python中网络编程对socket accept函数的理解
在服务器端,socket()返回的套接字用于监听(listen)和接受(accept),这个套接字不能用于与客户端之间发送和接收数据. accept()接受一个客户端的连接请求,并返回一个新的套接字, ...
- 自定义配置编译linux内核
1 编译linux内核原因一般情况下,我们是不需要重新去编译linux内核的,但如果你发现你需要修改内核的某个部分或者说你需要的某个模块并没有编译进内核,那里你可以通过重新编译内核来满足你的需求,比如 ...
- python第十四课--排序及自定义函数之自定义函数(案例一)
案例一: 演示自定义函数的使用:包含:1).定义格式的掌握2).函数的好处 自定义函数:实现打印矩形的操作两个原则需要考虑:1).有没有形参?有,2个 2).有没有返回值?没有. def printR ...
- const修饰的成员是类成员,还是实例成员?
很抱歉,我以为只有static修饰的成员是类成员,可以通过类名直接访问,然而,const 修饰的成员也属于类成员,直接通过类名访问,不能通过实例变量访问. 做维护久了,深刻的理解,扎实的基础对写出高质 ...
- Zookeeper ZAB 协议分析[转]
写在开始:这是我找到一篇比较好的博客,转载到这来进行备份原文参考: Zookeeper ZAB 协议分析 前言 ZAB 协议是为分布式协调服务 ZooKeeper 专门设计的一种支持崩溃恢复的原子广播 ...
- 【CF809E】Surprise me!
题目 这是一道神仙题 看到这样一个鬼畜的柿子 \[\sum_{i=1}^n\sum_{j=1}^n\varphi(a_i\times a_j)\times dis(i,j)\] 又是树上距离又是\(\ ...
- 【node.js】模块系统、函数
为了让Node.js的文件可以相互调用,Node.js提供了一个简单的模块系统. 一个 Node.js 文件就是一个模块,这个文件可能是JavaScript 代码.JSON 或者编译过的C/C++ 扩 ...
- ASP.NET MVC编程——路由
框架自动生成的路由配置 上图中,路由配置文件为App_Start文件夹下的RouteConfig.cs. 代码如下: public class RouteConfig { public static ...
- nuxt 脚手架创建nuxt项目中不支持es6语法的解决方案
node本身并不支持es6语法,我们通常在vue项目中使用es6语法,是因为,我们使用babel做过处理, 为了让项目支持es6语法,我们必须同时使用babel 去启动我们的程序,所以再启动程序中加 ...
- 【转】iOS消息推送实现过程记录
客户端代码:链接地址 服务器代码:链接地址 链接地址 这里记录下iOS消息推送实现的全过程 首先,申请秘钥. 之后进入链接地址开发者,当然你得有啊!!!!! 点击这里 如图: 下面实现创建推送证书( ...