当前位置:首页 > 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 插件 在 Vue 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交换…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基础…

vue nexttrick实现

vue nexttrick实现

Vue 中 nextTick 的实现原理 nextTick 是 Vue 提供的一个异步方法,用于在下次 DOM 更新循环结束之后执行延迟回调。它利用了 JavaScript 的事件循环机制,确保回调在…

vue实现刷新

vue实现刷新

Vue 实现页面刷新的方法 在Vue中实现页面刷新可以通过多种方式,以下是几种常见的方法: 使用 window.location.reload() 通过调用浏览器的原生方法强制刷新当前页面: me…

vue实现ftp

vue实现ftp

Vue 中实现 FTP 功能 Vue 本身是一个前端框架,无法直接操作 FTP 协议(FTP 通常需要后端支持)。以下是两种常见实现方式: 前端与后端配合实现 FTP 前端通过 API 与后端通信,…

vue实现下拉实现

vue实现下拉实现

Vue 实现下拉菜单的方法 使用原生 HTML 和 Vue 指令 通过 Vue 的 v-model 和 v-show 指令可以快速实现下拉菜单。定义一个布尔值控制下拉菜单的显示与隐藏,点击按钮切换状态…