**使用Vue.js +WebStorm 开发,
可以实现保存,删除 全部删除等功能。**
备注:
v-on:click=”” 实现点击 可以简写成@click=“”。
v-model=”” 实现数据的绑定。
v-for=“vaue in items”

`这里写图片描述

`这里写图片描述

<!DOCTYPE html>
<html lang="en"
<head>
    <meta charset="UTF-8">
    <title>留言板</title>
    <style>

    </style>
    <!---引入外部框架-->
    <link rel="stylesheet" href="lib/bootstrap.min.css">
    <script src="lib/jquery-3.2.1.min.js"></script>
    <script src="lib/bootstrap.js"></script>
    <script src="lib/vue.js"></script>

    <script>
        window.onload=function () {
            new Vue({
                el:"#box",
                data:{
                    myData:[],
                    username:"",
                    age:"",
                    classroom:"",
                    currentIndex:""
                },
                methods:{
                    add:function(){
                        this.myData.push({
                            name:this.username,
                            age:this.age,
                            classroom:this.classroom
                        });
                        this.username="";
                        this.age="";
                        this.classroom="";
                    },
                    clearInput:function () {
                        this.username="";
                        this.age="";
                        this.classroom="";
                    },
                    deleteItem:function (index) {
                        this.myData.splice(index,1);
                    },
                    deleteAll:function () {
                        this.myData=[];
                    }

                }
            });
        };



    </script>
</head>
<body>
    <div class="container"  id="box">
        <form role="form">
            <div class="form-group">
                <label for="username" class="h5"> 用户名:</label>
                <input type="text" id="username" class="form-control" placeholder="输入用户名" v-model="username">
            </div>
            <div class="form-group">
                <label for="age" class="h5"> 年 龄:</label>
                <input type="text" id="age" class="form-control" placeholder="输入年龄" v-model="age">
            </div>
            <div>
                <label for="classroom" class="h5">班级</label>
                <input type="text" id="classroom" class="form-control" placeholder="输入班级" v-model="classroom">
            </div>
            <div class="form-group h3 ">
                <input type="button" value="添加" class="btn btn-primary" v-on:click= "add">
                <input type="button" value="重置" class="btn btn-danger" v-on:click="clearInput">
            </div>
        </form>

        <!--分割线--->
        <hr>
        <table class="table table-bordered table-hover" >
            <caption  class="h3 text-info text-center ">用户信息表</caption>
            <tr class="text-info">
                <th class="text-center">序号</th>
                <th class="text-center">用户名</th>
                <th class="text-center">年龄</th>
                <th class="text-center">班级</th>
                <th class="text-center">操作</th>
            </tr>
            <tr class="text-center" v-for="(item,index) in myData">
                <td >{{index+1}}</td>
                <td >{{item.name}}</td>
                <td >{{item.age}}</td>
                <td >{{item.classroom}}</td>
                <td >
                    <input type="button" value="删除" class="btn btn-primary btn-sm" data-toggle="modal"
                           data-target="#layer"  v-on:click="currentIndex=index" >
                </td>
            </tr>
            <tr v-show="myData.length!=0">
                <td colspan="5" class="text-right">
                    <button class="btn btn-danger btn-sm" v-on:click="deleteAll">删除全部</button>
                </td>
            </tr>
            <tr v-show="myData.length==0">
                <td colspan="5" class="text-center text-muted">
                    <p >暂无数据......</p>
                </td>
            </tr>
        </table>
        <!---模态对话框-->
        <div role="dialog" class="modal"  id="layer" >
            <div class="modal-dialog" >
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">
                            <span>&times;</span>
                        </button>
                        <h4 class="modal-title">确认要删除此行数据吗?</h4>
                    </div>
                    <div class="modal-body text-right" >
                        <button class="btn btn-primary" data-dismiss="modal">取消</button>
                        <button class="btn btn-danger" data-dismiss="modal" v-on:click="deleteItem(currentIndex)">确认</button>
                    </div>
                </div>
            </div>

        </div>
    </div>
</body>
</html>
Logo

前往低代码交流专区

更多推荐