当前位置:首页 > VUE

vue分页插件实现

2026-03-08 21:53:02VUE

vue分页插件实现

使用Element UI的分页组件

Element UI提供了现成的分页组件el-pagination,可以直接集成到Vue项目中。安装Element UI后,在组件中引入分页组件并配置相关属性。

<template>
  <el-pagination
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-sizes="[10, 20, 30, 50]"
    :page-size="pageSize"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total">
  </el-pagination>
</template>

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

自定义分页组件

如果需要更灵活的分页逻辑,可以自定义分页组件。以下是一个简单的自定义分页实现。

vue分页插件实现

<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: {
    totalItems: Number,
    itemsPerPage: Number,
    currentPage: 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 {
  margin: 0 5px;
}
.pagination button.active {
  font-weight: bold;
}
</style>

使用第三方分页库

如果需要更复杂的分页功能,可以使用第三方库如vuejs-paginate。安装后,可以直接在项目中使用。

npm install vuejs-paginate

在组件中使用:

vue分页插件实现

<template>
  <paginate
    :page-count="totalPages"
    :click-handler="changePage"
    :prev-text="'Prev'"
    :next-text="'Next'"
    :container-class="'pagination'">
  </paginate>
</template>

<script>
import Paginate from 'vuejs-paginate';

export default {
  components: {
    Paginate
  },
  data() {
    return {
      totalPages: 10,
      currentPage: 1
    }
  },
  methods: {
    changePage(pageNum) {
      this.currentPage = pageNum;
      this.fetchData();
    }
  }
}
</script>

分页与API结合

在实际项目中,分页通常需要与后端API结合。以下是一个结合API请求的分页实现示例。

methods: {
  fetchData() {
    axios.get('/api/data', {
      params: {
        page: this.currentPage,
        size: this.pageSize
      }
    })
    .then(response => {
      this.dataList = response.data.items;
      this.total = response.data.total;
    })
    .catch(error => {
      console.error(error);
    });
  }
}

分页样式优化

分页组件的样式可以通过CSS进一步优化,例如添加过渡效果或调整按钮间距。

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.pagination button {
  padding: 5px 10px;
  margin: 0 5px;
  border: 1px solid #ddd;
  background: #fff;
  cursor: pointer;
}
.pagination button:hover {
  background: #f0f0f0;
}
.pagination button.active {
  background: #409eff;
  color: #fff;
  border-color: #409eff;
}

标签: 分页插件
分享给朋友:

相关文章

vue分页实现

vue分页实现

Vue 分页实现方法 基础分页组件实现 创建一个简单的分页组件,包含上一页、下一页和页码按钮。组件接收当前页码、总页数等参数,通过事件通知父组件页码变化。 <template> &l…

react 如何分页

react 如何分页

分页实现方法 在React中实现分页功能可以通过多种方式完成,具体取决于数据来源(如API或本地数据)和UI库的选择。以下是常见的实现方法: 使用本地数据分页 对于存储在组件状态或Context中…

php实现的分页

php实现的分页

PHP 实现分页的方法 基本分页逻辑 分页的核心在于计算总页数、当前页码以及数据偏移量。假设每页显示 10 条数据: $current_page = isset($_GET['page']) ? (…

swiper插件如何react

swiper插件如何react

安装 Swiper 依赖 在 React 项目中安装 Swiper 的核心库和 React 组件包。通过 npm 或 yarn 安装以下依赖: npm install swiper react-id…

vue实现接口分页

vue实现接口分页

Vue 实现接口分页的方法 在 Vue 中实现接口分页通常需要结合后端 API 和前端分页组件。以下是常见的实现方式: 使用 Element UI 的分页组件 安装 Element UI 后,可以使…

vue实现分页多元

vue实现分页多元

Vue 实现分页的多种方法 在 Vue 中实现分页功能有多种方式,可以根据项目需求选择合适的方法。以下是几种常见的实现方式: 使用 Element UI 的分页组件 Element UI 提供了现成…