vue2.0實現(xiàn)列表數(shù)據(jù)增加和刪除
更新時間:2020年06月17日 17:26:59 作者:yw00yw
這篇文章主要為大家詳細介紹了vue2.0實現(xiàn)列表數(shù)據(jù)增加和刪除,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue2.0實現(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>編號</th> <th>名稱</th> <th>創(chuàng)建時間</th> <th>操作</th> </tr> <tr v-if="list.length == 0"> <td colspan="4">當(dāng)前列表無數(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要求的對象 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('是否要刪除當(dāng)前數(shù)據(jù)')){ //當(dāng)用戶點擊的取消按鈕的時候,應(yīng)該阻斷這個方法中的后面代碼的繼續(xù)執(zhí)行 return; } // 方法一 // this.list.splice(index,1); // 方法二: // 根據(jù) id 獲取要刪除的索引,方法一是直接傳入刪除數(shù)組的索引 var index = this.list.findIndex(function(item){ return item.id == index; }); this.list.splice(index,1); } } }); </script>
效果圖
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Vue+elementUI實現(xiàn)動態(tài)展示列表的數(shù)據(jù)
- vue?使用el-table循環(huán)輪播數(shù)據(jù)列表的實現(xiàn)
- vue實現(xiàn)動態(tài)列表尾部添加數(shù)據(jù)執(zhí)行動畫
- vue如何從后臺獲取數(shù)據(jù)生成動態(tài)菜單列表
- Vue3 列表界面數(shù)據(jù)展示詳情
- 使用Vue3進行數(shù)據(jù)綁定及顯示列表數(shù)據(jù)
- vue列表數(shù)據(jù)刪除后主動刷新頁面及刷新方法詳解
- Vue如何獲取數(shù)據(jù)列表展示
- vue input輸入框關(guān)鍵字篩選檢索列表數(shù)據(jù)展示
- vue父列表數(shù)據(jù)獲取子列表數(shù)據(jù)的實現(xiàn)步驟
相關(guān)文章
vue實現(xiàn)樹形結(jié)構(gòu)增刪改查的示例代碼
其實很多公司都會有類似于用戶權(quán)限樹的增刪改查功能,本文主要介紹了vue實現(xiàn)樹形結(jié)構(gòu)增刪改查,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09Vue3子組件watch無法監(jiān)聽父組件傳遞的屬性值的解決方法
這篇文章主要介紹了Vue3子組件watch無法監(jiān)聽父組件傳遞的屬性值的解決方法,文中通過代碼示例講解的講解的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-10-10