JS組件系列之MVVM組件 vue 30分鐘搞定前端增刪改查
正文
前言:關(guān)于Vue框架,好幾個月之前就聽說過,了解一項新技術(shù)之后,總是處于觀望狀態(tài),一直在猶豫要不要系統(tǒng)學習下。正好最近有點空,就去官網(wǎng)了解了下,看上去還不錯的一個組件,就抽空研究了下。最近園子里vue也確實挺火,各種入門博文眼花繚亂,博主也不敢說寫得多好,就當是個學習筆記,有興趣的可以看看。
一、MVVM比拼
關(guān)于MVVM,原來在介紹knockout.js的時候有過講解,目前市面上比較火的MVVM框架也是一抓一大把,比如常見的有Knockout.js、Vue.js、AvalonJS、Angularjs等,每一款都有它們自己的優(yōu)勢。
- Knockout:微軟出品,可以說是MVVM的模型領域內(nèi)的先驅(qū),使用函數(shù)偷龍轉(zhuǎn)鳳,最短編輯長度算法實現(xiàn)DOM的同步,兼容IE6,實現(xiàn)高超,但源碼極其難讀,最近幾年發(fā)展緩慢。
- Vue:是最近幾年出來的一個開源Javascript框架,語法精簡,實現(xiàn)精致,但對瀏覽器的支持受限,最低只能支持IE9。
- AvalonJS:是一個簡單易用迷你的MVVM框架,由大神司徒正美研發(fā)。使用簡單,實現(xiàn)明快。
- React:React并不屬于MVVM架構(gòu),但是它帶來virtual dom的概念,受限于視圖的規(guī)模。
- Angularjs:Google出品,已經(jīng)被用于Google的多款產(chǎn)品當中。AngularJS有著諸多特性,最為核心的是:MVC、模塊化、自動化雙向數(shù)據(jù)綁定、語義化標簽、依賴注入等等。入門容易上手難,大量避不開的概念也是很頭疼的。
- 更多MVVM框架優(yōu)缺點比較,可以看下 這里 。
其實在博主的博文里面,說得最多的還是那句:任何技術(shù)和框架都有它存在的價值和意義!由上也可以看出沒有哪個框架是真正完美的,關(guān)鍵看你如何取舍,在你的項目中用好了以上任何一種框架,你就是技術(shù)大牛。不過話雖這樣說,博主覺得多了解一些框架對我們并無壞處,至少能開闊我們的眼界吧。
二、Vue常用網(wǎng)址
上文說了,Vue是一個開源框架,最新版本已經(jīng)更新到了2.0,是一個Javascript框架,不依賴于任何其他框架(例如jquery),下面是博主收集的一些常用網(wǎng)址。
Vue.js開源地址:https://github.com/vuejs/vue
Vue.js英文api地址:http://vuejs.org/v2/api/
后來博主又找到一個中文的api地址,感謝開源社區(qū)工作者的翻譯。https://vuefe.cn/api/
還有一個博主覺得很方便的就是一個Vue的在線測試網(wǎng)址:https://jsfiddle.net/chrisvfritz/50wL7mdz/。里面鍵入相應的html+js+css可以直接運行查看效果。
三、Vue基礎入門
1、MVVM圖例
說到MVVM,先來看看下面下面這張圖
這張圖足以說明MVVM的核心功能,在這三者里面,ViewModel無疑起著重要的橋梁作用。
- 一方面,通過ViewModel將Model的數(shù)據(jù)綁定到View的Dom元素上面,當Model里面的數(shù)據(jù)發(fā)生變化的時候,通過ViewModel里面數(shù)據(jù)綁定的機制,觸發(fā)View里面Dom元素的變化;
- 另一方面,又通過ViewModel來View里面的Dom元素的數(shù)據(jù)變化,當頁面上面的Dom元素發(fā)生變化的時候,
ViewModel通過Dom樹的機制,觸發(fā)對應的Model的數(shù)據(jù)變化。
當然在Vue.js里面ViewModel也是核心部件,它就是一個Vue實例。這個實例作用于單個或者多個html元素,從而實現(xiàn)Dom樹和數(shù)據(jù)綁定的雙向更新操作。
2、第一個Vue實例
關(guān)于第一個實例,無疑是最簡單的應用。要使用vue,不用多說,肯定是先去github上面下載源碼嘍,然后引入到我們的項目中來,需要引用的js就一個vue.js,版本是2.0.5。
先來看一個最簡單的例子:
<body> <div id="app"> <h1>姓名:{{ Name }}</h1> <h1>年齡:{{ Age }}</h1> <h1>學校:{{ School }}</h1> </div> <script src="Content/vue/dist/vue.js"></script> <script type="text/javascript"> //Model var data = { Name: '小明', Age: 18, School:'光明小學', } //ViewModel var vue = new Vue({ el: '#app', data: data, }); </script> </body>
這段代碼不難理解,我們的Model就是data變量,而ViewModel就是這里的new Vue()得到的對象。這里兩個最簡單的屬性相信大家一看就能明白。
- el:表示綁定的Dom元素,此例子中表示的是父級的Dom元素。
- data:需要綁定的數(shù)據(jù)Model。
如果僅僅是展示,只需要 姓名:{{ Name }} 這樣寫就好了。運行的效果如下:
值得一提的是 {{ Name }} 這種寫法僅僅只能實現(xiàn)單向綁定,只有在Model里面數(shù)據(jù)發(fā)生變化的時候會觸發(fā)界面Dom元素的變化,反之并不能觸發(fā)Model數(shù)據(jù)的變化。可以通過瀏覽器的Console來驗證這一理論。
那么,對于雙向綁定的機制,Vue是如何實現(xiàn)的呢?
3、雙向綁定
vue里面提供了v-model指令,為我們方便實現(xiàn)Model和View的雙向綁定,使用也非常簡單。還是上文的例子,我們加入一個文本框,里面使用v-model指令。
<body> <div id="app"> <h1>編輯姓名:<input type="text" v-model="Name" /></h1> <h1>姓名:{{ Name }}</h1> <h1>年齡:{{ Age }}</h1> <h1>學校:{{ School }}</h1> </div> <script src="Content/vue/dist/vue.js"></script> <script type="text/javascript"> //Model var data = { Name: '小明', Age: 18, School:'光明小學', } //ViewModel var vue = new Vue({ el: '#app', data: data, }); </script> </body>
得到效果:
雙向綁定效果展示:
通過v-model指令,很方便的實現(xiàn)了Model和View之間的雙向綁定。單從這種綁定的方式來看,還是比Knockout要簡單一點,至少不用區(qū)分什么普通屬性和監(jiān)控屬性。
四、常用指令
本來按照Vue文檔說明,常用指令應該是放在后面介紹的,但是從使用的層面考慮,先介紹常用指令還是非常必要的,因為博主覺得這些指令是我們?nèi)胧质褂肰ue的橋梁,沒有這些基石,一切的高級應用都是空話。
Vue里面為我們提供的常用指令主要有以下一些。
- v-text
- v-html
- v-if
- v-show
- v-else
- v-for
- v-on
- v-bind
- v-model
- v-pre
- v-cloak
- v-once
每一個指令都可以鏈接到相關(guān)文檔,博主覺得文檔里面每種指令的語法寫得非常詳細,在此就沒必要重復做說明了,下面博主打算將一些常用的指令以分組的形式分別結(jié)合demo來進行解釋說明。
1、v-text、v-html指令
v-text、v-html這兩者分為一組很好理解,一個用于綁定文本,一個用于綁定html。上文使用到的 {{ Name }}這種寫法就是v-text的的縮寫形式。這個很簡單,沒什么好糾結(jié)的,看一個Demo就能明白。
<body> <div id="app"> <h1>姓名:<label v-text="Name"></label></h1> <h1>姓名:{{ Name }}</h1> <div style="font-size:30px;font-weight:bold;" v-html="Age">年齡:</div> </div> <script src="Content/vue/dist/vue.js"></script> <script type="text/javascript"> //Model var data = { Name: '小明', Age: "<label>20</label>", School:'光明小學', } //ViewModel var vue = new Vue({ el: '#app', data: data, }); </script> </body>
效果如下:
代碼說明:
{{Name}}這種寫法和v-text的作用是相同的,用于綁定標簽的text屬性。注意如果標簽沒有text屬性,該綁定會失效,比如你在一個文本框上面使用v-text是沒有效果的
由得到的效果可以看出,v-html綁定后會覆蓋原來標簽里面的內(nèi)容(比如上面的“年齡:”),記住此處是覆蓋而非append。
對于v-html應用的時候要慎重,在網(wǎng)站上動態(tài)渲染任意 HTML 有一定的危險存在,因為容易導致 XSS 跨站腳本攻擊。所以最好是在信任的網(wǎng)址上面使用。
注意v-text和v-html綁定都是單向的,只能從Model到View的綁定,不能實現(xiàn)View到Model的更新。
2、v-model指令
v-model上面有介紹它的雙向綁定功能,對于v-model指令,vue限定只能對表單控件進行綁定,常見的有<input>、<select>、<textarea>等。
<body> <div id="app"> <h2>編輯姓名:<input type="text" v-model="Name" /></h2> <h2>姓名:{{Name}}</h2> <hr /> <h2>編輯備注:<textarea v-model="Remark"></textarea></h2> <h2>備注:{{Remark}}</h2> <hr /> <input type="checkbox" id="basketball" value="籃球" v-model="Hobby"> <label for="basketball">籃球</label> <input type="checkbox" id="football" value="足球" v-model="Hobby"> <label for="football">足球</label> <input type="checkbox" id="running" value="跑步" v-model="Hobby"> <label for="running">跑步</label> <br> <h2>學生愛好: {{ Hobby }}</h2> <hr /> <h2>戶籍:{{ Huji }}</h2> <select style="width:100px;" class="form-control" v-model="Huji"> <option>湖南</option> <option>廣東</option> <option>北京</option> </select> </div> <script src="Content/vue/dist/vue.js"></script> <script type="text/javascript"> //Model var data = { Name: '小明', Age: 18, School: '光明小學', Hobby: [], Remark: '三好學生', Huji:"" } //ViewModel var vue = new Vue({ el: '#app', data: data, }); </script> </body>
以上列舉了v-model的一些常見用法,應該都不難,基本都是雙向綁定,效果如下:
關(guān)于selece的數(shù)據(jù)源的動態(tài)綁定,我們留在v-for指令的時候介紹。
3、v-if、v-else指令
v-if和v-else是一對離不開的好兄弟,使用條件運算符判斷時常用。需要說明的是,v-if可以單獨使用,但是v-else的前面必須要有一個v-if的條件或者v-show指令(后面介紹),這個和我們編程的原理是一樣一樣的。
它們作為條件渲染指令,他們的基礎語法如下:
v-if="expression",v-else;
注意這里的v-else可以不寫,expression表達式是一個返回bool類型的屬性或者表達式。
<body> <div id="app"> <h1>姓名:<label v-text="Name"></label></h1> <h1>是否已婚:<span v-if="IsMarry">是</span></h1> <h1>大人or小孩:<span v-if="Age>18">大人</span><span v-else>小屁孩</span></h1> <h1>學校:{{ School }}</h1> </div> <script src="Content/vue/dist/vue.js"></script> <script type="text/javascript"> //Model var data = { Name: '小明', IsMarry: true, Age: 20, School:'光明小學', } //ViewModel var vue = new Vue({ el: '#app', data: data, }); </script> </body>
得到結(jié)果:
只有有一點編程基礎,上述應該不難看懂。
4、v-show指令
v-show指令表示根據(jù)表達式之bool值,覺得是否顯示該元素。需要說明的是,如果bool值false,對應的Dom標簽還是會渲染到頁面上面,只是將該標簽的css屬性display設為none而已。而如果你用v-if值,bool值為false的時候整個dom樹都不被渲染到頁面上面。從這點上來說看,如果你的需求是需要經(jīng)常切換元素的顯示和隱藏,使用v-show效率更高,而如果你只一次條件判斷,使用v-if更加合適。
v-show還常和v-else一起使用,表示如果v-show條件滿足,則顯示當前標簽,否則顯示v-else標簽。
<body> <div id="app"> <h1>姓名:<label v-text="Name"></label></h1> <h1>是否已婚:<span v-show="IsMarry">是</span></h1> <h1>學校:{{ School }}</h1> </div> <script src="Content/vue/dist/vue.js"></script> <script type="text/javascript"> //Model var data = { Name: '小明', IsMarry: false, Age: 16, School:'光明小學', } //ViewModel var vue = new Vue({ el: '#app', data: data, }); </script> </body>
得到效果:
5、v-for指令
v-for 指令需要以 item in items 形式的特殊語法。常用來綁定數(shù)據(jù)對象。
最簡單的例子:
<body> <div id="app"> <ul> <li v-for="value in nums">{{value}}</li> </ul> </div> <script src="Content/vue/dist/vue.js"></script> <script type="text/javascript"> //ViewModel var vue = new Vue({ el: '#app', data: { nums: [1, 2, 3, 4, 5, 6, 7, 8, 9] } }); </script> </body>
效果:
除了基礎數(shù)據(jù)之外,還支持Json數(shù)組的綁定。比如:
<div id="app"> <ul> <li v-for="value in values">姓名:{{value.Name}},年齡:{{value.Age}}</li> </ul> </div> <script src="Content/vue/dist/vue.js"></script> <script type="text/javascript"> //ViewModel var vue = new Vue({ el: '#app', data: { values: [{ Name: "小明", Age: 20 }, { Name: "小剛", Age: 18 }, { Name: "小紅", Age: 16 }] } }); </script>
效果:
在v-for里面,可以使用<template> 標簽來渲染多個元素塊,下面就基于bootstrap樣式使用v-for、v-if、v-else等實現(xiàn)一個簡單的demo。
<div id="app"> <nav> <ul class="pagination pagination-lg"> <template v-for="page in pages "> <li v-if="page==1" class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上一頁</a></li> <li v-if="page==1" class="active"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{page}}</a></li> <li v-else><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{page}}</a></li> <li v-if="page==pages"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一頁</a></li> </template> </ul> </nav> </div> <script src="Content/vue/dist/vue.js"></script> <script type="text/javascript"> //ViewModel var vue = new Vue({ el: '#app', data: { pages: 10 } }); </script>
得到效果
是不是很easy!需要說明一點的是,pages是10,然后遍歷它的時候,page的值會從1依次到10。
v-for指令除了支持數(shù)據(jù)對象的迭代以外,還支持普通Json對象的迭代,比如:
<div id="app"> <ul> <li v-for="(value, key) in values"> {{ key }} : {{ value }} </li> <li v-for="(value, key, index) in values"> {{ index }}. {{ key }} : {{ value }} </li> </ul> </div> <script src="Content/vue/dist/vue.js"></script> <script type="text/javascript"> //ViewModel var vue = new Vue({ el: '#app', data: { values: { Name: "小明", Age: 20, School: "**高中" } } }); </script>
得到效果:
6、v-once指令
v-once表示只渲染元素和組件一次。隨后的重新渲染,元素/組件及其所有的子節(jié)點將被視為靜態(tài)內(nèi)容并跳過。什么意思呢?還是來看demo說話:
<div id="app"> <h1>姓名:<label v-once v-text="Name"></label></h1> <h1 v-once>年齡:{{ Age }}</h1> <h1>學校:{{ School }}</h1> </div> <script src="Content/vue/dist/vue.js"></script> <script type="text/javascript"> //Model var data = { Name: '小明', Age: 18, School:'光明小學', } //ViewModel var vue = new Vue({ el: '#app', data: data, }); </script>
效果動態(tài)圖:
可以看出,只要使用v-once指令的,View和Model之間除了初次渲染同步,之后便不再同步,而同一次綁定里面沒使用v-once指令的還是會繼續(xù)同步。
7、v-bind指令
對于html標簽的text、value等屬性,Vue里面提供了v-text、v-model去綁定。但是對于除此之外的其他屬性呢,這就要用到接下來要講的v-bind指令了。博主的理解是v-bind的作用是綁定除了text、value之外的其他html標簽屬性,常見的比如class、style、自定義標簽的自定義屬性等。它的語法如下:
v-bind:property="expression"
先來看幾個簡單的例子。
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link href="Content/bootstrap/css/bootstrap.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet" /> <style type="text/css"> class1 { padding:20px; } .backred { background-color:red; } </style> </head> <body> <div id="app"> <h1>姓名:<label v-text="Name"></label></h1> <h1>是否紅領巾:<span class="class1" v-bind:class="{backred:IsBack}"><label v-if="IsBack">是</label></span></h1> <h1>學校星級:<span v-bind:style="{color:SchoolLevel}">aa</span></h1> </div> <script src="Content/vue/dist/vue.js"></script> <script type="text/javascript"> //Model var data = { Name: '小明', Age: 18, School: '光明小學', SchoolLevel: 'red', IsBack:true } //ViewModel var vue = new Vue({ el: '#app', data: data, }); </script> </body>
需要說明的是同一個標簽里面的同一個屬性,可以既有綁定的寫法,也有靜態(tài)的寫法,組件會自動幫你合并,比如上文中的class屬性。
得到效果如下:
關(guān)于自定義屬性的綁定,打算在綜合應用里面來說。
8、v-on指令
屬性jquery的朋友應該很熟悉這個“on”,對于時間綁定,jquery里面最常用的就是on了。同樣,在Vue里面,v-on指令用來綁定標簽的事件,其語法和v-bind基本類似。
v-on:event="expression";
這里的event可以是Javascript里面的常用事件,也可以是自定義事件。
<div id="app"> <h1>姓名:<label v-text="Name"></label></h1> <h1>年齡:{{ Age }}</h1> <button class="btn btn-primary" v-on:click="Age++;if(Name=='小明')Name='吉姆格林';else Name='小明';">年齡遞增</button> </div> <script src="Content/vue/dist/vue.js"></script> <script type="text/javascript"> //Model var data = { Name: '小明', Age: 18, } //ViewModel var vue = new Vue({ el: '#app', data: data, }); </script>
這段代碼是一個最簡單的應用,直接在click事件里面執(zhí)行邏輯,改變變量的值。效果如下:
除了直接在標簽內(nèi)寫處理邏輯,還可以定義方法事件處理器。
<div id="app"> <h1>姓名:<label v-text="Name"></label></h1> <h1>年齡:{{ Age }}</h1> <button class="btn btn-primary" v-on:click="Hello">Hello</button> </div> <script src="Content/vue/dist/vue.js"></script> <script type="text/javascript"> //Model var data = { Name: '小明', Age: 18, } //ViewModel var vue = new Vue({ el: '#app', data: data, methods: { Hello: function (event) { // `this` 在方法里指當前 Vue 實例 alert('Hello ' + this.Name + '!'); this.Age++; } } }); </script>
結(jié)果應該不難猜。
9、實例一:30分鐘搞定增刪改查
有了我們的Vue框架,關(guān)于行內(nèi)編輯的增刪改查,我們很簡單即可實現(xiàn),如果你熟的話應該還不用30分鐘吧。代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link href="Content/bootstrap/css/bootstrap.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet" /> <style type="text/css"> table thead tr th { text-align:center; } </style> </head> <body> <div style="padding:20px;" id="app"> <div class="panel panel-primary"> <div class="panel-heading">用戶管理</div> <table class="table table-bordered table-striped text-center"> <thead> <tr> <th>用戶名</th> <th>年齡</th> <th>畢業(yè)學校</th> <th>備注</th> <th>操作</th> </tr> </thead> <tbody> <template v-for="row in rows "> <tr><td>{{row.Name}}</td><td>{{row.Age}}</td><td>{{row.School}}</td><td>{{row.Remark}}</td> <td><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="Edit(row)">編輯</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="Delete(row.Id)">刪除</a></td> </tr> </template> <tr> <td><input type="text" class="form-control" v-model="rowtemplate.Name" /></td> <td><input type="text" class="form-control" v-model="rowtemplate.Age" /></td> <td><select class="form-control" v-model="rowtemplate.School"> <option>中山小學</option> <option>復興中學</option> <option>光明小學</option> </select></td> <td><input type="text" class="form-control" v-model="rowtemplate.Remark" /></td> <td><button type="button" class="btn btn-primary" v-on:click="Save">保存</button></td> </tr> </tbody> </table> </div> </div> <script src="Content/jquery-1.9.1.min.js"></script> <script src="Content/vue/dist/vue.js"></script> <script type="text/javascript"> //Model var data = { rows: [ { Id: 1, Name: '小明', Age: 18, School: '光明小學', Remark: '三好學生' }, { Id: 2, Name: '小剛', Age: 20, School: '復興中學', Remark: '優(yōu)秀班干部' }, { Id: 3, Name: '吉姆格林', Age: 19, School: '光明小學', Remark: '吉姆做了汽車公司經(jīng)理' }, { Id: 4, Name: '李雷', Age: 25, School: '復興中學', Remark: '不老實的家伙' }, { Id: 5, Name: '韓梅梅', Age: 22, School: '光明小學', Remark: '在一起' }, ], rowtemplate: { Id: 0, Name: '', Age: '', School: '', Remark: '' } }; //ViewModel var vue = new Vue({ el: '#app', data: data, methods: { Save: function (event) { if (this.rowtemplate.Id == 0) { //設置當前新增行的Id this.rowtemplate.Id = this.rows.length + 1; this.rows.push(this.rowtemplate); } //還原模板 this.rowtemplate = { Id: 0, Name: '', Age: '', School: '', Remark: '' } }, Delete: function (id) { //實際項目中參數(shù)操作肯定會涉及到id去后臺刪除,這里只是展示,先這么處理。 for (var i=0;i<this.rows.length;i++){ if (this.rows[i].Id == id) { this.rows.splice(i, 1); break; } } }, Edit: function (row) { this.rowtemplate = row; } } }); </script> </body> </html>
行內(nèi)編輯效果如下:
10、實例二:帶分頁的表格
上面的例子用最簡單的方式實現(xiàn)了一個增刪改查,為了進一步體驗我們Vue的神奇,博主更進了一步,用Vue去做了一個客戶端分頁的表格功能。其實代碼量并不大。
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link href="Content/bootstrap/css/bootstrap.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet" /> <style type="text/css"> table thead tr th { text-align: center; } </style> </head> <body> <div style="padding:20px;" id="app"> <div class="panel panel-primary"> <div class="panel-heading">用戶管理</div> <table class="table table-bordered table-striped text-center"> <thead> <tr> <th>用戶名</th> <th>年齡</th> <th>畢業(yè)學校</th> <th>備注</th> </tr> </thead> <tbody> <template v-for="(row, index) in rows "> <tr v-if="index>=(curpage-1)*pagesize&&index<curpage*pagesize"> <td>{{row.Name}}</td> <td>{{row.Age}}</td> <td>{{row.School}}</td> <td>{{row.Remark}}</td> </tr> </template> </tbody> </table> </div> <nav style="float:right;"> <ul class="pagination pagination-lg"> <template v-for="page in Math.ceil(rows.length/pagesize)"> <li v-on:click="PrePage()" id="prepage" v-if="page==1" class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上一頁</a></li> <li v-if="page==1" class="active" v-on:click="NumPage(page, $event)"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{page}}</a></li> <li v-else v-on:click="NumPage(page, $event)"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{page}}</a></li> <li id="nextpage" v-on:click="NextPage()" v-if="page==Math.ceil(rows.length/pagesize)"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一頁</a></li> </template> </ul> </nav> </div> <script src="Content/jquery-1.9.1.min.js"></script> <script src="Content/vue/dist/vue.js"></script> <script type="text/javascript"> //Model var data = { rows: [ { Id: 1, Name: '小明', Age: 18, School: '光明小學', Remark: '三好學生' }, { Id: 2, Name: '小剛', Age: 20, School: '復興中學', Remark: '優(yōu)秀班干部' }, { Id: 3, Name: '吉姆格林', Age: 19, School: '光明小學', Remark: '吉姆做了汽車公司經(jīng)理' }, { Id: 4, Name: '李雷', Age: 25, School: '復興中學', Remark: '不老實的家伙' }, { Id: 5, Name: '韓梅梅', Age: 22, School: '光明小學', Remark: '在一起' }, ], pagesize: 2, curpage:1,//當前頁的頁碼 }; //ViewModel var vue = new Vue({ el: '#app', data: data, methods: { //上一頁方法 PrePage: function (event) { $(".pagination .active").prev().trigger("click"); }, //下一頁方法 NextPage: function (event) { $(".pagination .active").next().trigger("click"); }, //點擊頁碼的方法 NumPage: function (num, event) { if (this.curpage == num) { return; } this.curpage = num; $(".pagination li").removeClass("active"); if (event.target.tagName.toUpperCase() == "LI") { $(event.target).addClass("active"); } else { $(event.target).parent().addClass("active"); } if (this.curpage == 1) { $("#prepage").addClass("disabled"); } else { $("#prepage").removeClass("disabled"); } if (this.curpage == Math.ceil(this.rows.length / this.pagesize)) { $("#nextpage").addClass("disabled"); } else { $("#nextpage").removeClass("disabled"); } } } }); </script> </body> </html>
來看看效果吧:
什么,數(shù)據(jù)少了不過癮?那我們加一點數(shù)據(jù)試試唄。調(diào)整一下data變量,其他不用做任何變化。
var data = { rows: [ { Id: 1, Name: '小明', Age: 18, School: '光明小學', Remark: '三好學生' }, { Id: 2, Name: '小剛', Age: 20, School: '復興中學', Remark: '優(yōu)秀班干部' }, { Id: 3, Name: '吉姆格林', Age: 19, School: '光明小學', Remark: '吉姆做了汽車公司經(jīng)理' }, { Id: 4, Name: '李雷', Age: 25, School: '復興中學', Remark: '不老實的家伙' }, { Id: 5, Name: '韓梅梅', Age: 22, School: '光明小學', Remark: '在一起' }, { Id: 1, Name: '小明', Age: 18, School: '光明小學', Remark: '三好學生' }, { Id: 2, Name: '小剛', Age: 20, School: '復興中學', Remark: '優(yōu)秀班干部' }, { Id: 3, Name: '吉姆格林', Age: 19, School: '光明小學', Remark: '吉姆做了汽車公司經(jīng)理' }, { Id: 4, Name: '李雷', Age: 25, School: '復興中學', Remark: '不老實的家伙' }, { Id: 5, Name: '韓梅梅', Age: 22, School: '光明小學', Remark: '在一起' }, { Id: 1, Name: '小明', Age: 18, School: '光明小學', Remark: '三好學生' }, { Id: 2, Name: '小剛', Age: 20, School: '復興中學', Remark: '優(yōu)秀班干部' }, { Id: 3, Name: '吉姆格林', Age: 19, School: '光明小學', Remark: '吉姆做了汽車公司經(jīng)理' }, { Id: 4, Name: '李雷', Age: 25, School: '復興中學', Remark: '不老實的家伙' }, { Id: 5, Name: '韓梅梅', Age: 22, School: '光明小學', Remark: '在一起' }, { Id: 1, Name: '小明', Age: 18, School: '光明小學', Remark: '三好學生' }, { Id: 2, Name: '小剛', Age: 20, School: '復興中學', Remark: '優(yōu)秀班干部' }, { Id: 3, Name: '吉姆格林', Age: 19, School: '光明小學', Remark: '吉姆做了汽車公司經(jīng)理' }, { Id: 4, Name: '李雷', Age: 25, School: '復興中學', Remark: '不老實的家伙' }, { Id: 5, Name: '韓梅梅', Age: 22, School: '光明小學', Remark: '在一起' }, ], pagesize: 6, curpage:1,//當前頁的頁碼 };
測試效果:
如果再進一步封裝,是不是有點分頁組件的概念了。簡單吧!當然,這只是為了體現(xiàn)常用指令而提供的一個實現(xiàn)思路,可能很多地方都有待優(yōu)化,待深入研究組件之后再進一步封裝。
相關(guān)文章
javascript自定義函數(shù)參數(shù)傳遞為字符串格式
本節(jié)主要介紹了通過自定義javascript函數(shù)傳遞參數(shù)為字符串格式的,用this傳遞、引號缺省,示例如下2014-07-07[js高手之路]設計模式系列課程-發(fā)布者,訂閱者重構(gòu)購物車的實例
下面小編就為大家?guī)硪黄猍js高手之路]設計模式系列課程-發(fā)布者,訂閱者重構(gòu)購物車的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08javascript showModalDialog 內(nèi)跳轉(zhuǎn)頁面的問題
在頁面中使用了showModalDialog,但是在跳轉(zhuǎn)鏈接時,不會在當前頁執(zhí)行,而是彈出一個新的頁面。2010-11-11javascript實現(xiàn)任務欄消息提示的簡單實例
下面小編就為大家?guī)硪黄猨avascript實現(xiàn)任務欄消息提示的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05使用JavaScript判斷一個元素是否在可視范圍內(nèi)的幾種方法
在Web開發(fā)中,有時我們需要知道一個元素是否在用戶的可視范圍內(nèi),以便執(zhí)行相應的操作,比如延遲加載圖片、實現(xiàn)懶加載、或是觸發(fā)動畫效果, 本文將詳細介紹使用 JavaScript 如何判斷一個元素是否在可視范圍內(nèi)的幾種方法,需要的朋友可以參考下2024-02-02