当前位置:首页 > 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中实现后台分页通常需要结合前端和后端逻辑,后端负责处理数据分页,前端负责展示分页数据和交互。以下是具体实现方法: 后端API设计 后端需要提供支持分页的API接口,通常包…

jquery页面

jquery页面

jQuery 页面操作指南 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互的操作。以下是 jQuery 在页面中的常见使用方法…

vue router实现分页

vue router实现分页

使用 Vue Router 实现分页 在 Vue.js 项目中,可以通过 Vue Router 结合分页组件实现分页功能。以下是实现分页的几种方法: 使用路由参数传递页码 在路由配置中定义一个动态参…

vue 实现页面注册

vue 实现页面注册

Vue 实现页面注册功能 在 Vue 中实现用户注册功能通常需要以下步骤: 创建注册表单组件 使用 Vue 的单文件组件结构创建一个注册表单,包含必要的输入字段如用户名、邮箱、密码等。 <t…

jquery页面跳转

jquery页面跳转

jQuery 页面跳转方法 使用 jQuery 实现页面跳转可以通过多种方式完成,以下是几种常见的方法: 使用 window.location.href 通过修改 window.location.h…

vue实现页面签字

vue实现页面签字

实现页面签名的Vue方案 在Vue中实现页面签名功能,可以通过HTML5的Canvas元素结合手势或鼠标事件来捕获用户绘制轨迹。以下是具体实现方法: 安装签名库(可选) 使用现成的Vue签名…