当前位置:首页 > VUE

vue查询分页前端实现

2026-01-20 01:02:19VUE

Vue 分页前端实现

基础分页组件实现

使用 Vue 的 v-for 和计算属性实现基础分页逻辑。以下是一个简单的分页组件示例:

<template>
  <div>
    <ul>
      <li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
    </ul>
    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  props: ['data', 'perPage'],
  data() {
    return {
      currentPage: 1
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.data.length / this.perPage)
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.perPage
      const end = start + this.perPage
      return this.data.slice(start, end)
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    }
  }
}
</script>

分页样式优化

添加 CSS 样式提升分页组件的视觉效果:

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
  gap: 10px;
}

.pagination button {
  padding: 5px 10px;
  border: 1px solid #ddd;
  background: #fff;
  cursor: pointer;
}

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

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

完整数字分页实现

扩展基础分页,显示完整的页码列表:

vue查询分页前端实现

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

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

    <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
    <button @click="goToPage(totalPages)" :disabled="currentPage === totalPages">尾页</button>
  </div>
</template>

<script>
export default {
  computed: {
    visiblePages() {
      const range = 2 // 显示当前页前后各2页
      let start = Math.max(1, this.currentPage - range)
      let end = Math.min(this.totalPages, this.currentPage + range)

      if (this.currentPage - range <= 1) {
        end = Math.min(2 * range + 1, this.totalPages)
      }

      if (this.currentPage + range >= this.totalPages) {
        start = Math.max(1, this.totalPages - 2 * range)
      }

      const pages = []
      for (let i = start; i <= end; i++) {
        pages.push(i)
      }
      return pages
    }
  },
  methods: {
    goToPage(page) {
      this.currentPage = page
    }
  }
}
</script>

与后端API结合

实现与后端API的分页数据请求:

methods: {
  async fetchData(page = 1) {
    try {
      const response = await axios.get('/api/data', {
        params: {
          page,
          per_page: this.perPage
        }
      })
      this.data = response.data.items
      this.totalItems = response.data.total
    } catch (error) {
      console.error('获取数据失败:', error)
    }
  },
  goToPage(page) {
    this.currentPage = page
    this.fetchData(page)
  }
}

使用第三方分页组件

推荐使用成熟的第三方分页组件如 vue-paginate

vue查询分页前端实现

  1. 安装:

    npm install vue-paginate
  2. 使用示例:

    
    import VuePaginate from 'vue-paginate'
    Vue.use(VuePaginate)

// 模板中使用

```

这些方法提供了从基础到高级的Vue分页实现方案,可根据项目需求选择合适的实现方式。

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

相关文章

vue弹幕实现不重叠

vue弹幕实现不重叠

实现 Vue 弹幕不重叠的方法 CSS 定位与动画控制 通过绝对定位和动态计算弹幕轨道高度,确保弹幕在不同轨道上运行。每条弹幕占据固定高度的轨道,避免重叠。 // 弹幕轨道高度计算 const tr…

vue前端分页怎么实现

vue前端分页怎么实现

前端分页实现方法 在Vue中实现前端分页通常涉及数据分割和页面控制逻辑。以下是常见的实现方式: 使用计算属性分页 通过计算属性对数据进行切片处理: computed: { paginatedD…

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue实现多用户登录

vue实现多用户登录

实现多用户登录的基本思路 在Vue中实现多用户登录通常需要结合后端API完成身份验证,并通过前端路由、状态管理(如Vuex或Pinia)和本地存储(如localStorage)来管理用户会话。以下是关…

vue实现下拉刷新组件

vue实现下拉刷新组件

实现下拉刷新组件的核心思路 下拉刷新功能通常通过监听触摸事件、滚动位置和动画效果实现。Vue中可以利用自定义指令或封装组件完成,以下为两种常见实现方式。 基于自定义指令的实现 创建指令监听触摸事件,…

vue实现边框

vue实现边框

Vue 实现边框的方法 在 Vue 中实现边框效果可以通过多种方式完成,包括内联样式、CSS 类绑定、动态样式以及使用第三方 UI 库。以下是几种常见的实现方法。 内联样式绑定 使用 Vue 的…