当前位置:首页 > 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中实现筛选功能通常涉及以下几个核心步骤: 数据绑定与筛选逻辑 使用v-model绑定筛选条件到Vue实例的数据属性,结合计算属性实现动态筛选: data() {…

vue 实现平面

vue 实现平面

Vue 实现平面的方法 在 Vue 中实现平面效果通常涉及 CSS 样式、UI 框架或自定义组件的使用。以下是几种常见方法: 使用 CSS 样式 通过 Vue 的样式绑定或 scoped CSS 为…

vue实现登录退出

vue实现登录退出

实现登录功能 在Vue中实现登录功能通常需要结合表单验证、API请求和状态管理。以下是一个基础实现示例: 安装必要依赖(如axios和vuex): npm install axios vuex 创…

vue实现左右菜单联动实现

vue实现左右菜单联动实现

Vue 实现左右菜单联动 使用事件总线通信 在 Vue 中可以通过事件总线实现左右菜单的通信。创建一个中央事件总线实例,让左右菜单通过事件监听和触发实现联动。 // eventBus.js impo…

vue实现html跳页

vue实现html跳页

Vue 实现 HTML 跳页的方法 在 Vue 中实现页面跳转(路由跳转)通常有两种方式:通过 <router-link> 组件或编程式导航。以下是具体实现方法。 使用 <rout…

vue 实现页面注册

vue 实现页面注册

Vue 实现页面注册功能 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,通常命名为 Register.vue。该组件包含用户名、邮箱、密码等输入字段,以及提交按钮。 <templa…