当前位置:首页 > VUE

vue实现分页功能例子

2026-01-20 18:10:13VUE

以下是一个基于 Vue 3 的分页功能实现示例,包含基本逻辑和样式:

基础实现代码

<template>
  <div class="pagination-container">
    <button 
      @click="prevPage" 
      :disabled="currentPage === 1"
    >上一页</button>

    <span 
      v-for="page in visiblePages" 
      :key="page"
      @click="goToPage(page)"
      :class="{ active: page === currentPage }"
    >{{ page }}</span>

    <button 
      @click="nextPage" 
      :disabled="currentPage === totalPages"
    >下一页</button>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';

const props = defineProps({
  totalItems: {
    type: Number,
    required: true
  },
  itemsPerPage: {
    type: Number,
    default: 10
  }
});

const currentPage = ref(1);

const totalPages = computed(() => 
  Math.ceil(props.totalItems / props.itemsPerPage)
);

const visiblePages = computed(() => {
  const pages = [];
  const maxVisible = 5;
  let start = Math.max(1, currentPage.value - Math.floor(maxVisible / 2));
  let end = Math.min(totalPages.value, start + maxVisible - 1);

  if (end - start + 1 < maxVisible) {
    start = Math.max(1, end - maxVisible + 1);
  }

  for (let i = start; i <= end; i++) {
    pages.push(i);
  }
  return pages;
});

function prevPage() {
  if (currentPage.value > 1) {
    currentPage.value--;
  }
}

function nextPage() {
  if (currentPage.value < totalPages.value) {
    currentPage.value++;
  }
}

function goToPage(page) {
  currentPage.value = page;
}
</script>

<style scoped>
.pagination-container {
  display: flex;
  gap: 8px;
  align-items: center;
}

button {
  padding: 4px 8px;
  cursor: pointer;
}

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

span {
  padding: 4px 8px;
  cursor: pointer;
}

span.active {
  font-weight: bold;
  color: #42b983;
}
</style>

使用示例

<template>
  <div>
    <!-- 显示分页数据 -->
    <ul>
      <li v-for="item in paginatedData" :key="item.id">
        {{ item.name }}
      </li>
    </ul>

    <!-- 分页组件 -->
    <Pagination 
      :total-items="data.length" 
      :items-per-page="itemsPerPage"
      @page-change="handlePageChange"
    />
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import Pagination from './Pagination.vue';

const data = ref([
  { id: 1, name: 'Item 1' },
  // ...更多数据
]);

const itemsPerPage = 5;
const currentPage = ref(1);

const paginatedData = computed(() => {
  const start = (currentPage.value - 1) * itemsPerPage;
  const end = start + itemsPerPage;
  return data.value.slice(start, end);
});

function handlePageChange(page) {
  currentPage.value = page;
}
</script>

高级功能扩展

  1. 添加每页条数选择器

    <select v-model="itemsPerPage" @change="resetPage">
    <option value="5">5条/页</option>
    <option value="10">10条/页</option>
    <option value="20">20条/页</option>
    </select>
  2. 添加跳转输入框

    <input 
    type="number" 
    v-model="inputPage" 
    min="1" 
    :max="totalPages"
    @keyup.enter="goToInputPage"
    >
  3. 添加省略号显示

    vue实现分页功能例子

    const visiblePages = computed(() => {
    // ...原有逻辑
    if (start > 1) pages.unshift('...');
    if (end < totalPages.value) pages.push('...');
    return pages;
    });

注意事项

  • 分页组件应通过v-model或事件与父组件通信
  • 大数据量时考虑使用虚拟滚动代替传统分页
  • 移动端需调整样式为更适合触摸操作的布局
  • 可结合Vue Router实现URL同步分页状态

标签: 分页例子
分享给朋友:

相关文章

vue实现列表分页

vue实现列表分页

Vue 列表分页实现方法 基础分页实现 安装依赖(如使用第三方库) npm install vue-paginate 模板部分示例 <template> <div>…

vue 实现数据分页

vue 实现数据分页

实现数据分页的基本思路 在Vue中实现数据分页通常需要结合前端分页逻辑和后端API支持。前端分页适合数据量较小的情况,后端分页适合大数据量场景。 前端分页实现 创建一个计算属性来分割当前页数据:…

vue分页代码实现

vue分页代码实现

Vue 分页代码实现 在 Vue 中实现分页功能通常需要结合后端 API 返回的数据和前端的分页组件。以下是一个完整的实现示例,包括分页组件的封装和与后端数据的交互。 分页组件封装 创建一个…

vue实现表格分页

vue实现表格分页

实现表格分页的基本思路 在Vue中实现表格分页通常需要结合以下核心功能:数据分片(根据页码和每页条数截取数据)、分页器组件(控制页码跳转)、表格渲染(展示当前页数据)。以下是具体实现方法。 准备分页…

vue css实现分页

vue css实现分页

实现分页的CSS方法 在Vue项目中实现分页功能,通常需要结合CSS样式来控制分页器的外观和交互效果。以下是几种常见的CSS实现方法: 基础分页样式 通过CSS定义分页容器的基本样式,包括分页按钮…

vue实现手机分页教程

vue实现手机分页教程

Vue实现手机分页的方法 在Vue中实现手机分页功能,可以通过以下步骤完成。这些方法适用于移动端设备,确保用户体验流畅。 使用v-infinite-scroll插件 安装v-infinite-scr…