Vue静态增删改查加批量删除

展示:
在这里插入图片描述

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="vue.js"></script>
</head>
<body>
    <div id="app">
        姓名:<input type="text" v-model="addname"/>
        年龄:<input type="text" v-model="addage"/>
        <input type="button" @click="add" value="添加"/>
        <input type="button" @click="deleteBatch" value="批量删除"/>
        
        <table border="1" width="400px" style="text-align: center" v-show="list.length>0">
            <tr>
                <th>编号</th>
                <th>姓名</th>
                <th>年龄</th>
                <th>操作</th>
            </tr>
            <tr v-for="(user,index) in list">
                <td>{{index+1}}</td>
                <td>{{user.name}}</td>
                <td>{{user.age}}</td>
                <td>
                    <input type="button" @click="del(index)" value="删除"/>
                    <input type="button" @click="huixian(index)" value="回显"/>
					        <input type="button" @click="upd" value="修改"/>

                </td>
            </tr>
        </table>

        <p v-show="list.length==0" style="color: red"><b>没有数据!</b></p>
    </div>
</body>
<script>
    new Vue({
        el:"#app",
        data:{
            addname:"",
            addage:"",
            xiabiao:"",
            list:[
                {"name":"张三","age":18},
                {"name":"李四","age":28},
                {"name":"王五","age":38}
            ]
        },
        methods:{
            // 添加
            add:function (){
                this.list.push({"name":this.addname,"age":this.addage})
                this.addname = "";
                this.addage = "";
            },
            // 删除
            del:function (index){
                if (confirm("您确定要删除吗?")){
                    this.list.splice(index,1);
                }
            },
            // 回显
            huixian:function (index) {
                this.addname = this.list[index].name;
                this.addage = this.list[index].age;
                this.xiabiao = index;
            },
            // 修改
            upd:function () {
                this.list[this.xiabiao].name = this.addname;
                this.list[this.xiabiao].age = this.addage;
                this.addname = "";
                this.addage = "";
            },
            // 批量删除
            deleteBatch:function () {
                if (confirm("您确定都要删除吗?")) {
                    this.list = [];
                }
            }
            
        }
    })
</script>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jq1223

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值