Vue+ElementUI?實現(xiàn)分頁功能-mysql數(shù)據(jù)
1.問題
當數(shù)據(jù)庫中數(shù)據(jù)比較多時,就要每次只查詢一部分來緩解服務器和頁面的壓力。這里使用elementui
的 Pagination 分頁 組件,配合mysql
的limit
語句,實現(xiàn)分頁查詢mysql數(shù)據(jù)。
下圖是最基本的分頁樣式:
當然需要引入對應的事件,來實現(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
也就是當前頁數(shù)為第一頁。
2.2獲取數(shù)據(jù)庫數(shù)據(jù)的函數(shù):getData():
參數(shù)為offset
,limit
,向后端請求數(shù)據(jù),待會兒解釋。這里使用了qs序列化參數(shù)??梢詤⒖嘉业牧硪黄┛停?code>Vue + ElementUI + Viewer翻頁后圖片無法預覽 Vue父子組件異步通信問題 里面解釋了qs的功能。
getData(offset,limit){ this.axios.post('/php/select.php', qs.stringify({ offset: offset, limit: limit, type: '失物招領' }), { 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ù),即點擊了翻頁,其中val參數(shù)就是當前頁數(shù),使用新的參數(shù),
調用getData實現(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ù) 開始和結束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
實現(xià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ù)。
當我點擊第二頁時:觸發(fā)handleCurrentChange()
函數(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ù)。
同時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.結果
注意:你的limit
參數(shù)一定要和Pagination
的page-size
屬性一致,也就時一次查詢一頁數(shù)據(jù)。而offset
就是當前的頁數(shù)。
到此這篇關于Vue+ElementUI 實現(xiàn)分頁查詢-mysql數(shù)據(jù)的文章就介紹到這了,更多相關Vue+ElementUI 實現(xiàn)分頁查詢內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue.js中的計算屬性、監(jiān)視屬性與生命周期詳解
最近在學習vue,學習中遇到了一些感覺挺重要的知識點,感覺有必要整理下來,這篇文章主要給大家介紹了關于Vue.js中計算屬性、監(jiān)視屬性與生命周期的相關資料,需要的朋友可以參考下2021-06-06微信小程序地圖導航功能實現(xiàn)完整源代碼附效果圖(推薦)
這篇文章主要介紹了微信小程序地圖導航功能實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04關于elementUi表格合并行數(shù)據(jù)并展示序號
這篇文章主要介紹了關于elementUi表格合并行數(shù)據(jù)并展示序號,通過給table傳入span-method方法可以實現(xiàn)合并行或列,方法的參數(shù)是一個對象,感興趣的朋友可以學習一下2023-04-04vue+elementUI動態(tài)增加表單項并添加驗證的代碼詳解
這篇文章主要介紹了vue+elementUI動態(tài)增加表單項并添加驗證的代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12