vue实现分页功能jiexi

首先我们先定义好分页效果

<div class="common-seo-page-box clearfix align-center assist">
    <ul class="page-center">
		<li v-if="currentPage>0"><a v-on:click="prePage">上一页</a></li>
		<li v-if="currentPage==0"><a class="banclick">上一页</a></li>
		<!-- 当前页背景色为蓝色 -->
		<li v-for="(item, index) in pageNum" :key="index"  v-bind:class="{ 'active':                                         currentPage == index}">
		<a v-on:click="toPage(index)">{{ index+1 }}</a>
		</li>
		<li v-if="currentPage<pageNum"><a v-on:click="nextPage">下一页</a></li>
		<li v-if="currentPage==pageNum"><a class="banclick">下一页</a></li>
		<li><a>共<i>{{pageNum}}</i>页</a></li>
	</ul>
</div>

css:

    .common-seo-page-box{
        padding-top: 19;
    }    
    .align-center{
        text-alogn: center;
    }
    .clearfix{
        z-index: 1;
    }
    li{
	    list-style: none
	}
	.common-seo-page-box li:first-child>a {
	   margin-left: 0px
	}
	.common-seo-page-box  .page-center{
		float: left;
		position: relative;
		left: 50%;
	}
	.common-seo-page-box li{
	    border: 1px solid #ddd;
	    text-decoration: none;
	    position: relative;
	    float: left;
		right: 50%;
	    padding: 6px 12px;
	    margin-left: -1px;
	    line-height: 1.42857143;
	    
	    cursor: pointer
	}
	.common-seo-page-box li:hover{
	    /* background-color: #eee; */
	}
	.common-seo-page-box li.banclick{
	    cursor:not-allowed;
	}
	.common-seo-page-box .active{
	    color: #fff;
	    cursor: default;
	    background-color: #337ab7;
	    border-color: #337ab7;
	}
	.common-seo-page-box .active a{
	    color: #fff;
	    /* cursor: default;
	    background-color: #337ab7;
	    border-color: #337ab7; */
	}
	.common-seo-page-box i{
	    font-style:normal;
	    color: #d44950;
	    margin: 0px 4px;
	    font-size: 12px;
	}

接下来就是重点: vue.js

export default{
	name: 'ListPage',
	data(){
		return {	
		    feasts: [],
            list_id: '',
			pageSize: 2,
			currentPage: 0
		}
	},
    mounted () {
		this.$axios.get(this.HOST + 'api/list/' + this.list_id)
		.then(res => {
			this.detailData(res.data)
		})
		.catch(error => {
			console.log(error)
		})
	},
    methods: {
		getid(){
			var id = this.$route.query.id
			this.list_id = id
		},
		detailData(data){
			// 过滤器
			var labels = []
				
			for (var i=0; i<data.labelsData.length; i++) {
				var labelsObj = {
					id: data.labelsData[i].id,
					title: data.labelsData[i].title,
					master: data.labelsData[i].master_img
				}
				labels.push(labelsObj)
			}
			this.feasts = labels
		},
		nextPage: function(){
			var vm = this;
			if (vm.currentPage == vm.pageNum - 1) return;
			vm.currentPage++;	 
		},
		prePage: function(){
			var vm = this;
			if (vm.currentPage == 0) return;
			vm.currentPage--;
				
		},
		toPage: function(page){
			var vm = this;
			vm.currentPage = page
		}
	},
	computed:{
		dataShow: function(){
			let start = this.currentPage*this.pageSize;
			let end = Math.min((this.currentPage+1)*this.pageSize, this.feasts.length)
			return this.feasts.slice(start, end)
		},
		pageNum: function(){
			return Math.ceil(this.feasts.length / this.pageSize) || 1 ;
		},
		
	}
        

最后我们来一张效果图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值