js如何使用Pagination+PageHelper實(shí)現(xiàn)分頁(yè)
一、分頁(yè)的原理:
1.1 分頁(yè)的原理
通過(guò)element-ui 的內(nèi)置組件pagination實(shí)現(xiàn)分頁(yè),任何分頁(yè)都有以下五個(gè)部分組成:
- 記錄的總條數(shù)
- 每頁(yè)顯示的記錄條數(shù)
- 總頁(yè)數(shù)
- 當(dāng)前是第幾頁(yè)
- 當(dāng)前頁(yè)的所有記錄
1.2 真假分頁(yè)
pagination實(shí)際上是一個(gè)組件,組件里設(shè)置了分頁(yè)常用到的參數(shù),讓pagination組件得到分頁(yè)常用的參數(shù)值,這就能夠?qū)崿F(xiàn)分頁(yè)了。
真分頁(yè):當(dāng)你目前在首頁(yè)的時(shí)候,點(diǎn)擊“第二頁(yè)”或“下一頁(yè)”的時(shí)候,會(huì)重新向后端發(fā)送請(qǐng)求,請(qǐng)求第二頁(yè)的數(shù)據(jù)
假分頁(yè):一開(kāi)始從后端發(fā)送請(qǐng)求獲取所有的數(shù)據(jù),前端通過(guò)在組件的方式對(duì)數(shù)據(jù)進(jìn)行分頁(yè),再點(diǎn)擊分頁(yè)的按鈕的時(shí)候,數(shù)據(jù)其實(shí)已經(jīng)在瀏覽器緩存的緩存中了,不需要再請(qǐng)求后端接口
二、后端-PageHelper的使用:
1、首先要在pom.xml中添加pageHelper的依賴(lài)
<!--分頁(yè)插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>2、在映射文件中書(shū)寫(xiě)“SQL查詢(xún)”語(yǔ)句;注意:語(yǔ)句結(jié)束不要用“;”
<select id="QueryProductsById" resultMap="ProductsMap">
SELECT
<include refid="products_cloumn_list"/>
FROM products WHERE id = #{Id}
</select>3、書(shū)寫(xiě)Controller類(lèi),注意:調(diào)用PageHelper的startPage方法一定要在調(diào)用接口中方法前。
@RequestMapping("/PageInfo")
public PageInfo<Products> pageInfo(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<Products> list = productsDaoService.QueryProducts();
PageInfo<Products> pageInfo = new PageInfo<Products>(list);
return pageInfo;
}4、啟動(dòng)tomcat服務(wù)器,使用Apipost對(duì)接口進(jìn)行測(cè)試,如果接口沒(méi)有問(wèn)題的話,就會(huì)在“實(shí)時(shí)響應(yīng)”中獲取到返回值信息。

三、前端-Pagination的使用:
(使用pagination之前,需要會(huì)element-UI有初步的了解),因?yàn)槭褂胮agination就是一個(gè)從vue-element-admin上“搬運(yùn)”代碼的過(guò)程。具體可以在element集成上搜索“pagination”進(jìn)行查看

1、添加<template>標(biāo)簽的內(nèi)容到需要分頁(yè)的頁(yè)面中
<pagination
:total="total"
:page.sync="listQuery.page"
:limit.sync="listQuery.limit"
@pagination="getList" />2、根據(jù)element集成中,在<script>中導(dǎo)入Pagination組件
import Pagination from '@/components/Pagination'
pagination組件中index.vue的內(nèi)容如下:
<template>
<div :class="{'hidden':hidden}" class="pagination-container">
<el-pagination
:background="background"
:current-page.sync="currentPage"
:page-size.sync="pageSize"
:layout="layout"
:page-sizes="pageSizes"
:total="total"
v-bind="$attrs"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</div>
</template>
<script>
import { scrollTo } from '@/utils/scroll-to'
export default {
name: 'Pagination',
props: {
total: {
required: true,
type: Number
},
page: {
type: Number,
default: 1
},
limit: {
type: Number,
default: 20
},
pageSizes: {
type: Array,
default() {
return [10, 20, 30, 50]
}
},
layout: {
type: String,
default: 'total, sizes, prev, pager, next, jumper'
},
background: {
type: Boolean,
default: true
},
autoScroll: {
type: Boolean,
default: true
},
hidden: {
type: Boolean,
default: false
}
},
computed: {
currentPage: {
get() {
return this.page
},
set(val) {
this.$emit('update:page', val)
}
},
pageSize: {
get() {
return this.limit
},
set(val) {
this.$emit('update:limit', val)
}
}
},
methods: {
handleSizeChange(val) {
this.$emit('pagination', { page: this.currentPage, limit: val })
if (this.autoScroll) {
scrollTo(0, 800)
}
},
handleCurrentChange(val) {
this.$emit('pagination', { page: val, limit: this.pageSize })
if (this.autoScroll) {
scrollTo(0, 800)
}
}
}
}
</script>
<style scoped>
.pagination-container {
background: #fff;
padding: 32px 16px;
}
.pagination-container.hidden {
display: none;
}
</style>3、注冊(cè)本地組件,并且因?yàn)樵谔砑?lt;template>標(biāo)簽的時(shí)候,綁定的有屬性和方法,所以要對(duì)屬性進(jìn)行聲明,以及方法的實(shí)現(xiàn)
export default {
components: { Pagination },
data() {
return {
list: [{
//查詢(xún)出來(lái)的商品集合
}],
total: 0,
listQuery: {
page: 1,
limit: 20
}
}
},
methods: {
getList() {
// 獲取數(shù)據(jù)
}
}
}4、實(shí)現(xiàn) getList() 方法,發(fā)送axios請(qǐng)求獲取后端傳遞的數(shù)據(jù),分別將返回的總條數(shù)和數(shù)據(jù)信息分貝賦給本地的total、list集合
getList() {
// 獲取數(shù)據(jù)
var vm = this;
this.axios({
method: 'get',
url: 'http://localhost:8080/ssm-template/products/PageInfo?pageNum='+vm.listQuery.page+'&pageSize='+vm.listQuery.limit
})
.then(function (response) {
vm.total = response.data.total;
vm.list = response.data.list;
})
},5、使用 created()方法,讓頁(yè)面加載時(shí)候調(diào)用 getList()方法,實(shí)現(xiàn)分頁(yè)即可 :
created() { this.getList() },效果圖如下:

四、分頁(yè)中的細(xì)節(jié):
分頁(yè)中可以在進(jìn)行更為詳細(xì)的設(shè)置,比如背景色、當(dāng)前頁(yè)、總頁(yè)數(shù)、去往第幾頁(yè)等等都可以在pagination的index.vue中進(jìn)行設(shè)置
<template>
<div :class="{'hidden':hidden}" class="pagination-container">
<el-pagination
:background="background"http://背景色 true 為有背景色,false為無(wú)背景色
:current-page.sync="currentPage" //當(dāng)前頁(yè)
:page-size.sync="pageSize" //頁(yè)面的大小
:layout="layout"
:page-sizes="pageSizes"
:total="total" //總頁(yè)數(shù)
v-bind="$attrs"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</div>
</template>
<script>
import { scrollTo } from '@/utils/scroll-to'
export default {
name: 'Pagination',
props: {
total: {
required: true,
type: Number
},
page: {
type: Number,
default: 1
},
limit: {
type: Number,
default: 20
},
pageSizes: {
type: Array,
default() {
return [10, 20, 30, 50]
}
},
layout: {
type: String,
default: 'total, sizes, prev, pager, next, jumper'
},
background: {
type: Boolean,
default: true
},
autoScroll: {
type: Boolean,
default: true
},
hidden: {
type: Boolean,
default: false
}
},
computed: {
currentPage: {
get() {
return this.page
},
set(val) {
this.$emit('update:page', val)
}
},
pageSize: {
get() {
return this.limit
},
set(val) {
this.$emit('update:limit', val)
}
}
},
methods: {
handleSizeChange(val) {
this.$emit('pagination', { page: this.currentPage, limit: val })
if (this.autoScroll) {
scrollTo(0, 800)
}
},
handleCurrentChange(val) {
this.$emit('pagination', { page: val, limit: this.pageSize })
if (this.autoScroll) {
scrollTo(0, 800)
}
}
}
}
</script>
<style scoped>
.pagination-container {
background: #fff;
padding: 32px 16px;
}
.pagination-container.hidden {
display: none;
}
</style>可以進(jìn)行適當(dāng)?shù)男薷?,或者如果不想要某些功能,刪除對(duì)應(yīng)的部分即可~~~
到此這篇關(guān)于js如何使用Pagination+PageHelper實(shí)現(xiàn)分頁(yè)的文章就介紹到這了,更多相關(guān)js Pagination PageHelper分頁(yè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Mybatis分頁(yè)插件PageHelper的使用詳解
- SpringBoot項(xiàng)目中分頁(yè)插件PageHelper無(wú)效的問(wèn)題及解決方法
- SpringMvc+Mybatis+Pagehelper分頁(yè)詳解
- Spring Boot+Mybatis+Druid+PageHelper實(shí)現(xiàn)多數(shù)據(jù)源并分頁(yè)的方法
- Springboot整合pagehelper分頁(yè)功能
- 使用mybatis插件PageHelper實(shí)現(xiàn)分頁(yè)效果
- MyBatis分頁(yè)插件PageHelper的具體使用
- MyBatis基于pagehelper實(shí)現(xiàn)分頁(yè)原理及代碼實(shí)例
- Bootstrap Paginator+PageHelper實(shí)現(xiàn)分頁(yè)效果
- Spring Boot+Mybatis+Pagehelper分頁(yè)實(shí)現(xiàn)
相關(guān)文章
el-form實(shí)現(xiàn)表單和圖片手動(dòng)上傳和校驗(yàn)功能
在寫(xiě)項(xiàng)目時(shí),難免遇到需要上傳表單,圖片等文件,且表單內(nèi)容需進(jìn)行驗(yàn)證及必填項(xiàng)提示,圖片需要和信息一起傳遞且圖片載入后需可預(yù)覽,這篇文章給大家介紹el-form實(shí)現(xiàn)表單和圖片手動(dòng)上傳和校驗(yàn)功能,感興趣的朋友一起看看吧2024-01-01
javascript中html字符串轉(zhuǎn)化為jquery dom對(duì)象的方法
最近項(xiàng)目需求要開(kāi)發(fā)百度地圖相關(guān)的一個(gè)應(yīng)用,需要從硬編碼的html字符串中提取自己想要的元素以及屬性信息,由于在js中或者jq中操作元素節(jié)點(diǎn)以及屬性都是使用dom對(duì)象或者jq對(duì)象。下面介紹javascript中html字符串轉(zhuǎn)化為jquery dom對(duì)象的方法,需要的朋友可以參考下2015-08-08
JS中console對(duì)象內(nèi)部提供調(diào)試方法示例詳解
本文介紹了JavaScript中`console`對(duì)象提供的多種調(diào)試方法,包括`log`、`debug`、`dir`、`table`、`clear`、`group`、`groupEnd`、`time`和`timeEnd`,每種方法都有其特定的用途,感興趣的朋友跟隨小編一起看看吧2025-02-02
js高精度計(jì)算decimal.js庫(kù)用法demo
這篇文章主要給大家介紹了關(guān)于js高精度計(jì)算decimal.js庫(kù)用法的相關(guān)資料,decimal.js是使用的二進(jìn)制來(lái)計(jì)算的,所以能解決js的精度問(wèn)題,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
Google Map Api和GOOGLE Search Api整合實(shí)現(xiàn)代碼
將GOOGLE MAP API 和 GOOGLE Search API 進(jìn)行整合,我用面向?qū)ο蟮姆绞綄?xiě)了一個(gè)類(lèi),通過(guò)傳一個(gè)經(jīng)緯度進(jìn)去,自動(dòng)通過(guò)GOOGLE LOCAL SEARCH獲取附近的相關(guān)信息。比如餐廳、景點(diǎn)等,反過(guò)來(lái)標(biāo)到地圖上,并可在任意容器內(nèi)顯示。2009-07-07
js實(shí)現(xiàn)網(wǎng)頁(yè)同時(shí)進(jìn)行多個(gè)倒計(jì)時(shí)功能
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)網(wǎng)頁(yè)同時(shí)進(jìn)行多個(gè)倒計(jì)時(shí)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
一個(gè)無(wú)限級(jí)XML綁定跨框架菜單(For IE)
一個(gè)無(wú)限級(jí)XML綁定跨框架菜單(For IE)...2007-01-01
JS實(shí)現(xiàn)Date日期格式的本地化的方法小結(jié)
為了更好的更新多語(yǔ)言日期的顯示,所以希望實(shí)現(xiàn)日期的本地化格式顯示要求,常規(guī)的特殊字符型格式化無(wú)法滿足顯示要求,這里整理了幾種我思考實(shí)現(xiàn)的本地化實(shí)現(xiàn)功能2024-06-06
javascript實(shí)現(xiàn)數(shù)字時(shí)鐘效果
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)數(shù)字時(shí)鐘效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02

