当前位置:首页 > VUE

vue实现ul翻页

2026-02-20 14:19:46VUE

实现 UL 列表翻页功能

在 Vue 中实现 ul 列表的翻页功能,可以通过分页逻辑和动态渲染列表实现。以下是具体实现方法:

分页数据计算

定义分页相关数据和方法,包括当前页码、每页显示数量、总数据量等:

data() {
  return {
    items: [], // 原始数据列表
    currentPage: 1, // 当前页码
    itemsPerPage: 10 // 每页显示数量
  };
},
computed: {
  paginatedItems() {
    const start = (this.currentPage - 1) * this.itemsPerPage;
    const end = start + this.itemsPerPage;
    return this.items.slice(start, end);
  },
  totalPages() {
    return Math.ceil(this.items.length / this.itemsPerPage);
  }
}

模板渲染

在模板中使用 v-for 渲染分页后的列表,并添加翻页按钮:

<ul>
  <li v-for="(item, index) in paginatedItems" :key="index">
    {{ item }}
  </li>
</ul>

<div class="pagination">
  <button 
    @click="currentPage--" 
    :disabled="currentPage === 1">
    上一页
  </button>
  <span>第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span>
  <button 
    @click="currentPage++" 
    :disabled="currentPage >= totalPages">
    下一页
  </button>
</div>

样式优化

添加基础样式使翻页控件更美观:

vue实现ul翻页

ul {
  list-style: none;
  padding: 0;
}

.pagination {
  margin-top: 20px;
  display: flex;
  align-items: center;
  gap: 10px;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

完整组件示例

<template>
  <div>
    <ul>
      <li v-for="(item, index) in paginatedItems" :key="index">
        {{ item }}
      </li>
    </ul>

    <div class="pagination">
      <button 
        @click="currentPage--" 
        :disabled="currentPage === 1">
        上一页
      </button>
      <span>第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span>
      <button 
        @click="currentPage++" 
        :disabled="currentPage >= totalPages">
        下一页
      </button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: Array.from({length: 100}, (_, i) => `项目 ${i + 1}`),
      currentPage: 1,
      itemsPerPage: 10
    };
  },
  computed: {
    paginatedItems() {
      const start = (this.currentPage - 1) * this.itemsPerPage;
      const end = start + this.itemsPerPage;
      return this.items.slice(start, end);
    },
    totalPages() {
      return Math.ceil(this.items.length / this.itemsPerPage);
    }
  }
};
</script>

<style>
/* 添加前面提到的样式 */
</style>

进阶优化

添加页码跳转功能:

<input 
  type="number" 
  v-model.number="inputPage" 
  min="1" 
  :max="totalPages">
<button @click="goToPage">跳转</button>
data() {
  return {
    inputPage: 1
  };
},
methods: {
  goToPage() {
    if (this.inputPage >= 1 && this.inputPage <= this.totalPages) {
      this.currentPage = this.inputPage;
    }
  }
}

使用第三方库

如果需要更复杂的分页功能,可以考虑使用分页组件库:

vue实现ul翻页

  1. 安装 vue-paginate

    npm install vue-paginate
  2. 使用示例:

    
    import VuePaginate from 'vue-paginate';
    Vue.use(VuePaginate);

methods: { changePage(pageNum) { this.currentPage = pageNum; } }

标签: 翻页vue
分享给朋友:

相关文章

vue前端实现登录

vue前端实现登录

实现登录功能的基本流程 使用Vue实现登录功能需要结合前端框架和后端API交互。以下是一个典型的实现方案: 安装必要依赖 确保项目已安装axios用于HTTP请求: npm install axi…

vue实现绑卡的原理

vue实现绑卡的原理

Vue 实现绑卡的原理 Vue 实现绑卡功能通常涉及前端表单处理、数据绑定、验证以及和后端 API 的交互。以下是实现绑卡功能的核心原理和步骤: 数据绑定与表单处理 Vue 通过 v-model 指…

vue实现加减

vue实现加减

Vue 实现加减功能 在 Vue 中实现加减功能可以通过数据绑定和事件监听来完成。以下是一个简单的实现方法: 模板部分 <template> <div> <…

vue实现tabbar

vue实现tabbar

Vue 实现 TabBar 的方法 使用 Vue Router 实现基础 TabBar 安装 Vue Router 依赖: npm install vue-router 创建路由配置文件(route…

vue 实现ztree

vue 实现ztree

在Vue中实现类似ZTree的功能,可以通过集成第三方库或自定义组件完成。以下是两种常见方法: 使用vue-tree组件 vue-tree是一个轻量级的Vue树形组件,支持复选框、懒加载等功能,适合…

vue实现追加

vue实现追加

追加数据到数组或列表 在Vue中追加数据到数组或列表,可以通过push方法或concat方法实现。以下是几种常见的实现方式: 方法一:使用push方法 this.items.push(newIte…