当前位置:首页 > 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请求结合。

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

服务器端分页

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

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 实现页面的基本方法 创建 Vue 项目 使用 Vue CLI 快速初始化项目,运行以下命令安装并创建项目: npm install -g @vue/cli vue create my-pro…

vue实现页面跳转

vue实现页面跳转

vue实现页面跳转的方法 在Vue中实现页面跳转主要有以下几种方式: 使用router-link组件 router-link是Vue Router提供的组件,用于声明式导航: <router…

vue组件传值实现分页

vue组件传值实现分页

Vue组件传值实现分页的方法 在Vue中实现分页功能,通常需要父子组件之间的数据传递。以下是几种常见的传值方式: 使用props传递数据 父组件通过props向子组件传递当前页码和总页数等分页信息:…

vue如何实现默认页面

vue如何实现默认页面

Vue 实现默认页面的方法 在 Vue 项目中,通常需要设置一个默认页面(如首页或登录页)作为用户访问时的初始页面。可以通过路由配置实现。 配置路由默认跳转 在 Vue Router 中,使用 re…

js实现分页

js实现分页

分页的基本实现思路 在JavaScript中实现分页功能通常需要结合前端和后端逻辑。前端负责渲染分页控件和处理用户交互,后端负责提供分页数据。 前端分页实现 纯前端分页适用于数据量较小的情况,可以直…

h5实现登录页面跳转页面跳转页面

h5实现登录页面跳转页面跳转页面

实现H5登录页面跳转 在H5中实现登录页面跳转可以通过多种方式完成,以下是几种常见方法: 使用window.location.href window.location.href = '目标页面UR…