Vue+ElementUI?實(shí)現(xiàn)分頁功能-mysql數(shù)據(jù)
1.問題
當(dāng)數(shù)據(jù)庫中數(shù)據(jù)比較多時(shí),就要每次只查詢一部分來緩解服務(wù)器和頁面的壓力。這里使用elementui的 Pagination 分頁 組件,配合mysql的limit語句,實(shí)現(xiàn)分頁查詢mysql數(shù)據(jù)。
下圖是最基本的分頁樣式:

當(dāng)然需要引入對應(yīng)的事件,來實(shí)現(xiàn)頁面改變就查詢數(shù)據(jù)庫。

2.解決
2.1分頁組件
<el-pagination
background
layout="prev, pager, next"
:page-size="8"
:total="total"
:current-page="pageNum"
@current-change="handleCurrentChange">
</el-pagination>
data:初始化總數(shù)據(jù)條數(shù)(total)為1,pageNum也就是當(dāng)前頁數(shù)為第一頁。

2.2獲取數(shù)據(jù)庫數(shù)據(jù)的函數(shù):getData():
參數(shù)為offset,limit,向后端請求數(shù)據(jù),待會兒解釋。這里使用了qs序列化參數(shù)。可以參考我的另一篇博客:Vue + ElementUI + Viewer翻頁后圖片無法預(yù)覽 Vue父子組件異步通信問題 里面解釋了qs的功能。
getData(offset,limit){
this.axios.post('/php/select.php', qs.stringify({
offset: offset,
limit: limit,
type: '失物招領(lǐng)'
}), { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).then((res) => {
if(res.data === 0){
this.total = 0;
this.list = [];
return;
}
this.total = res.data.total
this.list = res.data.data
this.loading = false
}).catch((err) => {
this.$message.error(err)
})
}
2.3頁面加載完成,需要請求第一頁的數(shù)據(jù)
created () {
this.getData(0,8);
},
頁面改變觸發(fā)handleCurrentChange()函數(shù),即點(diǎn)擊了翻頁,其中val參數(shù)就是當(dāng)前頁數(shù),使用新的參數(shù),
調(diào)用getData實(shí)現(xiàn)查詢不同頁面的數(shù)據(jù):
handleCurrentChange(val){
this.list = [] //清空上一頁數(shù)據(jù)
this.getData((val-1)*8,8);
}
下面是后端數(shù)據(jù):php + mysql
現(xiàn)在數(shù)據(jù)表中總共有10條數(shù)據(jù):

前端getData請求的select.php文件
select.php:
<?php
$servername = "localhost";
$username = "用戶名";
$password = "密碼";
$dbname = "數(shù)據(jù)庫名稱";
// 創(chuàng)建連接
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("連接失敗: " . $conn->connect_error);
}
$type = $_POST['type'];
//獲取前端的參數(shù) 開始和結(jié)束number
if ( !isset( $_POST['offset'] ) ) {
echo 0;
exit();
};
$offset = ( int )$_POST['offset'];
if ( !isset( $_POST['limit'] ) ) {
echo 0;
exit();
};
$limit = ( int )$_POST['limit'];
//分頁查詢數(shù)據(jù)庫
$sql = "SELECT * FROM posts where type='$type' order by id desc LIMIT $limit OFFSET $offset";
$result = $conn->query($sql);
$sqlGetCount = "SELECT COUNT(*) cnt FROM posts where type='$type'";
$rescnt = $conn->query($sqlGetCount);
$rescnt = $rescnt->fetch_assoc();
$arr = array();
if ($result->num_rows > 0) {
while ( $row = $result->fetch_assoc() ) {
array_push( $arr, $row );
}
//echo json_encode( $arr, JSON_UNESCAPED_UNICODE );
echo json_encode(array_merge(array('data'=>$arr),array('total'=>(int)$rescnt['cnt'])));
} else {
echo 0;
}
mysqli_close( $conn );
?>
這里使用了mysql的limit實(shí)現(xiàn)一次只查詢一部分?jǐn)?shù)據(jù),前端傳來了參數(shù)offset和limit。
sql語句:
"SELECT * FROM posts where type='$type' order by id desc LIMIT $limit OFFSET $offset"
3.分析
這里的 LIMIT $limit OFFSET $offset的意思就是從 $offest的值開始,查詢 $limit條數(shù)據(jù)。
例如 $limit = 8, $offest = 0:表示查詢數(shù)據(jù)庫的前8條數(shù)據(jù),從0開始(不包含0,mysql索引從0開始),查詢8條,也就是1~8條數(shù)據(jù)。
當(dāng)我點(diǎn)擊第二頁時(shí):觸發(fā)handleCurrentChange()函數(shù):

此時(shí)參數(shù)val=2,則offest = 8, limit = 8。
就會查詢第9~17條數(shù)據(jù),如果沒有17條數(shù)據(jù),也會返回查詢到9條后的所有數(shù)據(jù)。例如目前我數(shù)據(jù)庫就10條數(shù)據(jù),那么返回第9條和第10條兩條數(shù)據(jù)。
同時(shí)select.php中頁返回了總數(shù)據(jù)條數(shù)total:
SELECT COUNT(*) cnt FROM posts where type='$type'

前端頁面獲取到total值后賦值給this.total(綁定了Pagination的total屬性,也就是總數(shù)據(jù)條數(shù))。Pagination根據(jù):page-size="8"屬性就會將數(shù)據(jù)自動分頁。例如后端返回的total為10,則分成兩頁。

4.結(jié)果

注意:你的limit參數(shù)一定要和Pagination的page-size屬性一致,也就時(shí)一次查詢一頁數(shù)據(jù)。而offset就是當(dāng)前的頁數(shù)。
到此這篇關(guān)于Vue+ElementUI 實(shí)現(xiàn)分頁查詢-mysql數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Vue+ElementUI 實(shí)現(xiàn)分頁查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3源碼分析reactivity實(shí)現(xiàn)方法示例
這篇文章主要為大家介紹了Vue3源碼分析reactivity實(shí)現(xiàn)方法原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
vue中使用moment設(shè)置倒計(jì)時(shí)的方法
這篇文章給大家介紹了vue中使用moment設(shè)置倒計(jì)時(shí)的方法,文中通過代碼示例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-02-02
Django Vue實(shí)現(xiàn)動態(tài)菜單和動態(tài)權(quán)限
本文主要介紹了Django Vue實(shí)現(xiàn)動態(tài)菜單和動態(tài)權(quán)限,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
vite項(xiàng)目無法使用zangodb包裝器的解決方案
vite作為新一代工具鏈,具有很多便利之處,配置也非常簡單,它很好地整合了Rollup和其他復(fù)雜的構(gòu)建項(xiàng),并提供了多種方向的典型腳手架模板,深受大家喜愛,本文給大家介紹了如何解決vite項(xiàng)目無法使用zangodb包裝器的問題,需要的朋友可以參考下2023-10-10
基于Vue3+UniApp在單個(gè)頁面實(shí)現(xiàn)固定TabBar的多種方式
tabBar 是移動端應(yīng)用常見的頁面效果,用于實(shí)現(xiàn)多頁面的快速切換,本文給大家介紹了如何基于Vue3+UniApp在單個(gè)頁面實(shí)現(xiàn)固定TabBar的多種方式,需要的朋友可以參考下2025-03-03

