当前位置:首页 > VUE

vue实现列表翻页

2026-02-19 20:09:51VUE

使用 Vue 实现列表翻页

分页组件实现

在 Vue 中实现列表翻页通常需要结合分页组件和数据请求逻辑。以下是一个基础实现方案:

<template>
  <div>
    <ul>
      <li v-for="item in currentPageData" :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 {
  data() {
    return {
      allData: [], // 所有数据
      currentPage: 1, // 当前页码
      pageSize: 10 // 每页显示数量
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.allData.length / this.pageSize)
    },
    currentPageData() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      return this.allData.slice(start, end)
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
      }
    }
  },
  async created() {
    // 这里替换为实际的数据获取逻辑
    this.allData = await fetchData()
  }
}
</script>

与后端API交互

实际项目中通常需要从后端API获取分页数据:

methods: {
  async fetchPageData(page) {
    try {
      const response = await axios.get('/api/items', {
        params: {
          page,
          size: this.pageSize
        }
      })
      this.allData = response.data.items
      this.totalCount = response.data.total
    } catch (error) {
      console.error('获取数据失败:', error)
    }
  }
}

使用第三方分页组件

可以考虑使用现成的分页组件如 vue-paginateelement-ui 的分页组件:

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

无限滚动实现

对于移动端或需要流畅滚动体验的场景,可以使用无限滚动:

import InfiniteLoading from 'vue-infinite-loading'

export default {
  components: { InfiniteLoading },
  methods: {
    async loadMore($state) {
      try {
        const newData = await fetchMoreData()
        if (newData.length) {
          this.items.push(...newData)
          $state.loaded()
        } else {
          $state.complete()
        }
      } catch (err) {
        $state.error()
      }
    }
  }
}

性能优化

对于大型数据集,考虑虚拟滚动技术以提高性能:

<template>
  <RecycleScroller
    class="scroller"
    :items="items"
    :item-size="50"
    key-field="id">
    <template v-slot="{ item }">
      <div class="item">
        {{ item.name }}
      </div>
    </template>
  </RecycleScroller>
</template>

以上方案可以根据具体项目需求进行组合和调整,实现适合不同场景的列表翻页功能。

vue实现列表翻页

标签: 翻页列表
分享给朋友:

相关文章

vue实现翻页

vue实现翻页

Vue实现翻页功能 翻页功能通常结合后端API分页数据实现,以下是基于Vue 2或Vue 3的实现方案: 基础数据准备 需要定义当前页码、每页条数、总数据量等核心变量: data() { re…

jquery 列表

jquery 列表

jQuery 列表操作 jQuery 提供了多种方法来操作 HTML 列表(如 <ul> 或 <ol>)。以下是一些常见的操作方式: 动态添加列表项 使用 append()…

vue实现滑动翻页

vue实现滑动翻页

Vue 实现滑动翻页的方法 使用 touch 事件监听 在 Vue 组件中监听 touchstart、touchmove 和 touchend 事件,通过计算滑动距离判断翻页方向。 <tem…

uniapp 内容列表

uniapp 内容列表

uniapp 内容列表的实现方法 在uniapp中实现内容列表通常需要结合组件和数据绑定。以下是一个常见的方法: 模板部分 <template> <view class="c…

vue实现列表页

vue实现列表页

Vue 实现列表页的方法 数据绑定与渲染 使用 v-for 指令动态渲染列表数据,结合 :key 确保渲染性能。示例代码: <template> <ul> &l…

vue滚动实现翻页

vue滚动实现翻页

实现滚动翻页的基本思路 在Vue中实现滚动翻页,通常需要监听滚动事件,判断是否滚动到页面底部,然后触发加载更多数据的操作。这可以通过结合原生DOM事件或第三方库来实现。 使用原生滚动事件监听 在V…