- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) > JavaScript >
- Vue前端高效開(kāi)發(fā)之列表渲染指令
說(shuō)起列表不得不提起循環(huán),v-for指令便是可以在vue中實(shí)現循環(huán)的操作。
v-for指令是基于一個(gè)數組來(lái)重復渲染的指令,通常就用于顯示列表和表格。
語(yǔ)法:
<ul v-for="(鍵,值,索引) in 數組">
<li>{{索引}}:{{值}}:{{鍵}}</li>
</ul>
例:
<body> <style> * { margin: 0px; padding: 0px; } ul { list-style: none; } </style> <!--遍歷數據--> <div id="app"> <!--item:鍵--> <!--value:值--> <!--index:下標--> <ul v-for="(item,value,index) in people"> <li>{{index}}:{{value}}:{{item}}</li> </ul> </div> <script src="js/Vue.js"></script> <script> new Vue({ el: "#app", data: { text: "我們的征途是星辰大海!", arr: ["瑪卡巴卡", "唔西迪西", "小點(diǎn)點(diǎn)", "湯姆布利多", "叮叮車(chē)"], people: { id: 1, name: "周潤發(fā)", age: 65 } } }) </script> </body>
由例子可以看出,v-for指令不僅可以遍歷字符串,數組,還可以遍歷對象類(lèi)型,根據鍵值和索引,以列表或者表格形式顯示。
一般在項目開(kāi)發(fā)中,數據往往需要經(jīng)過(guò)一些處理,除了利用基本的表達式和過(guò)濾器外,還可以使用vue的計算屬性,優(yōu)化程序以及完成復雜計算。
例:實(shí)現模糊篩選以及增加和刪除。
首先通過(guò)v-for語(yǔ)句實(shí)現表格顯示數據
<table class="table table-hover table-border"> <tr class="info"> <th>編號</th> <th>姓名</th> <th>年齡</th> <th>介紹</th> </tr> <tr v-for="item in lists"> <td>{{item.id}}</td> <td>{{item.name}}</td> <td>{{item.age+"歲"}}</td> <td>{{item.show}}</td> </tr> </table>
<script> new Vue({ el: "#app", data: { "lists": [{ "id": 1, "name": "張三", "age": 18, "show": "張三介紹" }, { "id": 2, "name": "李四", "age": 19, "show": "李四介紹" }, { "id": 3, "name": "王五", "age": 20, "show": "王五介紹" }, { "id": 4, "name": "趙六", "age": 21, "show": "趙六介紹" }, { "id": 5, "name": "孫八", "age": 22, "show": "孫八介紹" }] } </script>
使用計算屬性實(shí)現模糊查詢(xún)
<p><input type="text" v-model="selectkey" placeholder="請輸入"></p>
computed: { newlist: function() { var _this = this; return _this.lists.filter(function(val) { return val.name.indexOf(_this.selectkey) != -1; }) } }
把計算屬性以函數形式寫(xiě)到computed選項中,將v-for語(yǔ)句遍歷的集合改為篩選后的newlist集合,通過(guò)判斷字符串中是否存在子字符串篩選lists集合中的數據,再把篩選后的數據交給v-for遍歷顯示。
實(shí)現添加及刪除
<p class="input-group"> <span class="input-group-addon">編號:</span> <input type="text" v-model="id" placeholder="請輸入編號" class="form-control"> </p> <p class="input-group"> <span class="input-group-addon">姓名:</span> <input type="text" v-model="name" placeholder="請輸入姓名" class="form-control"> </p> <p class="input-group"> <span class="input-group-addon">年齡:</span> <input type="text" v-model="age" placeholder="請輸入年齡" class="form-control"> </p> <p class="input-group"> <span class="input-group-addon">信息:</span> <input type="text" v-model="show" placeholder="請輸入信息" class="form-control"> </p> <p class="input-group"> <button @click="add()" class="btn btn-primary">添加信息</button> </p>
<td> <button v-on:click="dels(item.id)" class="btn btn-primary">刪除</button> </td>
methods: { add: function() { var girl = { "id": this.id, "name": this.name, "age": this.age, "show": this.show } this.lists.push(girl); }, dels: function(o) { //刪除的是下標,刪除幾個(gè) for (let i = 0; i < this.lists.length; i++) { if (this.lists[i].id == o) { this.lists.splice(i, 1); } } } }
通過(guò)methods綁定事件,添加兩個(gè)按鈕事件方法add和dels用于處理添加和刪除操作。
添加就是使用push方法添加,刪除這里的splice方法僅能通過(guò)下標刪除,而傳過(guò)來(lái)的值是id所以這里為了確保正確性就需要循環(huán)判斷下標,進(jìn)行刪除操作。
這就是計算屬性。用于處理數據。
vue除了計算屬性還提供了監聽(tīng)屬性用于處理數據,用于觀(guān)察數據變動(dòng)。
不同的是監聽(tīng)屬性需要有兩個(gè)形參,一個(gè)是當前值,一個(gè)是更新后的值。
例:
watch: { first: function (val) { this.full = val + ' ' + this.last }, last: function (val) { this.full = this.first + ' ' + val } }
相比于監聽(tīng)屬性,明顯計算屬性會(huì )優(yōu)于監聽(tīng)屬性,所以在非特殊情況下,還是推薦優(yōu)先使用計算屬性。
到此這篇關(guān)于Vue前端高效開(kāi)發(fā)之列表渲染指令的文章就介紹到這了,更多相關(guān)Vue列表渲染內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站