VUE+Boostrap实现简单的用户添加和删除

小编 2026-07-01 阅读:789 评论:0
1.导入所需的库      css需要bootstrap.css      js需要jquery.js,bootstrap.js,vue.js   (直接使用下载好的就行,不需要使用npm等进行下载)...

1.导入所需的库

     css需要bootstrap.css

     js需要jquery.js,bootstrap.js,vue.js

  (直接使用下载好的就行,不需要使用npm等进行下载)

2.使用boostrap实现界面

    实现效果如下

    \"\"

    代码实现如下:

<body>
<div  id=\"userList\" class=\"container\">
    <h3 class=\"text-center\">添加用户</h3>
    <form class=\"form-horizontal\" role=\"form\">
        <div class=\"form-group\">
            <label for=\"name\" class=\"col-sm-2 control-label col-sm-offset-1 \" >姓名:</label>
            <div class=\"col-sm-6\">
                <input id=\"name\" type=\"text\" class=\"form-control\" v-model=\"user.name\"   placeholder=\"请输入姓名\">
            </div>
        </div>

        <div class=\"form-group\">
            <label for=\"age\" class=\"col-sm-2 control-label col-sm-offset-1 \" >年龄:</label>
            <div class=\"col-sm-6\">
                <input id=\"age\" type=\"text\" class=\"form-control\" v-model=\"user.age\" placeholder=\"请输入年龄\">
            </div>
        </div>

        <div class=\"form-group\">
            <label for=\"email\" class=\"col-sm-2 control-label col-sm-offset-1 \" >邮箱:</label>
            <div class=\"col-sm-6\">
                <input id=\"email\" type=\"text\" class=\"form-control\" v-model=\"user.email\" placeholder=\"请输入邮箱\">
            </div>
        </div>
        <div class=\"form-group text-center\">
            <input type=\"button\"  class=\"btn btn-primary\" value=\"添 加\" v-on:click=\"addUser\">
            <input type=\"reset\"   class=\"btn btn-primary\" value=\"重 置\">
        </div>

    </form>
    <hr>
    <table class=\"table table-bordered table-hover\">
        <caption class=\"h3 text-center text-info\">目录列表</caption>
        <thead>
        <tr>
            <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>
        </thead>
        <tbody>
        <tr v-for=\"(user,index) in users\" class=\"text-center\">
            <td>{{index}}</td>
            <td>{{user.name}}</td>
            <td>{{user.age}}</td>
            <td>{{user.email}}</td>
            <td>
                <button class=\"btn btn-danger btn-sm\" data-toggle=\"modal\" data-target=\"#del\" v-on:click=\"nowIndex=index\">删除</button>
            </td>
        </tr>
        <tr >
            <td colspan=\"5\" class=\"text-right\">
                <button class=\"btn btn-danger btn-sm\" data-toggle=\"modal\" data-target=\"#del\" v-on:click=\"nowIndex=-1\">删除所有</button>
            </td>
        </tr>
        </tbody>
    </table>



    <!--模态框,弹出框-->
    <div class=\"modal\" id=\"del\">
        <div class=\"modal-dialog\">
            <div class=\"modal-content\">
                <div class=\"modal-header\">
                    <!--close表示关闭标签的样式-->
                    <button class=\"close\" data-dismiss=\"modal\">&times;</button>
                    <h4 class=\"modal-title\" v-show=\"nowIndex!==-1\">确认要删除用户{{users[nowIndex]?users[nowIndex].name:\'\'}}吗?</h4>
                    <h4 class=\"modal-title\" v-show=\"nowIndex===-1\">确认要删除所有用户吗?</h4>
                </div>
                <div class=\"modal-body text-center\">
                    <button class=\"btn btn-primary\" data-dismiss=\"modal\">取消</button>
                    <button class=\"btn btn-primary\" data-dismiss=\"modal\"  v-on:click=\"deleteUser\">确认</button>
                </div>
            </div>
        </div>
    </div>



</div>

</body>

    代码需要关注的地方:

  1. bootstrap中的栅栏布局class=\"container\";
  2. 可以使用bootstrap中的class=\"text-center (text-right)\"实现容器内的内容居中;
  3. 使用表单的时候:①首先使用<form>标签将所有表单内容包含起来,添加bootstrap类role=\"form\"和class=\"form-horizontal\"实现水平居中,也可以参考bootstrap文档实现垂直等效果;②每一个表单组用div表示,添加类class=\"form-group\",里面可以放<label>或者<input>标签,为了布局方面,可以把input等元素放在一个div里,可以使用boostrap栅栏布局,栅栏布局col相加不大于12,可以使用col-sm-off-set-num,进行偏移,实现居中效果,label和input标签class=\"label-control\"和class=\"form-control\",input可以添加placeholder添加默认内容;③input type设置为button,可以添加bootstrap样式class=\"btn btn-fault(primary,danger,warning)  btn-sm value添加button里的内容,需要强调将inpue设置为type=\"reset\"可以将form标签里的所有内容清空。
  4. 水平分割线可以用<hr>
  5. 表格元素的使用:①结构table下有caption thead tbody,thead下面有tr ,tr里面有th,tbody里面tr,tr有td;②为table设置bootstrap属性,可以有实线,虚线,鼠标悬停效果;③caption是文本,可以为文本添加boostrap效果;④表格里的数据由VUE实例里的数据加载出来;
  6. ①bootstrap里的模态框,有三层modal__modal-dialog__modal-content,modal-content里面有三层,modal-header modal-body,modal-footer;②header设置关闭按钮参考&times;和提示内容class=\"modal-title\";③开启模态框,在需要点击的按钮上设置data-toggle=\"modal\"和data-target=\"#del\";关闭模态框,在模态框中的按钮设置data-dismiss=“modal”,需要强调的是,可以为按钮同时设置data-dismiss和click事件;

3.添加VUE实例

    <script>
        window.onload=function () {
            let vm=new Vue({
                el:\'#userList\',
                data:{
                    users:[
                        {name:\'wkh\',age:22,email:\'6666666@qq.com\'},
                        {name:\'yjp\',age:23,email:\'6777777@qq.com\'}
                    ],
                    /*不写页面会显示不出俩,会报错,user is not defined*/
                    user:{},
                    /*当前要删除项的索引*/
                    nowIndex:-1

                },
                methods:{
                    addUser(){
                        this.users.push(this.user);
                        this.user={};
                    },
                    deleteUser(){
                        if(this.nowIndex==-1){//删除所有
                            this.users=[];
                        }else{
                            /*从指定位置开始删除元素*/
                            this.users.splice(this.nowIndex,1);
                        }
                    }
                }
            })
        }
    </script>

代码需要关注的地方

  1.  windoe.onload=function(){}页面加载完执行;
  2. new一个vue实例,有el,data,methods等内容;
  3. 添加用户时,用户的姓名,年龄,邮件和VUE中的数据相关联,也就是相互绑定需要v-modal,绑定的是usr.name,所以需要在data里设置这样user对象;添加用户按钮设置v-onclick:\"xxx\"事件,简写@:click=\"xxx\";添加用户动作使用push,添加完成后清空user对象。
  4. 表格数据在tr就是行上使用v-for指令,然后在td里使用{{}}语法展示数据;
  5. 区分删除那一行还是删除所有。可以使用数组的index属性来区分,index最小是0,可以用变量currentIndex区分,如果是删除所有,就设置为-1.删除当前用户,则设置为当前index值,这样在弹出模态框的同时,currentIndex的值就发生了变化。   然后可以在模态框中点击确认进行动作的执行,动作执行写一个函数,先判断currentIndex的值,如果是-1,删除所有,如果不是,就删除当前值。删除所有直接赋值空数组,删除固定下标的值,使用splice。
  6. 注意this的使用。
版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

热门文章
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
标签列表