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)前列表無數(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)畫
- vue如何從后臺(tái)獲取數(shù)據(jù)生成動(dòng)態(tài)菜單列表
- Vue3 列表界面數(shù)據(jù)展示詳情
- 使用Vue3進(jìn)行數(shù)據(jù)綁定及顯示列表數(shù)據(jù)
- vue列表數(shù)據(jù)刪除后主動(dòng)刷新頁面及刷新方法詳解
- Vue如何獲取數(shù)據(jù)列表展示
- vue input輸入框關(guān)鍵字篩選檢索列表數(shù)據(jù)展示
- vue父列表數(shù)據(jù)獲取子列表數(shù)據(jù)的實(shí)現(xiàn)步驟
相關(guān)文章
在Vue中實(shí)現(xiàn)二維碼生成與掃描功能的方法
二維碼是一種廣泛應(yīng)用于各種場(chǎng)合的編碼方式,它可以將信息編碼成一張二維圖案,方便快捷地傳遞信息,在Vue.js中,我們可以使用一些庫和組件來實(shí)現(xiàn)二維碼的生成和掃描,本文將介紹如何在Vue中實(shí)現(xiàn)二維碼的生成和掃描的方法2023-06-06
使用props傳值時(shí)無法在mounted處理的解決方案
這篇文章主要介紹了使用props傳值時(shí)無法在mounted處理的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
基于vue-cli3和element實(shí)現(xiàn)登陸頁面
這篇文章主要介紹了vue-cli3和element做一個(gè)簡(jiǎn)單的登陸頁面本文實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
關(guān)閉eslint檢查和ts檢查的簡(jiǎn)單步驟記錄
這篇文章主要給大家介紹了關(guān)于關(guān)閉eslint檢查和ts檢查的相關(guān)資料,eslint是一個(gè)JavaScript的校驗(yàn)插件,通常用來校驗(yàn)語法或代碼的書寫風(fēng)格,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02

