A Pen by Edward Lance Lorilla on CodePen.
Created
May 17, 2021 15:49
-
-
Save edwardlorilla/78b3bcc28daf77142524cebce5c956d8 to your computer and use it in GitHub Desktop.
vue-table implements adding and deleting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| }, | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.min.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .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