vue 数据分页

8 篇文章 0 订阅
1 篇文章 0 订阅

数据列表分页处理

在数据分类中,每页仅能显示8种具体的分类。当超出8种是自动出现第二页,同时,所有的页具有轮播效果。如图:
在这里插入图片描述
具体代码如下:

html文件


<div class="icons">
    <swiper>
      <swiper-slide v-for="(page,index) of pages" :key="index">
        <div class="icon" v-for="item of page" :key="item.id">
          <div class="icon-img">
            <img class="icon-img-content" :src="item.imgUrl">
          </div>
          <p class="icon-desc">{{item.title}}</p>
        </div>
      </swiper-slide>
    </swiper>
  </div>

js文件

import Swiper from "../../../../node_modules/vue-awesome-swiper/src/swiper";
  export default {
    components: {Swiper},
    name: "HomeIcons",
    data() {
      return {
        iconList: [
          {
            id: "0001",
            imgUrl: "http://img1.qunarzz.com/piao/fusion/1803/95/f3dd6c383aeb3b02.png",
            title: "热门景点1"
          }, {
            id: "0002",
            imgUrl: "http://img1.qunarzz.com/piao/fusion/1803/95/f3dd6c383aeb3b02.png",
            title: "热门景点2"
          }, {
            id: "0003",
            imgUrl: "http://img1.qunarzz.com/piao/fusion/1803/95/f3dd6c383aeb3b02.png",
            title: "热门景点3"
          }, {
            id: "0004",
            imgUrl: "http://img1.qunarzz.com/piao/fusion/1803/95/f3dd6c383aeb3b02.png",
            title: "热门景点4"
          }, {
            id: "0005",
            imgUrl: "http://img1.qunarzz.com/piao/fusion/1803/95/f3dd6c383aeb3b02.png",
            title: "热门景点5"
          }, {
            id: "0006",
            imgUrl: "http://img1.qunarzz.com/piao/fusion/1803/95/f3dd6c383aeb3b02.png",
            title: "热门景点6"
          }, {
            id: "0007",
            imgUrl: "http://img1.qunarzz.com/piao/fusion/1803/95/f3dd6c383aeb3b02.png",
            title: "热门景点7"
          }, {
            id: "0008",
            imgUrl: "http://img1.qunarzz.com/piao/fusion/1803/95/f3dd6c383aeb3b02.png",
            title: "热门景点8"
          }, {
            id: "0009",
            imgUrl: "http://img1.qunarzz.com/piao/fusion/1803/95/f3dd6c383aeb3b02.png",
            title: "热门景点9"
          }],
        swiperOption: {
          pagination: ".swiper-pagination",
          autoplay: false
        }
      };
    },
    computed: {
      pages(){
        const pages = []; // pages是为二维数组
        this.iconList.forEach((item, index) => {
          const page = Math.floor(index / 8); // page本质是0.1.2.3分别表示第1,2,3,4页
          if (!pages[page]) {
            pages[page] = [];
          }
          pages[page].push(item);
        });
        return pages;
      }
    }
  };

原文地址: https://blog.csdn.net/qq_41115965/article/details/81570661

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基本的Vue数据分页示例: ```html <template> <div> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Email</th> <th>Phone</th> </tr> </thead> <tbody> <tr v-for="(user, index) in pagedUsers" :key="index"> <td>{{ user.id }}</td> <td>{{ user.name }}</td> <td>{{ user.email }}</td> <td>{{ user.phone }}</td> </tr> </tbody> </table> <div class="pagination"> <button @click="prevPage">Prev</button> <span v-for="page in totalPage" :key="page"> <button :class="{ active: page === currentPage }" @click="setCurrentPage(page)">{{ page }}</button> </span> <button @click="nextPage">Next</button> </div> </div> </template> <script> export default { data() { return { users: [], // 所有用户数据 currentPage: 1, // 当前页码 pageSize: 10, // 每页数据量 }; }, computed: { // 计算总页数 totalPage() { return Math.ceil(this.users.length / this.pageSize); }, // 根据当前页码和每页数据量计算分页后的用户数据 pagedUsers() { const startIndex = (this.currentPage - 1) * this.pageSize; const endIndex = startIndex + this.pageSize; return this.users.slice(startIndex, endIndex); }, }, methods: { // 切换到上一页 prevPage() { if (this.currentPage > 1) { this.currentPage--; } }, // 切换到下一页 nextPage() { if (this.currentPage < this.totalPage) { this.currentPage++; } }, // 设置当前页码 setCurrentPage(page) { this.currentPage = page; }, }, mounted() { // 获取所有用户数据 // 这里假设已经获取到了 users 数据 // 可以是从接口获取,或者直接写死的静态数据 this.users = [ { id: 1, name: "Tom", email: "tom@example.com", phone: "1234567890" }, { id: 2, name: "Jerry", email: "jerry@example.com", phone: "0987654321" }, // ... ]; }, }; </script> ``` 在这个示例中,我们首先定义了三个数据属性: - `users`:存放所有用户数据的数组。 - `currentPage`:当前页码。 - `pageSize`:每页数据量。 然后,我们定义了两个计算属性: - `totalPage`:根据 `users` 和 `pageSize` 计算出总页数。 - `pagedUsers`:根据 `currentPage` 和 `pageSize` 计算出当前页的用户数据。 最后,我们定义了三个方法: - `prevPage`:切换到上一页。 - `nextPage`:切换到下一页。 - `setCurrentPage`:设置当前页码。 在 `mounted` 钩子中,我们可以获取所有用户数据,这里简单地用静态数据来模拟。 在模板中,我们使用 `v-for` 指令来展示分页后的用户数据。同时,我们也使用了计算属性 `totalPage` 和 `pagedUsers` 来展示页码和分页后的用户数据。最后,我们使用了两个按钮来切换上一页和下一页,并展示了所有页码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值