vue实现分页源码

小女子不才,自定义的分页,若有不当之处还望大佬指正,有更好的方法还望留言
一、html

<template>
	<div class="content">
		<div class="data-content">
			内容展示区域
		</div>
		//以下为分页样式及事件调用
		<div class="page-warpper">
			<div class="page page-china page-pre" @click="toPrePage">上一页</div>
			<div class="page" v-if="(currentPage > pageCount)||(minPage>1)">···</div>
			<template v-for="(item,index) in indexs">
				<div class="page" 
				:class="[item.isShow?'active':'']"
				@mouseover="mouseOver(index)"
				@mouseleave="mouseOut(index)"
				@click="toPage(index)">{{item.number}}</div>
			</template>			
			<div class="page" v-if="(totalPages > pageCount)&&(currentPage<totalPages)">···</div>
			<div class="page page-china page-next" @click="toNextPage">下一页</div>
		</div>
	</div>
</template>

二、css 根据个人喜好,自定义样式

<style scoped>
	.page-warpper{
		display: flex;
		flex-flow: row;
		justify-content: center;
		align-items: center;
		width: 100%;
	}
	.page{
		width: 34px;
		height: 34px;
		display: flex;
		flex-flow: column;
		justify-content: center;
		align-items: center;
		font-size: 16px;
		color: #333333;
		margin: 0 5px;
		border: 1px solid #999999;
		border-radius: 6px;
		cursor: pointer;
	}
	.page.page-china{
		width: 80px;
	}
	.page.active{
		background-color: #D6E9C6;
		color: #F0AD4E;
	}
	.page.page-china:hover{
		background-color: #D6E9C6;
		color: #F0AD4E;
	}
</style>

三、重要的js

<script>
	export default{
		data(){
			return{
				pageCount:4, //一组显示多少个分页数字
				currentPage:1, //当前点击的页码
				toatalNum:8, //数据返回的总条数,我这里好像没有用到,纯做记录了
				pageSize:1, //一页显示多少条数据,后台接口调用时需要
				totalPages:8, //返回的总页数
				indexs:'', //循环得到当前需要显示的页码的数组对象
				minPage:'', //当前显示页码的最小页码
				data:'' //接口返回数据
			}
		},
		mounted(){
			var that = this
			that.setPageNumber() //设置默认显示页码数从1开始
		},
		methods:{
			setPageNumber(){
				var that = this
				that.indexs = [];
				var long = that.currentPage + that.pageCount;
				for(var i = that.currentPage;i<long;i++){
					if(i == that.currentPage){
						that.indexs.push({
							isShow : true,
							number : i
						})
					}else{
						that.indexs.push({
							isShow:false,
							number:i
						})
					}					
				}
			},
			mouseOver(index){ //鼠标移入事件
				var that = this
				that.indexs[index].isShow = true
			},
			mouseOut(index){//鼠标移出事件
				var that = this
				if(that.indexs[index].number != that.currentPage){
					that.indexs[index].isShow = false
				}				
			},
			toPage(index){//点击页面时触发的事件
				var that = this
				that.indexs[index].isShow = true;
				that.indexs.forEach((e,i)=>{
					if(e.number == that.currentPage){
						e.isShow = false
					}
				})
				that.currentPage = that.indexs[index].number
				that.minPage = that.indexs[0].number
			},
			toNextPage(){//点击下一页时触发的事件
				var that = this
				if(that.currentPage < that.totalPages){
					that.indexs.forEach((e,i)=>{
						if(e.number == that.currentPage){
							e.isShow = false
						}
					})
					that.currentPage++
				}else{
					that.currentPage = that.totalPages
					that.minPage = that.indexs[0].number
					return
				}
				if(that.currentPage > that.pageCount){
					that.indexs = [];
					var i = that.currentPage - that.pageCount+1
					for(i;i<=that.currentPage;i++){
						if(i == that.currentPage){
							that.indexs.push({
								isShow : true,
								number : i
							})
						}else{
							that.indexs.push({
								isShow : false,
								number : i
							})
						}
					}
				}
				that.indexs.forEach((e,i)=>{
					if(e.number == that.currentPage){
						e.isShow = true
					}
				})
				that.minPage = that.indexs[0].number				
			},
			toPrePage(){//点击上一页时触发的事件
				var that = this
				if(that.currentPage > 1){					
					that.indexs.forEach((e,i)=>{
						if(e.number == that.currentPage){
							e.isShow = false
						}
					})
					that.currentPage--
					if(that.currentPage >= that.indexs[0].number){
						that.indexs.forEach((e,i)=>{
							if(e.number == that.currentPage){
								e.isShow = true
							}
						})
						that.minPage = that.indexs[0].number
						return
					}
					var i = that.currentPage					
					var long = that.currentPage + that.pageCount;		
					that.indexs = [];
					for(i;i<long;i++){
						if(i==that.currentPage){
							that.indexs.push({
								isShow : true,
								number : i
							})
						}else{
							that.indexs.push({
								isShow : false,
								number : i
							})
						}
					}
					that.minPage = that.indexs[0].number
				}else{
					that.currentPage = 1
					that.minPage = that.indexs[0].number
					return
				}
			}
		}
	}
</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 我可以帮你生成一份基于vue实现分页列表的组件源码,具体实现如下: 1. 创建一个Vue组件,并定义数据,方法,计算属性,等。 2. 定义一个页数变量的计算属性,用于计算页数。 3. 在组件中定义一个分页函数,用于实现分页列表的功能。 4. 在模板中添加分页控件,用于控制分页的功能。 5. 最后,在Vue组件中调用分页函数,并渲染分页列表。 ### 回答2: 当然可以为您生成一份Vue分页列表的组件源码。以下是一个简单的示例: ```vue <template> <div> <ul> <li v-for="item in paginatedList" :key="item.id">{{ item.name }}</li> </ul> <div class="pagination"> <button @click="previousPage">上一页</button> <button @click="nextPage">下一页</button> </div> </div> </template> <script> export default { data() { return { list: [], // 列表数据 currentPage: 1, // 当前页码 pageSize: 10, // 每页显示的条数 }; }, computed: { paginatedList() { const startIndex = (this.currentPage - 1) * this.pageSize; const endIndex = startIndex + this.pageSize; return this.list.slice(startIndex, endIndex); }, }, methods: { previousPage() { if (this.currentPage > 1) { this.currentPage--; } }, nextPage() { const totalPages = Math.ceil(this.list.length / this.pageSize); if (this.currentPage < totalPages) { this.currentPage++; } }, }, created() { // 在这里获取列表数据,并将数据赋值给list // 例如:this.list = api.getListData(); }, }; </script> <style scoped> .pagination { margin-top: 20px; text-align: center; } </style> ``` 这是一个简单的分页列表组件,它包含一个`list`数组用于存储列表数据,`currentPage`表示当前页码,`pageSize`表示每页显示的条数。`paginatedList`是一个计算属性,根据当前页码和每页显示的条数来计算出当前页的数据。`previousPage`方法和`nextPage`方法分别用于切换上一页和下一页的数据。在`created`生命周期钩子中可以获取列表数据,你可以根据实际需求自行修改和补充代码。 ### 回答3: 当然可以!以下是一个简单的Vue分页列表的组件源码: ```html <template> <div> <ul> <li v-for="item in pagedItems" :key="item.id">{{ item.name }}</li> </ul> <div> <button @click="previousPage" :disabled="currentPage === 1">上一页</button> <span>{{ currentPage }}</span> <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button> </div> </div> </template> <script> export default { data() { return { items: [ { id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }, { id: 3, name: 'Item 3' }, // ... 这里可以继续添加更多的数据项 ], pageSize: 5, // 每页显示的项数 currentPage: 1 // 当前页码 }; }, computed: { totalPages() { return Math.ceil(this.items.length / this.pageSize); }, pagedItems() { const startIndex = (this.currentPage - 1) * this.pageSize; const endIndex = startIndex + this.pageSize; return this.items.slice(startIndex, endIndex); } }, methods: { previousPage() { if (this.currentPage > 1) { this.currentPage--; } }, nextPage() { if (this.currentPage < this.totalPages) { this.currentPage++; } } } }; </script> ``` 在该组件中,我们定义了一个`items`数组,其中包含所有的数据项。我们还定义了`pageSize`来指定每页显示的项数,以及`currentPage`来跟踪当前的页码。 在计算属性中,我们使用`totalPages`计算总页数,即将`items`数组的长度除以`pageSize`并向上取整。同时,在`pagedItems`计算属性中,我们根据当前页码和每页显示的项数来切割`items`数组,以得到当前页码对应的数据。 在方法中,我们定义了`previousPage`和`nextPage`方法来切换上一页和下一页的功能,并且保证当前页码不超出总页数的范围。这样,我们就实现了一个基本的Vue分页列表的组件。 希望这段代码对你有帮助!如有任何问题,请随时向我提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值