1. 程式人生 > 程式設計 >vue-table實現新增和刪除

vue-table實現新增和刪除

本文例項為大家分享了vue-table實現新增和刪除的具體程式碼,供大家參考,具體內容如下

一.程式碼

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>vue-table示例</title>
    <style>
        .table_box {
            height: auto;
            width: 90%;
            margin: 5% auto;
        }

        .table {
            border-collapse:http://www.cppcns.com
collapse; width: 100%; height: auto; } h1 { text-align: center; } </style> </head> <body> <div id="app"> <div class="table_box"> <h1>表格練習</h1> <input type="PnQuXZwdtext" v-model="text"/> <button @click="add">新增</button> <table class="table" border="1"> <thead> <tr> <th>序號</th> <th>品牌</th> <th>時間</th> <th>操作</th> </tr> </thead> <tbody> &lwww.cppcns.com
t;tr v-for="(v,k) in list" :key="k"> <th>{{v.id}}</th> <th>{{v.name}}</th> <th>{{v.tiwww.cppcns.comme}}</th> <th> <a href="#" @click.prevent="del(k)">刪除</a> </th> </tr> </tbody> </table> </div> </div> </body> </html> <script src="https://cdn.
js
delivr.net/npm/vue"></script> <script> 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("請問您是否確定刪除當前行")) { th程式設計客棧is.list.splice(index,1); } },} }); </script>

二.執行效果

vue-table實現新增和刪除

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。