当前位置:首页 > 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>

样式优化

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

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;
    }
  }
}

使用第三方库

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

  1. 安装 vue-paginate

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

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

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

vue实现ul翻页

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

相关文章

vue 实现长列表

vue 实现长列表

Vue 实现长列表的优化方案 虚拟滚动技术 虚拟滚动是处理长列表的高效方式,仅渲染可视区域内的元素。通过计算滚动位置动态加载和卸载DOM节点,大幅减少内存占用和渲染压力。 <template&…

vue实现边框

vue实现边框

Vue 实现边框的方法 在 Vue 中实现边框效果可以通过多种方式完成,包括内联样式、CSS 类绑定、动态样式以及使用第三方 UI 库。以下是几种常见的实现方法。 内联样式绑定 使用 Vue 的 :…

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生改…

vue实现grid

vue实现grid

Vue 实现 Grid 布局的方法 使用 CSS Grid 布局 Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。 &l…

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,…

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…