vue2.0實(shí)現(xiàn)列表數(shù)據(jù)增加和刪除
更新時(shí)間:2020年06月17日 17:26:59 作者:yw00yw
這篇文章主要為大家詳細(xì)介紹了vue2.0實(shí)現(xiàn)列表數(shù)據(jù)增加和刪除,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了vue2.0實(shí)現(xiàn)列表數(shù)據(jù)增加和刪除的具體代碼,供大家參考,具體內(nèi)容如下
css
<style> [v-cloak]{ display: none; } table{ width: 800px; border-collapse: collapse; margin: 20px auto; } table th,table td{ background: #0094ff; color: white; font-size: 16px; padding: 5px; text-align: center; border: 1px solid black; } table td{ background: #fff; color: red; } </style>
html
<div id="app"> <input type="text" v-model="id"> <input type="text" v-model="pname"> <button @click="addData">添加</button> <table> <tr> <th>編號(hào)</th> <th>名稱</th> <th>創(chuàng)建時(shí)間</th> <th>操作</th> </tr> <tr v-if="list.length == 0"> <td colspan="4">當(dāng)前列表無(wú)數(shù)據(jù)</td> </tr> <tr v-for="(item,index) in list"> <td>{{item.id}}</td> <td>{{item.pname}}</td> <td>{{item.ctime}}</td> <td> <!-- 方法一 --> <!-- <a href="#" @click="delData(index)">刪除</a> --> <!-- 方法二 --> <a href="#" @click="delData(item.id)">刪除</a> </td> </tr> </table> </div>
js
<script src="../dist/vue.js"></script> <script> var vm = new Vue({ el: '#app', data: { id: 0, pname: '', list: [ {id: 1, pname: '奔馳1', ctime: new Date} ] }, methods: { addData(){ // 包裝成list要求的對(duì)象 var p = {id: this.id, pname: this.pname, ctime: new Date()} this.list.push(p); // 清空文本框中的數(shù)據(jù) this.id = 0; this.pname = ''; }, delData: function(index){ if(!confirm('是否要?jiǎng)h除當(dāng)前數(shù)據(jù)')){ //當(dāng)用戶點(diǎn)擊的取消按鈕的時(shí)候,應(yīng)該阻斷這個(gè)方法中的后面代碼的繼續(xù)執(zhí)行 return; } // 方法一 // this.list.splice(index,1); // 方法二: // 根據(jù) id 獲取要?jiǎng)h除的索引,方法一是直接傳入刪除數(shù)組的索引 var index = this.list.findIndex(function(item){ return item.id == index; }); this.list.splice(index,1); } } }); </script>
效果圖
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Vue+elementUI實(shí)現(xiàn)動(dòng)態(tài)展示列表的數(shù)據(jù)
- vue?使用el-table循環(huán)輪播數(shù)據(jù)列表的實(shí)現(xiàn)
- vue實(shí)現(xiàn)動(dòng)態(tài)列表尾部添加數(shù)據(jù)執(zhí)行動(dòng)畫(huà)
- vue如何從后臺(tái)獲取數(shù)據(jù)生成動(dòng)態(tài)菜單列表
- Vue3 列表界面數(shù)據(jù)展示詳情
- 使用Vue3進(jìn)行數(shù)據(jù)綁定及顯示列表數(shù)據(jù)
- vue列表數(shù)據(jù)刪除后主動(dòng)刷新頁(yè)面及刷新方法詳解
- Vue如何獲取數(shù)據(jù)列表展示
- vue input輸入框關(guān)鍵字篩選檢索列表數(shù)據(jù)展示
- vue父列表數(shù)據(jù)獲取子列表數(shù)據(jù)的實(shí)現(xiàn)步驟
相關(guān)文章
vue實(shí)現(xiàn)樹(shù)形結(jié)構(gòu)增刪改查的示例代碼
其實(shí)很多公司都會(huì)有類似于用戶權(quán)限樹(shù)的增刪改查功能,本文主要介紹了vue實(shí)現(xiàn)樹(shù)形結(jié)構(gòu)增刪改查,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09vue設(shè)置頁(yè)面超時(shí)15分鐘自動(dòng)退出登錄的方法詳解
當(dāng)用戶登錄后,如果長(zhǎng)時(shí)間未操作頁(yè)面這個(gè)時(shí)候需要自動(dòng)退出登錄回到登錄頁(yè)面,本文將給大家介紹一下vue設(shè)置頁(yè)面超時(shí)15分鐘自動(dòng)退出登錄的方法,感興趣的同學(xué)可以自己動(dòng)手試一下2023-10-10Vue3子組件watch無(wú)法監(jiān)聽(tīng)父組件傳遞的屬性值的解決方法
這篇文章主要介紹了Vue3子組件watch無(wú)法監(jiān)聽(tīng)父組件傳遞的屬性值的解決方法,文中通過(guò)代碼示例講解的講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-10-10vue.js提交按鈕時(shí)進(jìn)行簡(jiǎn)單的if判斷表達(dá)式詳解
這篇文章主要給大家介紹了關(guān)于vue.js提交按鈕時(shí)如何進(jìn)行簡(jiǎn)單的if判斷表達(dá)式的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08