当前位置:首页 > VUE

vue和bootstrap实现分页

2026-02-23 15:42:22VUE

使用 Vue 和 Bootstrap 实现分页

安装依赖

确保项目中已安装 Vue 和 Bootstrap。通过 npm 或 yarn 安装 Bootstrap 及其 Vue 适配组件:

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

引入 Bootstrap 样式和组件

在项目的入口文件(如 main.js)中引入 Bootstrap 样式和 BootstrapVue:

import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
import BootstrapVue from 'bootstrap-vue'

Vue.use(BootstrapVue)

分页组件实现

在 Vue 组件中使用 BootstrapVue 的 <b-pagination> 组件实现分页功能。以下是一个完整示例:

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

    <b-table
      id="my-table"
      :items="items"
      :per-page="perPage"
      :current-page="currentPage"
      small
    ></b-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' },
        // 更多数据...
      ],
      perPage: 5,
      currentPage: 1
    }
  },
  computed: {
    totalRows() {
      return this.items.length
    }
  }
}
</script>

自定义分页行为

通过监听 currentPage 的变化或调用 API 实现动态数据加载:

watch: {
  currentPage(newVal) {
    this.fetchData(newVal)
  }
},
methods: {
  fetchData(page) {
    // 调用 API 获取分页数据
    axios.get(`/api/items?page=${page}`)
      .then(response => {
        this.items = response.data.items
        this.totalRows = response.data.total
      })
  }
}

分页样式调整

BootstrapVue 的分页组件支持多种样式配置,例如对齐方式、大小等:

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

服务器端分页

对于大数据量场景,建议结合后端分页接口。每次翻页时重新请求数据:

vue和bootstrap实现分页

fetchData(page) {
  axios.get(`/api/items?page=${page}&limit=${this.perPage}`)
    .then(response => {
      this.items = response.data.items
      this.totalRows = response.data.total
    })
}

通过以上步骤,可以快速实现基于 Vue 和 Bootstrap 的分页功能,同时支持客户端和服务器端分页模式。

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

相关文章

vue 实现轮播

vue 实现轮播

Vue 实现轮播的方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template&…

vue实现必填

vue实现必填

Vue 实现表单必填验证的方法 在Vue中实现表单必填验证可以通过多种方式完成,以下是几种常见的方法: 使用HTML5原生属性 最简单的必填验证可以通过HTML5的required属性实现: &l…

vue实现groupbox

vue实现groupbox

Vue 实现 GroupBox 组件 在 Vue 中实现类似 GroupBox 的效果可以通过自定义组件完成。GroupBox 通常是一个带有标题的边框容器,用于将相关控件分组显示。 基本实现方法…

vue实现筛选

vue实现筛选

实现筛选功能的基本思路 在Vue中实现筛选功能通常需要结合数据绑定、计算属性和方法。筛选的核心逻辑是根据用户输入的条件过滤原始数据列表,并动态更新显示结果。 数据准备 定义一个数组存储原始数据,另一…

vue路由实现滑动

vue路由实现滑动

实现 Vue 路由滑动效果的方法 使用 Vue Router 结合 CSS 过渡动画 在 Vue Router 的路由视图组件 <router-view> 上添加过渡效果,结合 CSS 实…

vue实现搜索过滤

vue实现搜索过滤

Vue 实现搜索过滤 使用计算属性实现搜索过滤 在 Vue 中,计算属性(computed)是实现搜索过滤的常见方法。通过计算属性动态过滤数据,无需修改原始数据。 <template>…