当前位置:首页 > VUE

vue实现分页页面

2026-01-23 11:31:53VUE

Vue实现分页页面

分页组件实现

创建一个独立的Pagination.vue组件,接收当前页码、总页数等参数,通过事件触发页码变化。

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

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

    <button 
      @click="changePage(currentPage + 1)" 
      :disabled="currentPage === totalPages"
    >下一页</button>
  </div>
</template>

<script>
export default {
  props: {
    currentPage: Number,
    totalPages: Number,
    maxVisiblePages: {
      type: Number,
      default: 5
    }
  },
  computed: {
    pages() {
      const range = [];
      const half = Math.floor(this.maxVisiblePages / 2);
      let start = Math.max(1, this.currentPage - half);
      let end = Math.min(this.totalPages, start + this.maxVisiblePages - 1);

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

      for (let i = start; i <= end; i++) {
        range.push(i);
      }
      return range;
    }
  },
  methods: {
    changePage(page) {
      if (page >= 1 && page <= this.totalPages) {
        this.$emit('page-changed', page);
      }
    }
  }
};
</script>

<style>
.pagination {
  display: flex;
  gap: 5px;
  margin-top: 20px;
}
.active {
  background-color: #42b983;
  color: white;
}
</style>

数据分页处理

在父组件中处理数据分页逻辑,通常与API请求结合。

vue实现分页页面

<template>
  <div>
    <table>
      <tr v-for="item in paginatedData" :key="item.id">
        <td>{{ item.name }}</td>
      </tr>
    </table>

    <Pagination 
      :current-page="currentPage" 
      :total-pages="totalPages"
      @page-changed="handlePageChange"
    />
  </div>
</template>

<script>
import Pagination from './Pagination.vue';

export default {
  components: { Pagination },
  data() {
    return {
      allData: [],
      currentPage: 1,
      itemsPerPage: 10
    };
  },
  computed: {
    totalPages() {
      return Math.ceil(this.allData.length / this.itemsPerPage);
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage;
      const end = start + this.itemsPerPage;
      return this.allData.slice(start, end);
    }
  },
  methods: {
    handlePageChange(page) {
      this.currentPage = page;
    },
    async fetchData() {
      try {
        const response = await api.get('/items');
        this.allData = response.data;
      } catch (error) {
        console.error(error);
      }
    }
  },
  created() {
    this.fetchData();
  }
};
</script>

服务器端分页

对于大数据量,建议使用服务器端分页,减少客户端压力。

vue实现分页页面

async fetchPaginatedData(page) {
  try {
    const response = await api.get('/items', {
      params: {
        page,
        limit: this.itemsPerPage
      }
    });
    this.paginatedData = response.data.items;
    this.totalItems = response.data.total;
  } catch (error) {
    console.error(error);
  }
}

分页样式优化

添加过渡效果和响应式设计提升用户体验。

.pagination button {
  padding: 5px 10px;
  border: 1px solid #ddd;
  background: white;
  cursor: pointer;
  transition: all 0.3s;
}

.pagination button:hover:not(:disabled) {
  background: #eee;
}

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

@media (max-width: 600px) {
  .pagination {
    flex-wrap: wrap;
  }
}

分页功能扩展

添加每页显示数量选择器和跳转到指定页码功能。

<template>
  <div class="pagination-controls">
    <select v-model="itemsPerPage" @change="handleItemsPerPageChange">
      <option value="5">5条/页</option>
      <option value="10">10条/页</option>
      <option value="20">20条/页</option>
    </select>

    <input 
      type="number" 
      v-model="goToPage" 
      min="1" 
      :max="totalPages"
      @keyup.enter="handleGoToPage"
    >
    <button @click="handleGoToPage">跳转</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      goToPage: 1,
      itemsPerPage: 10
    };
  },
  methods: {
    handleItemsPerPageChange() {
      this.currentPage = 1;
      this.$emit('items-per-page-changed', this.itemsPerPage);
    },
    handleGoToPage() {
      if (this.goToPage >= 1 && this.goToPage <= this.totalPages) {
        this.$emit('page-changed', parseInt(this.goToPage));
      }
    }
  }
};
</script>

标签: 分页页面
分享给朋友:

相关文章

vue实现选项卡分页

vue实现选项卡分页

Vue实现选项卡分页的方法 使用动态组件切换 通过Vue的<component :is="currentTab">动态加载不同组件,结合v-for循环渲染选项卡标题,点击时切换curre…

vue router实现分页

vue router实现分页

Vue Router 实现分页的方法 在 Vue.js 中,可以通过 Vue Router 实现分页功能,通常需要结合路由参数和动态路由匹配。以下是几种常见的实现方式: 使用查询参数实现分页 在路由…

vue实现结算页面

vue实现结算页面

Vue 实现结算页面的核心步骤 数据绑定与表单验证 使用 Vue 的 v-model 绑定表单数据,例如收货地址、支付方式等。通过 Vue 的 computed 属性计算总价,结合 v-if 或 v-…

vue实现页面切换

vue实现页面切换

Vue 实现页面切换的方法 在 Vue 中实现页面切换通常可以通过以下几种方式完成,具体选择取决于项目需求和架构设计。 使用 Vue Router Vue Router 是 Vue.js 官方推荐的…

vue 实现页面跳转

vue 实现页面跳转

vue 实现页面跳转的方法 在 Vue 中实现页面跳转主要通过路由(Vue Router)完成,以下是几种常见的方式: 声明式导航(模板中使用 <router-link>) 在模板中直接…

Vue实现lagger页面

Vue实现lagger页面

Vue实现懒加载页面的方法 懒加载(Lazy Loading)是一种优化技术,用于延迟加载页面或组件,直到它们真正需要显示时才加载。以下是几种在Vue中实现懒加载页面的常见方法: 使用Vue Rou…