以下是一个使用Vue 3的数据分页功能的示例:

<template>
  <div>
    <h1>Data Pagination</h1>

    <ul>
      <li v-for="item in currentPageData" :key="item.id">{{ item.title }}</li>
    </ul>

    <div>
      <button @click="prevPage" :disabled="currentPage === 1">Previous Page</button>
      <span>{{ currentPage }}</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">Next Page</button>
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue';

export default {
  name: 'DataPagination',
  data() {
    return {
      currentPage: 1,
      itemsPerPage: 5,
      data: [
        { id: 1, title: 'Item 1' },
        { id: 2, title: 'Item 2' },
        { id: 3, title: 'Item 3' },
        { id: 4, title: 'Item 4' },
        { id: 5, title: 'Item 5' },
        { id: 6, title: 'Item 6' },
        { id: 7, title: 'Item 7' },
        // ...
      ]
    };
  },
  computed: {
    totalPages() {
      return Math.ceil(this.data.length / this.itemsPerPage);
    },
    currentPageData() {
      const startIndex = (this.currentPage - 1) * this.itemsPerPage;
      const endIndex = startIndex + this.itemsPerPage;
      return this.data.slice(startIndex, endIndex);
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--;
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++;
      }
    }
  }
};
</script>

在上面的示例中,我们使用了refcomputed来定义了currentPageitemsPerPagedata的响应式状态。totalPages是一个计算属性,根据数据长度和每页显示的项数计算出总页数。currentPageData是另一个计算属性,根据当前页数和每页显示的项数计算出当前页的数据。prevPagenextPage方法用于切换上一页和下一页,并在边界处禁用相应的按钮。

请注意,上面的代码只是一个简单示例,实际的数据分页功能可能需要更多的逻辑和交互

vue3写一个数据分页功能

原文地址: https://www.cveoy.top/t/topic/ic1K 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录