最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue分頁(yè)查詢?cè)趺磳?shí)現(xiàn)

 更新時(shí)間:2023年04月11日 09:10:41   作者:haohulala  
這篇文章主要介紹了Vue分頁(yè)查詢?cè)趺磳?shí)現(xiàn),使用vue實(shí)現(xiàn)分頁(yè)的邏輯并不復(fù)雜,接收后端傳輸過來(lái)的數(shù)據(jù),然后根據(jù)數(shù)據(jù)的總數(shù)和每一頁(yè)的數(shù)據(jù)量就可以計(jì)算出一共可以分成幾頁(yè)

我編寫了一個(gè)簡(jiǎn)單的前端頁(yè)面用來(lái)查詢數(shù)據(jù),頁(yè)面一共有幾個(gè)邏輯

具體的效果可以看下面的演示

下面就來(lái)看一下具體的實(shí)現(xiàn)步驟。

首先看一下vue的代碼

<script type="text/javascript">
    Vue.createApp({
        data()  {
            return {
                items : [],
                // 關(guān)鍵詞
                keyword : "",
                // 是否沒有數(shù)據(jù)
                isnull : false,
                // 一開始不顯示上一頁(yè)和下一頁(yè)
                isshow : false,
                // 一共有多少條數(shù)據(jù)
                countInfo : 0,
                // 每一頁(yè)顯示幾條數(shù)據(jù)
                pageSize : 10,
                // 當(dāng)前是第幾頁(yè)
                currentPage : 1,
                // 一共有幾頁(yè)
                countAll : 1,
                code : 200
            }
        },
        methods: {
            search() {
                // 拿到待搜索的關(guān)鍵詞
                var keyword = document.getElementById("keyword").value;
                console.log(keyword);
                this.keyword = keyword;
                this.currentPage = 1;
                var url = "http://localhost:8080/csdn/search/" + keyword + "/" + this.currentPage;
                console.log(url);
                axios.get(url).then((response) => {
                    if(response.data.msg.count==0) {
                        this.isnull = true;
                        // 將原始數(shù)據(jù)置空
                        this.items = [];
                        // 不顯示上一頁(yè)下一頁(yè)按鈕
                        this.isshow = false;
                    } else {
                        this.isnull = false;
                        console.log(response)
                        this.items = response.data.msg.list;
                        this.countInfo = response.data.msg.count;
                        // 計(jì)算一共有幾頁(yè)
                        this.countAll = Math.ceil(response.data.msg.count / this.pageSize); 
                        this.isshow = true;
                    }
                })
                .catch(function (error) {
                    console.log(error);
                });
            },
            getNextPage() {
                if(this.currentPage == this.countAll) {
                    this.currentPage = this.currentPage;
                } else {
                    this.currentPage = this.currentPage + 1;
                }
                var url = "http://localhost:8080/csdn/search/" + this.keyword + "/" + this.currentPage;
                axios.get(url).then((response) => {
                    console.log(response)
                    this.items = response.data.msg.list;
                    // 計(jì)算一共有幾頁(yè)
                    this.countAll = Math.ceil(response.data.msg.count / this.pageSize); 
                })
                .catch(function (error) {
                    console.log(error);
                });
            },
            getPrePage() {
                if(this.currentPage == 1) {
                    this.currentPage = 1;
                } else {
                    this.currentPage = this.currentPage - 1;
                }
                var url = "http://localhost:8080/csdn/search/" + this.keyword + "/" + this.currentPage;
                axios.get(url).then((response) => {
                    console.log(response)
                    this.items = response.data.msg.list;
                    // 計(jì)算一共有幾頁(yè)
                    this.countAll = Math.ceil(response.data.msg.count / this.pageSize); 
                })
                .catch(function (error) {
                    console.log(error);
                });
            }
        },
    }).mount("#app");
</script>

data()中返回了幾個(gè)變量,

  • items:用來(lái)存放待展示的數(shù)據(jù)項(xiàng)
  • keyword:記錄本次查詢使用的關(guān)鍵詞
  • isnull:表示一次查詢的結(jié)果數(shù)量是否為0,用來(lái)控制沒有結(jié)果的顯示邏輯
  • isshow:表示是否顯示上一頁(yè)下一頁(yè)按鈕,以及顯示當(dāng)前頁(yè)數(shù)和數(shù)據(jù)總數(shù)
  • countInfo:記錄一共有多少條結(jié)果
  • pageSize:記錄每頁(yè)顯示的數(shù)據(jù)項(xiàng),目前后端固定每頁(yè)展示10條數(shù)據(jù)
  • currentPage:記錄當(dāng)前是第幾頁(yè)
  • countAll:記錄一共有多少頁(yè)數(shù)據(jù)
  • code:后端返回的一個(gè)狀態(tài)碼,沒什么用

一共提供了三個(gè)方法進(jìn)行查詢

  • search():進(jìn)行一個(gè)新的關(guān)鍵詞的查詢
  • getNextPage():查詢下一頁(yè)的數(shù)據(jù),如果已經(jīng)是最后一頁(yè)了,則查詢當(dāng)前頁(yè)的結(jié)果
  • getPrePage():查詢上一頁(yè)的數(shù)據(jù),如果已經(jīng)是第一頁(yè)了,則查詢當(dāng)前頁(yè)的結(jié)果

接著我們?cè)賮?lái)看一下后端返回的數(shù)據(jù)格式

上圖中方框內(nèi)的數(shù)據(jù)就是后端返回的數(shù)據(jù),msg中記錄的就是我們需要用到的數(shù)據(jù),里面有交給數(shù)據(jù)項(xiàng)

  • count:表示數(shù)據(jù)總數(shù),只是查詢數(shù)據(jù)總數(shù),并不會(huì)將所有的數(shù)據(jù)都返回給前端
  • list:返回當(dāng)前頁(yè)的數(shù)據(jù)
  • page:表示當(dāng)前是第幾頁(yè)

我們具體來(lái)看一下list中數(shù)據(jù)項(xiàng)的內(nèi)容

可以發(fā)現(xiàn)list中的每一項(xiàng)就是構(gòu)成我們前端頁(yè)面中一行的數(shù)據(jù),這在vue中體現(xiàn)為數(shù)據(jù)的綁定,下面就來(lái)看看詳細(xì)的html代碼

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>納米搜索</title>
    <link rel="stylesheet"  rel="external nofollow" >
    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <script src="https://unpkg.com/vue@3"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div class="container">
    <!-- 先編寫一個(gè)搜索欄 -->
    <div class="row" id="app">
        <div class="col-md-1"></div>
        <div class="col-md-10">
            <!-- 這里面有兩個(gè)個(gè)部分 -->
            <div class="row">
                <!--<div class="col-md-2"></div>-->
                <div class="col-md-12">
                    <div style="float: left; margin-top: 20px;margin-left: 20%">
                        <h2 style="color:cornflowerblue">納米搜索</h2>
                    </div>
                    <div style="float: left; margin-top: 20px; margin-left: 20px">
                        <div class="form-group" style="margin-right: 20px; float: left;" >
                            <div class="input-group" >
                                <input type="text" class="form-control" name="keyword"  id="keyword" placeholder="請(qǐng)輸入要搜索的關(guān)鍵詞">
                            </div>
                        </div>
                        <div style="float:left">
                            <button id="search" type="button" class="btn btn-primary" v-on:click="search">搜索</button>
                        </div>
                    </div>
                </div>
                <!--<div class="col-md-2"></div>-->
            </div>
            <hr>
            <div>
                <div v-for="item of items">
                    <!-- 第一行是url -->
                    <a :href="item.url" rel="external nofollow"  target="_blank">
                        <div style="color: #0000cc">{{item.title}}</div>
                    </a>
                    <div style="color: #28a745">{{item.url}}</div>
                    <!-- 這一行顯示文章作者 -->
                    <div style="color: #000000">文章作者:<span style="color: #000000; margin-left: 10px">{{item.nick_name}}</span></div>
                    <!-- 這一行顯示標(biāo)簽 -->
                    <div style="color: #000000">文章標(biāo)簽:<span style="color: #000000; margin-left: 10px">{{item.tag}}</span></div>
                    <!-- 下面一行顯示發(fā)表時(shí)間,閱讀數(shù)和收藏?cái)?shù) -->
                    <div>
                        <div style="color: #000000">發(fā)表時(shí)間:<span style="color: #000000;margin-left: 10px">{{item.up_time}}</span></div>
                        <div style="color: #000000;float: left">閱讀量:<span style="color: #000000;margin-left: 10px">{{item.read_volum}}</span></div>
                        <div style="color: #000000;float: left; margin-left: 10px">收藏量:<span style="color: #000000;margin-left: 10px">{{item.collection_volum}}</span></div>
                    </div>
                    <br>
                    <hr>
                </div>
            </div>
            <!-- 當(dāng)沒有查詢結(jié)果的時(shí)候顯示 -->
            <div v-if="isnull">
                <span>非常抱歉,沒有您想要的結(jié)果(。?_?。)?I'm sorry~</span>
            </div>
            <!-- 當(dāng)有數(shù)據(jù)的時(shí)候顯示 -->
            <div v-if="isshow">
                <div style="float:left; margin-right: 20px;" >
                    <button type="button" class="btn btn-primary" v-on:click="getPrePage">上一頁(yè)</button>
                </div>
                <div style="float:left; margin-right: 20px;" >
                    <button type="button" class="btn btn-primary" v-on:click="getNextPage" >下一頁(yè)</button>
                </div>
                <div style="float:left; margin-right: 20px; margin-top: 5px;">
                    <span>第{{currentPage}}/{{countAll}}頁(yè)</spa>
                </div>
                <div style="float:left; margin-right: 20px; margin-top: 5px;">
                    <span>共有{{countInfo}}條數(shù)據(jù)</spa>
                </div>
            </div>
        </div>
        <div class="col-md-1"></div>
    </div>
</div>
</body>
<script type="text/javascript">
    Vue.createApp({
        data()  {
            return {
                items : [],
                // 關(guān)鍵詞
                keyword : "",
                // 是否沒有數(shù)據(jù)
                isnull : false,
                // 一開始不顯示上一頁(yè)和下一頁(yè)
                isshow : false,
                // 一共有多少條數(shù)據(jù)
                countInfo : 0,
                // 每一頁(yè)顯示幾條數(shù)據(jù)
                pageSize : 10,
                // 當(dāng)前是第幾頁(yè)
                currentPage : 1,
                // 一共有幾頁(yè)
                countAll : 1,
                code : 200
            }
        },
        methods: {
            search() {
                // 拿到待搜索的關(guān)鍵詞
                var keyword = document.getElementById("keyword").value;
                console.log(keyword);
                this.keyword = keyword;
                this.currentPage = 1;
                var url = "http://localhost:8080/csdn/search/" + keyword + "/" + this.currentPage;
                console.log(url);
                axios.get(url).then((response) => {
                    if(response.data.msg.count==0) {
                        this.isnull = true;
                        // 將原始數(shù)據(jù)置空
                        this.items = [];
                        // 不顯示上一頁(yè)下一頁(yè)按鈕
                        this.isshow = false;
                    } else {
                        this.isnull = false;
                        console.log(response)
                        this.items = response.data.msg.list;
                        this.countInfo = response.data.msg.count;
                        // 計(jì)算一共有幾頁(yè)
                        this.countAll = Math.ceil(response.data.msg.count / this.pageSize); 
                        this.isshow = true;
                    }
                })
                .catch(function (error) {
                    console.log(error);
                });
            },
            getNextPage() {
                if(this.currentPage == this.countAll) {
                    this.currentPage = this.currentPage;
                } else {
                    this.currentPage = this.currentPage + 1;
                }
                var url = "http://localhost:8080/csdn/search/" + this.keyword + "/" + this.currentPage;
                axios.get(url).then((response) => {
                    console.log(response)
                    this.items = response.data.msg.list;
                    // 計(jì)算一共有幾頁(yè)
                    this.countAll = Math.ceil(response.data.msg.count / this.pageSize); 
                })
                .catch(function (error) {
                    console.log(error);
                });
            },
            getPrePage() {
                if(this.currentPage == 1) {
                    this.currentPage = 1;
                } else {
                    this.currentPage = this.currentPage - 1;
                }
                var url = "http://localhost:8080/csdn/search/" + this.keyword + "/" + this.currentPage;
                axios.get(url).then((response) => {
                    console.log(response)
                    this.items = response.data.msg.list;
                    // 計(jì)算一共有幾頁(yè)
                    this.countAll = Math.ceil(response.data.msg.count / this.pageSize); 
                })
                .catch(function (error) {
                    console.log(error);
                });
            }
        },
    }).mount("#app");
</script>
</html>

使用vue編寫前端動(dòng)態(tài)頁(yè)面真的比原生js或者jquery要方便很多,對(duì)比theamleaf也有很多好處。

我們?cè)谑褂胻heamleaf的時(shí)候,每次提交表單都需要刷新頁(yè)面,使用vue+axios進(jìn)行ajax請(qǐng)求則不需要刷新頁(yè)面,這不僅會(huì)減輕服務(wù)端的壓力,而且可以帶來(lái)更好的用戶體驗(yàn)。

到此這篇關(guān)于Vue分頁(yè)查詢?cè)趺磳?shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Vue分頁(yè)查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue使用pdfjs顯示PDF可復(fù)制的實(shí)現(xiàn)方法

    vue使用pdfjs顯示PDF可復(fù)制的實(shí)現(xiàn)方法

    這篇文章主要介紹了vue使用pdfjs顯示PDF可復(fù)制的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2018-12-12
  • Vue標(biāo)尺插件使用詳解

    Vue標(biāo)尺插件使用詳解

    這篇文章主要為大家詳細(xì)介紹了Vue標(biāo)尺插件的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Vue3時(shí)間軸組件問題記錄(時(shí)間信息收集組件)

    Vue3時(shí)間軸組件問題記錄(時(shí)間信息收集組件)

    本文介紹了如何在Vue3項(xiàng)目中封裝一個(gè)時(shí)間信息收集組件,采用雙向綁定響應(yīng)式數(shù)據(jù),通過對(duì)Element-Plus的Slider組件二次封裝,實(shí)現(xiàn)時(shí)間軸功能,解決了小數(shù)計(jì)算導(dǎo)致匹配問題和v-model綁定組件無(wú)效問題,感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • Vue2.0 從零開始_環(huán)境搭建操作步驟

    Vue2.0 從零開始_環(huán)境搭建操作步驟

    下面小編就為大家?guī)?lái)一篇Vue2.0 從零開始_環(huán)境搭建操作步驟。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2017-06-06
  • vue3超出文本展示el tooltip實(shí)現(xiàn)示例

    vue3超出文本展示el tooltip實(shí)現(xiàn)示例

    這篇文章主要為大家介紹了vue3超出文本展示el tooltip實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • vue3 el-form-item如何自定義label標(biāo)簽內(nèi)容

    vue3 el-form-item如何自定義label標(biāo)簽內(nèi)容

    這篇文章主要介紹了vue3 el-form-item如何自定義label標(biāo)簽內(nèi)容問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue不用window的方式如何刷新當(dāng)前頁(yè)面

    vue不用window的方式如何刷新當(dāng)前頁(yè)面

    這篇文章主要介紹了vue不用window的方式如何刷新當(dāng)前頁(yè)面,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Vue3.0中的monorepo管理模式的實(shí)現(xiàn)

    Vue3.0中的monorepo管理模式的實(shí)現(xiàn)

    這篇文章主要介紹了Vue3.0中的monorepo管理模式的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • vue中node_modules中第三方模塊的修改使用詳解

    vue中node_modules中第三方模塊的修改使用詳解

    這篇文章主要介紹了vue中node_modules中第三方模塊的修改使用,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05
  • vue項(xiàng)目使用cmd運(yùn)行方式

    vue項(xiàng)目使用cmd運(yùn)行方式

    在開發(fā)過程中,啟動(dòng)項(xiàng)目是必不可少的一步,本文介紹了兩種啟動(dòng)項(xiàng)目的方法,第一種方法是通過命令提示符(cmd)進(jìn)入項(xiàng)目目錄,然后輸入npm run serve來(lái)啟動(dòng)項(xiàng)目,第二種方法是直接在項(xiàng)目目錄下打開命令提示符(cmd),輸入npm run serve即可啟動(dòng)項(xiàng)目
    2024-10-10

最新評(píng)論

扎囊县| 剑河县| 黄平县| 东港市| 虹口区| 内江市| 丰县| 鄂托克前旗| 博白县| 内乡县| 淅川县| 南溪县| 从化市| 蓬安县| 揭阳市| 汤阴县| 海安县| 阳春市| 定兴县| 泰兴市| 宁安市| 攀枝花市| 宽城| 疏勒县| 台湾省| 烟台市| 乌鲁木齐市| 巴林右旗| 平湖市| 沙田区| 五大连池市| 江城| 黑河市| 阜康市| 台北市| 会昌县| 乌拉特后旗| 高青县| 乐安县| 宁城县| 类乌齐县|