当前位置:首页 > VUE

分页实现vue

2026-03-07 03:13:13VUE

使用 v-pagination 组件(Element UI)

安装 Element UI:

npm install element-ui

在 Vue 文件中引入并配置分页组件:

<template>
  <div>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      layout="prev, pager, next"
    />
  </div>
</template>

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

使用 b-pagination 组件(Bootstrap Vue)

安装 Bootstrap Vue:

npm install bootstrap-vue

在 Vue 文件中引入并配置分页组件:

分页实现vue

<template>
  <div>
    <b-pagination
      v-model="currentPage"
      :total-rows="total"
      :per-page="pageSize"
      @change="handlePageChange"
    />
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 100
    };
  },
  methods: {
    handlePageChange(page) {
      this.currentPage = page;
      this.fetchData();
    },
    fetchData() {
      // 根据 currentPage 和 pageSize 请求数据
    }
  }
};
</script>

手动实现分页逻辑

如果不依赖 UI 库,可以手动实现分页逻辑:

<template>
  <div>
    <div v-for="item in paginatedData" :key="item.id">
      {{ item.name }}
    </div>
    <div>
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>第 {{ currentPage }} 页</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      totalItems: 100,
      items: [] // 假设这是从后端获取的数据
    };
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.pageSize);
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.items.slice(start, end);
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--;
        this.fetchData();
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++;
        this.fetchData();
      }
    },
    fetchData() {
      // 根据 currentPage 和 pageSize 请求数据
    }
  }
};
</script>

使用 v-data-table 分页(Vuetify)

安装 Vuetify:

分页实现vue

npm install vuetify

在 Vue 文件中配置表格分页:

<template>
  <v-data-table
    :items="items"
    :headers="headers"
    :page.sync="currentPage"
    :items-per-page="pageSize"
    :server-items-length="total"
    @update:page="handlePageChange"
  />
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 100,
      items: [],
      headers: [
        { text: 'ID', value: 'id' },
        { text: 'Name', value: 'name' }
      ]
    };
  },
  methods: {
    handlePageChange(page) {
      this.currentPage = page;
      this.fetchData();
    },
    fetchData() {
      // 根据 currentPage 和 pageSize 请求数据
    }
  }
};
</script>

后端分页(API 请求)

通常分页需要后端配合,通过 API 传递分页参数:

<script>
export default {
  methods: {
    fetchData() {
      const params = {
        page: this.currentPage,
        pageSize: this.pageSize
      };
      axios.get('/api/data', { params })
        .then(response => {
          this.items = response.data.items;
          this.total = response.data.total;
        });
    }
  }
};
</script>

分页样式自定义

可以通过 CSS 自定义分页样式:

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
}
.pagination button:disabled {
  opacity: 0.5;
}

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

相关文章

vue页面分离的实现

vue页面分离的实现

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

vue实现滚动截屏

vue实现滚动截屏

实现滚动截屏的基本思路 滚动截屏通常指对超出可视区域的长页面进行完整截图。在Vue中,可以通过结合DOM操作、Canvas绘制及第三方库实现。以下是具体方法: 使用html2canvas库实现 安装…

vue实现双折线图

vue实现双折线图

实现双折线图的步骤 安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例: npm install echarts --save 在 Vue 组件中引入 ECh…

vue实现一个标签选择

vue实现一个标签选择

Vue 标签选择组件实现 基础实现 使用 Vue 的 v-for 和 v-model 指令可以快速实现标签选择功能: <template> <div class="tag-se…

vue实现点击

vue实现点击

实现点击事件的基本方法 在Vue中实现点击事件可以通过v-on指令或@简写方式绑定。以下是常见的点击事件绑定方法: <template> <button v-on:click=…

vue实现翻译

vue实现翻译

Vue 实现翻译功能的方法 使用 i18n 插件实现多语言切换 安装 vue-i18n 插件: npm install vue-i18n 创建语言资源文件(如 en.json 和 zh.json):…