当前位置:首页 > VUE

vue Bootstrap实现分页

2026-03-28 20:08:12VUE

Vue Bootstrap 分页实现方法

在 Vue 项目中使用 Bootstrap 实现分页功能,可以通过以下步骤完成:

安装 Bootstrap 和 Vue Bootstrap

确保项目中已安装 Bootstrap 和 Vue Bootstrap。如果没有安装,可以通过以下命令安装:

npm install bootstrap @popperjs/core
npm install bootstrap-vue

main.js 中引入 Bootstrap 和 Vue Bootstrap:

import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
import { BootstrapVue } from 'bootstrap-vue'
Vue.use(BootstrapVue)

使用 b-pagination 组件

Vue Bootstrap 提供了 b-pagination 组件,可以轻松实现分页功能。以下是一个基本示例:

<template>
  <div>
    <b-pagination
      v-model="currentPage"
      :total-rows="totalRows"
      :per-page="perPage"
      aria-controls="my-table"
    ></b-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      totalRows: 100,
      perPage: 10
    }
  }
}
</script>

分页与表格数据绑定

通常分页需要与表格数据绑定,以下是一个完整示例:

<template>
  <div>
    <b-table
      id="my-table"
      :items="items"
      :per-page="perPage"
      :current-page="currentPage"
      :fields="fields"
    ></b-table>
    <b-pagination
      v-model="currentPage"
      :total-rows="totalRows"
      :per-page="perPage"
      aria-controls="my-table"
    ></b-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      perPage: 5,
      totalRows: 1,
      fields: ['id', 'name', 'age'],
      items: [
        { id: 1, name: 'John', age: 20 },
        { id: 2, name: 'Jane', age: 22 },
        { id: 3, name: 'Bob', age: 25 },
        { id: 4, name: 'Alice', age: 23 },
        { id: 5, name: 'Tom', age: 21 },
        { id: 6, name: 'Jerry', age: 24 }
      ]
    }
  },
  created() {
    this.totalRows = this.items.length
  }
}
</script>

自定义分页样式

可以通过 b-pagination 组件的属性自定义分页样式:

<b-pagination
  v-model="currentPage"
  :total-rows="totalRows"
  :per-page="perPage"
  size="sm"
  align="center"
  pills
></b-pagination>

分页事件处理

可以通过 @change 事件监听页码变化:

<b-pagination
  v-model="currentPage"
  :total-rows="totalRows"
  :per-page="perPage"
  @change="onPageChange"
></b-pagination>
methods: {
  onPageChange(page) {
    console.log('Current page is: ' + page)
    // 可以在这里处理数据加载逻辑
  }
}

分页与 API 数据加载

在实际项目中,分页通常与后端 API 结合使用:

vue Bootstrap实现分页

methods: {
  async fetchData(page) {
    const response = await axios.get(`/api/items?page=${page}&perPage=${this.perPage}`)
    this.items = response.data.items
    this.totalRows = response.data.total
  },
  onPageChange(page) {
    this.fetchData(page)
  }
},
created() {
  this.fetchData(1)
}

通过以上方法,可以在 Vue 项目中轻松实现 Bootstrap 分页功能,并根据需求进行定制和扩展。

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

相关文章

vue页面分离的实现

vue页面分离的实现

Vue页面分离的实现方法 将Vue页面分离为多个组件或模块,有助于提升代码可维护性和复用性。以下是几种常见的实现方式: 组件化拆分 通过将页面拆分为多个子组件,每个组件负责特定功能或UI部分。使用i…

vue实现素材

vue实现素材

Vue 实现素材的方法 使用 Vue 组件管理素材 在 Vue 项目中,可以通过组件化的方式管理素材。创建一个专门的组件来加载和显示素材,例如图片、视频或音频文件。组件可以接收素材的路径或 URL 作…

vue observer实现

vue observer实现

Vue Observer 实现原理 Vue 的响应式系统核心是通过 Object.defineProperty(Vue 2)或 Proxy(Vue 3)实现的 Observer 模式。以下是关键实现细…

vue实现swipe

vue实现swipe

Vue实现Swipe功能的方法 使用第三方库(推荐) Vue生态中有多个成熟的轮播/滑动组件库,例如vue-awesome-swiper或swiper/vue。以下是基于swiper/vue的实现示例…

实现vue cli

实现vue cli

安装 Vue CLI 确保系统中已安装 Node.js(版本建议 12.x 或更高)。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 安装完成后验证版本:…

vue实现addclass

vue实现addclass

Vue 实现动态添加 class 的方法 在 Vue 中动态添加 class 可以通过多种方式实现,以下是常见的几种方法: 使用对象语法 通过绑定一个对象到 :class,可以动态切换 class…