Skip to content

Instantly share code, notes, and snippets.

@edwardlorilla
Created May 17, 2021 15:49
Show Gist options
  • Save edwardlorilla/78b3bcc28daf77142524cebce5c956d8 to your computer and use it in GitHub Desktop.
Save edwardlorilla/78b3bcc28daf77142524cebce5c956d8 to your computer and use it in GitHub Desktop.
vue-table implements adding and deleting
<div id="app">
<div class="table_box">
<h1>Form exercises</h1>
<input type="text" v-model="text"/>
<button @click="add">Add</button>
<table class="table" border="1">
<thead>
<tr>
<th>Serial number</th>
<th>Brand</th>
<th>Time</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<tr v-for="(v,k) in list" :key="k">
<th>{{v.id}}</th>
<th>{{v.name}}</th>
<th>{{v.time}}</th>
<th>
<a href="#" @click.prevent="del(k)">Delete</a>
</th>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
var vm = new Vue({
el:'#app',
data: {
num: 1,
list: [],
text:'',
},
methods: {
add: function () {
this.list.unshift({
"id": this.num++,
"name": this.text,
"time": new Date().toLocaleString(),
});
},
del: function (index) {
if (confirm("Are you sure to delete the current line")) {
this.list.splice(index, 1);
}
},
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.min.js"></script>
.table_box {
height: auto;
width: 90%;
margin: 5% auto;
}
.table {
border-collapse: collapse;
width: 100%;
height: auto;
}
h1 {
text-align: center;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment