初学者-用VUE指令实现表格的增加、删除、修改操作

自己简单的学习了VUE的一些指令操作后,为了巩固知识,尝试用VUE的指令实现表格的增删改的操作,不涉及到后端。表格内容是VUE里面的对象数组实现的;效果图如下;
在这里插入图片描述
主要VUE涉及到的指令知识是:

  1. v-show控制某一部分是否展示
  2. v-bind绑定事件
  3. v-model绑定数据
  4. Vue自定义指令操作
  5. Vue方法书写

下面展示代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>VUEtest</title>
    <script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.min.js"></script>
    <link  href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <style>
        *{
            font-size: 15px;
            font-family: "微软雅黑 Light";
        }
    </style>
</head>
<body >
<div  class="app" style="width:70%;margin: 0 auto;"align="center">
    <table align="center" class="table-strapped table table-striped table-bordered table-hover" >
        <h1>表格展示
            <div align="right" >
                <button class="button"  v-btcolor @click="More()">直接新增信息</button>
                <button class="button"  @click="Store()">保存信息</button>
            </div>
        </h1>
        <thead>
        <tr>
            <th>序号</th>
            <th >姓名</th>
            <th>分数</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody >

        <tr v-for="(item,key,index) in persons">
            <td ><input type="text" v-model="item.id" :disabled="isDisabled" placeholder="请输入序号"></td>
            <td ><input type="text" v-model="item.name" :disabled="isDisabled" placeholder="请输入姓名"></td>
            <td ><input type="text" v-model="item.score" :disabled="isDisabled" placeholder="请输入分数"></td>
            <td>
                <table >
                    <tr class="col-md-6">
                        <td><a @click="Exit()">编辑</a></td>
                    </tr>
                    <tr class="col-md-6">
                        <td><a @click="Del(item.id)">删除</a></td>
                    </tr>
                </table>
            </td>
        </tr>
        </tbody>
    </table>
</div>




<script>
    vue1=new Vue({
        el:'.app',
        data:{
            isDisabled:true,
            isShow:false,
            persons: [{id: 1,name:"小何",score:"99"},
                {id: 2,name: "小李", score: "88"},
                {id: 3,name: "小花",score: "88"},
                {id: 4,name: "小刘",score: "88"},
                {id: 5,name: "小明",score: "90"}]
        },
        methods:{
            Exit(){
                //允许编辑
                this.isDisabled=false
            },
            Store(){
                //禁止编辑
                this.isDisabled=true
            },
            Del(){
                //删除数据
                this.persons.splice(this.id,1)
            },
            Add(){
                // 展示新增数据的窗口
                this.isShow=true,
                    this.person.push({
                        id:'',
                        name:'',
                        score:''
                    })
            },
            More(){
                //直接新增数据
                this.persons.push({
                    id:'',
                    name:'',
                    score:''
                }), this.isDisabled=false
            }
        },
        directives:{
            "btcolor":{
                bind:function(el) {
                    el.style.color = "black";

                }
            }
        }
    })

</script>

</body>
</html>

有问题可在下方讨论哦~(0o0)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值