欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue+ElementUI?實(shí)現(xiàn)分頁功能-mysql數(shù)據(jù)

 更新時間:2022年01月25日 10:52:23   作者:小綿楊Yancy  
這篇文章主要介紹了Vue+ElementUI?實(shí)現(xiàn)分頁查詢-mysql數(shù)據(jù),當(dāng)數(shù)據(jù)庫中數(shù)據(jù)比較多時,就每次只查詢一部分來緩解服務(wù)器和頁面壓力。這里使用elementui的?Pagination?分頁?組件,配合mysql的limit語句,實(shí)現(xiàn)分頁查詢mysql數(shù)據(jù),下面來看看具體實(shí)現(xiàn)過程,希望對大家學(xué)習(xí)有所幫助

1.問題

當(dāng)數(shù)據(jù)庫中數(shù)據(jù)比較多時,就要每次只查詢一部分來緩解服務(wù)器和頁面的壓力。這里使用elementuiPagination 分頁 組件,配合mysqllimit語句,實(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 );
?>

這里使用了mysqllimit實(shí)現(xiàn)一次只查詢一部分?jǐn)?shù)據(jù),前端傳來了參數(shù)offsetlimit。

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)擊第二頁時:觸發(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.結(jié)果

注意:你的limit參數(shù)一定要和Paginationpage-size屬性一致,也就時一次查詢一頁數(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)文章

最新評論