当前位置:首页 > VUE

vue实现页面分页

2026-03-07 22:20:16VUE

Vue实现页面分页的方法

使用Element UI的分页组件

Element UI提供了现成的分页组件el-pagination,适合快速集成。
安装Element UI后,在组件中引入分页组件并绑定数据:

vue实现页面分页

<template>
  <el-pagination
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-size="pageSize"
    :total="total">
  </el-pagination>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 100
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    fetchData() {
      // 根据currentPage和pageSize请求数据
    }
  }
};
</script>

自定义分页逻辑

如果需要手动实现分页,可以通过计算属性对数据进行切片:

vue实现页面分页

<template>
  <div>
    <ul>
      <li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
    </ul>
    <button @click="prevPage">上一页</button>
    <span>当前页:{{ currentPage }}</span>
    <button @click="nextPage">下一页</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dataList: [], // 原始数据
      currentPage: 1,
      pageSize: 5
    };
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.dataList.slice(start, end);
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) this.currentPage--;
    },
    nextPage() {
      if (this.currentPage < this.totalPages) this.currentPage++;
    }
  }
};
</script>

结合后端API分页

实际项目中通常需要后端配合,传递分页参数并接收分页结果:

methods: {
  async fetchData() {
    const res = await axios.get('/api/data', {
      params: {
        page: this.currentPage,
        size: this.pageSize
      }
    });
    this.dataList = res.data.items;
    this.total = res.data.total;
  }
}

分页样式优化

通过CSS调整分页组件样式,或使用第三方库如v-pagination

<v-pagination
  v-model="currentPage"
  :pages="totalPages"
  :range-size="3"
  active-color="#4CAF50"
/>

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

相关文章

php实现分页

php实现分页

PHP实现分页的基本方法 使用原生PHP实现分页功能需要结合数据库查询和HTML展示。以下是核心实现步骤: 数据库查询与分页逻辑 // 连接数据库 $conn = new mysqli('loca…

js实现分页

js实现分页

实现分页的基本思路 分页功能通常需要处理数据分割、页码生成和用户交互。核心逻辑包括计算总页数、根据当前页截取数据、渲染页码按钮等。 前端分页实现(静态数据) 假设已有全部数据,仅需前端分页展示:…

实现js页面跳转页面跳转页面

实现js页面跳转页面跳转页面

使用 window.location.href 实现跳转 通过修改 window.location.href 属性,可以直接跳转到目标 URL。这种方法会触发页面刷新,并在浏览器历史记录中留下记录。…

jquery页面加载

jquery页面加载

jQuery 页面加载事件 在 jQuery 中,页面加载事件通常通过 $(document).ready() 或简写的 $() 来实现。这种方式确保代码在 DOM 完全加载后执行,但无需等待图片等资…

vue分页实现原理

vue分页实现原理

Vue 分页实现原理 Vue 分页的核心原理是通过计算当前页的数据范围,动态渲染数据列表并控制分页组件的交互。以下是关键实现步骤: 数据分片计算 分页需要根据当前页码和每页条数截取数据。假设原始数据…

vue实现搜索页面

vue实现搜索页面

Vue 实现搜索页面的方法 基础结构搭建 创建搜索组件,包含输入框和搜索按钮,使用v-model绑定搜索关键词: <template> <div class="search-c…