Vue 将后台获取的数据进行分页

本文详细介绍了如何使用Vue.js创建一个动态分页组件,包括HTML结构设计、Vue数据绑定及事件处理,实现首页、上一页、下一页和尾页等功能。通过计算属性展示当前页数和总页数,确保用户可以轻松浏览大量数据。

html部分

<div class="page">
    <button class="btn btn-default" type="submit" @click="primaryPage">首页</button>
    <button class="btn btn-default" type="submit" @click="prePage">上页</button>
    <button class="btn btn-default" type="submit">{{current_page}}/{{Math.ceil(arrs.length/pagesize)}}</button>
    <button class="btn btn-default" type="submit" @click="nextPage">下页</button>
    <button class="btn btn-default" type="submit" @click="lastPage">尾页</button>
</div>

vue部分

data : {
      currentPage : 1,//当前页号
      pagesize :10 //每页大小
     }
     
computed:{
    page_arrs(){
      let {currentPage,pagesize} = this
      return this.arrs.slice((currentPage-1)*pagesize,currentPage*pagesize)
    },
    current_page(){
      return this.currentPage
    }
},
methods: {
    primaryPage(){
      this.currentPage = 1
    },
    prePage(){
      if(this.currentPage == 1){
        return
      }
      this.currentPage = this.currentPage - 1
    },
    nextPage(){
      if(this.currentPage == Math.ceil(this.arrs.length/this.pagesize)){
        return
      }
      this.currentPage = this.currentPage + 1
    },
    lastPage(){
      this.currentPage = Math.ceil(this.arrs.length/this.pagesize)
    }
},
要实现从后台拿图片并分页,可以采用以下步骤: 1. 在后台实现图片上传功能,并将图片信息存储到数据库中; 2. 定义前端页面,使用Vue框架进行开发; 3. 在Vue组件中,使用axios库发起请求,获取后台图片数据; 4. 在Vue组件中,使用分页插件(如vue-pagination)进行分页; 5. 在Vue组件中,通过v-for指令渲染图片列表。 下面是一个简单的代码示例: ```html <template> <div> <div v-for="image in images" :key="image.id"> <img :src="image.url" alt="image"> </div> <vue-pagination :pagination="pagination" @paginate="getImages" /> </div> </template> <script> import axios from 'axios'; import VuePagination from 'vue-pagination'; export default { name: 'ImageList', components: { VuePagination, }, data() { return { images: [], pagination: { current_page: 1, last_page: 1, per_page: 10, total: 0, }, }; }, mounted() { this.getImages(); }, methods: { getImages(page = 1) { axios.get(`/api/images?page=${page}&per_page=${this.pagination.per_page}`) .then(response => { this.images = response.data.data; this.pagination.current_page = response.data.current_page; this.pagination.last_page = response.data.last_page; this.pagination.total = response.data.total; }) .catch(error => console.log(error)); }, }, }; </script> ``` 在上面的代码中,我们定义了一个ImageList组件,通过axios库发起请求,获取后台图片数据,并使用vue-pagination插件进行分页。通过v-for指令渲染图片列表,实现了从后台拿图片并分页的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值