表格的增加删除

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>学员信息表</title>

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

    <script src="./vue.js"></script>

    <style>

        [v-cloak] {

            display: none;

        }

    </style>

</head>

<body>

    <div id="app" class="container" v-cloak>

        <h2 class="text text-center text-success">学员信息表单</h2>

        <div class="form-group">

            <label for="username">用户名</label>

            <input v-model="user.name" type="text" class="form-control" id="username" placeholder="请输入用户名">

        </div>

        <div class="form-group">

            <label for="phone">手机号</label>

            <input v-model="user.phone" type="text" class="form-control" id="phone" placeholder="请输入手机号">

        </div>

        <div class="form-group">

            <label for="remark">备注</label>

            <input v-model="user.remark" type="text" class="form-control" id="remark">

        </div>

        <button @click="add" type="submit" class="btn btn-success">添加</button>

        <button @click="reset" type="submit" class="btn btn-info">重置</button>

        <h2 class="text text-center text-warning">学员信息表</h2>

        <table class="table table-bordered table-hover">

            <thead>

                <tr>

                    <th>序号</th>

                    <th>姓名</th>

                    <th>手机号</th>

                    <th>备注</th>

                    <th>操作</th>

                </tr>

            </thead>

            <tbody>

                <!-- 如果表格里有数据 -->

                <template v-if="students.length>0">

                    <tr v-for="(item,index) in students" :key="item.id">

                        <td>{{item.id}}</td>

                        <td>{{item.name}}</td>

                        <td>{{item.phone}}</td>

                        <td>{{item.remark}}</td>

                        <td>

                           

                            <button @click="del(index)" class="btn btn-danger">删除</button>

                           

                        </td>

                    </tr>

                </template>

                <!-- 如果表格里没有数据 -->

                <tr v-else>

                    <td colspan="5">暂无数据</td>

                </tr>

            </tbody>

        </table>

        <div v-if="students.length>0">

            <button @click="delAll" class="btn btn-primary">全部删除</button>

        </div>

    </div>

    <script>

        var vm = new Vue({

            el: "#app",

            data: {

                //表单数据

                user: {

                    name: '', //用户名

                    phone: '', //手机号

                    remark: '', //备注

                },

                // 表格数据

               

                // 判断students在不在localStorage中,如果在就进行获取,不在显示空

                students:localStorage.getItem('students') ? JSON.parse(localStorage.getItem('students')) : [],

                // students: [

                //     {

                //         id: 1,

                //         name: '张三',

                //         phone: '110',

                //         remark: '张三'

                //     },

                //     {

                //         id: 2,

                //         name: '李四',

                //         phone: '121',

                //         remark: '李四'

                //     },

                //     {

                //         id: 3,

                //         name: '王五',

                //         phone: '119',

                //         remark: '王五'

                //     }

                // ]

            },

            methods: {

                // 添加数据

                add() {

                    let obj = {

                        id: this.students.length + 1,

                        // name:this.user.name,

                        // phone:this.user.phone,

                        // remark:this.user.remark

                    // 或者

                        ...this.user

                    }

                    // console.log(this.user);

                   

                    // 将obj中的数据添加到students中

                    this.students.push(obj);

                    // 将数据存入localstorage中

                    localStorage.setItem('students',JSON.stringify(this.students))

                    // 添加成功后清空

                    this.reset()

                },

                // 重置

                reset() {

                    this.user = {

                        name: '', //用户名

                        phone: '', //手机号

                        remark: '', //备注

                    }

                },

                // 删除

                del(index) {

                    this.students.splice(index,1);

                    if(this.students.lengh == 0){

                        // 移除localStorage

                        localStorage.removeItem('students')

                    }else{

                        localStorage.setItem('students',JSON.stringify(this.students))

                    }

                   

                },

                // 全部删除

                delAll() {

                    this.students = [];

                    // 移除localStorage

                    localStorage.removeItem('students')

                },

               

            }

        })

    </script>

</body>

</html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值