当前位置:首页 > VUE

vue 分页 实现

2026-01-08 00:33:20VUE

Vue 分页实现方法

使用第三方库(如 Element UI)

Element UI 提供了现成的分页组件 el-pagination,适合快速集成。

安装 Element UI:

npm install element-ui

在 Vue 文件中引入并使用:

<template>
  <div>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      layout="prev, pager, next">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 100
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
      // 加载对应页的数据
    }
  }
};
</script>

自定义分页组件

如果需要完全自定义分页逻辑,可以手动实现分页组件。

定义分页组件:

<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: currentPage === page }">
        {{ page }}
      </button>
    </span>
    <button 
      @click="changePage(currentPage + 1)" 
      :disabled="currentPage === totalPages">
      下一页
    </button>
  </div>
</template>

<script>
export default {
  props: {
    currentPage: Number,
    totalItems: Number,
    itemsPerPage: Number
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.itemsPerPage);
    },
    pages() {
      const range = [];
      for (let i = 1; i <= this.totalPages; i++) {
        range.push(i);
      }
      return range;
    }
  },
  methods: {
    changePage(page) {
      if (page >= 1 && page <= this.totalPages) {
        this.$emit('page-changed', page);
      }
    }
  }
};
</script>

<style>
.pagination button.active {
  background-color: #42b983;
  color: white;
}
</style>

分页数据逻辑

在父组件中处理分页数据请求或过滤:

<template>
  <div>
    <custom-pagination
      :current-page="currentPage"
      :total-items="items.length"
      :items-per-page="itemsPerPage"
      @page-changed="onPageChanged">
    </custom-pagination>
    <ul>
      <li v-for="item in paginatedItems" :key="item.id">
        {{ item.name }}
      </li>
    </ul>
  </div>
</template>

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

export default {
  components: { CustomPagination },
  data() {
    return {
      currentPage: 1,
      itemsPerPage: 5,
      items: [
        { id: 1, name: 'Item 1' },
        // 更多数据...
      ]
    };
  },
  computed: {
    paginatedItems() {
      const start = (this.currentPage - 1) * this.itemsPerPage;
      const end = start + this.itemsPerPage;
      return this.items.slice(start, end);
    }
  },
  methods: {
    onPageChanged(page) {
      this.currentPage = page;
    }
  }
};
</script>

后端分页 API 集成

如果数据来自后端 API,通常需要传递分页参数并处理返回结果。

示例 API 请求:

methods: {
  fetchData(page) {
    axios.get('/api/items', {
      params: {
        page: page,
        per_page: this.itemsPerPage
      }
    }).then(response => {
      this.items = response.data.items;
      this.total = response.data.total;
    });
  }
}

调用时机:

onPageChanged(page) {
  this.currentPage = page;
  this.fetchData(page);
}

通过以上方法,可以实现 Vue 中的分页功能,无论是前端分页还是后端分页均可灵活处理。

vue 分页 实现

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

相关文章

vue无限菜单怎么实现

vue无限菜单怎么实现

实现 Vue 无限菜单的方法 递归组件实现嵌套菜单 使用 Vue 的递归组件特性可以轻松实现无限级嵌套菜单。通过组件调用自身的方式处理嵌套数据。 <template> <ul&…

vue中登陆实现逻辑

vue中登陆实现逻辑

Vue 中登录实现逻辑 前端实现逻辑 表单验证 使用 Vue 的表单验证库(如 VeeValidate)或自定义验证规则,确保用户输入的账号和密码符合要求。例如,检查邮箱格式、密码长度等。 发送登…

vue实现switch

vue实现switch

Vue 实现 Switch 开关组件 在 Vue 中实现 Switch 开关组件可以通过自定义组件或使用现有 UI 库完成。以下是几种常见实现方式: 自定义 Switch 组件 创建一个基础 Sw…

vue实现图册

vue实现图册

Vue实现图册功能 在Vue中实现图册功能通常涉及图片展示、切换、缩放等交互效果。以下是常见的实现方法和关键代码示例: 基础图册组件结构 使用Vue单文件组件构建基础结构: <templ…

vue实现频道

vue实现频道

Vue实现频道功能的方法 使用Vue Router动态路由 通过Vue Router的动态路由功能可以轻松实现频道切换。在路由配置中定义动态参数,组件内通过this.$route.params获取当前…

vue轮播实现

vue轮播实现

Vue 轮播实现方法 使用第三方库(推荐) 安装 swiper 或 vue-awesome-swiper 库,快速实现功能丰富的轮播效果。 以 vue-awesome-swiper 为例: npm…