vue实现数据增删查

1添加数据(v-model数据双向绑定,使数据实时更新)

界面代码(附带bootstrap表格样式

<div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">添加品牌</h3>
            </div>
            <div class="panel-body form-inline">
                <label>
                    Id:
                    <input type="text" class="form-control" v-model="id">
                </label>
                <label>
                    Name:
                    <input type="text" class="form-control" v-model="name">
                </label>
                <input type="button" value="添加" class="btn btn-primay" @click="add()">
            </div>
        </div>
js代码(函数部分)
add(){
                    //组织出一个对象以便于赋值
                    var car={id:this.id,name:this.name,ctime:new Date()}
                    //push函数
                    this.list.push(car)
                    this.id=this.time=''
                },

2查找(接上)

界面代码
<label>
                    Name:
                    <input type="text" class="form-control" v-model="name">
                </label>
                <input type="button" value="添加" class="btn btn-primay" @click="add()">
js代码(函数部分)
search(keywords){
                    //遍历所有元素
                    return this.list.filter(item=>{
                        // 注意 :ES6中,为字符串提供了一个新方法,叫做  String.prototype.includes('要包含的字符串')
                        //  如果包含,则返回 true ,否则返回 false
                        if((item.name).includes(keywords)){
                            return item
                        }
                    })

                }

3删除

界面代码
<table class="table table-border table-hover table-striped">
            <thead>
               ......
            </thead>
            <tbody>
                <tr v-for="item in search(keywords)":key="item.id">
                    <td>{{item.id}}</td>
                    <td v-text="item.name"></td>
                    <td>{{item.ctime}}</td>
                    <td><a href="" @click.prevent="del(item.id)">删除</a></td>
                </tr>
            </tbody>
        </table>
js代码(函数部分)
del(id){
                    //遍历所有元素
                    //item是v-for输出循环的item,因为每个元素包含在多有for循环输出的item里面
                    var index=this.list.findIndex(item=>{
                        if(item.id==id){
                            return true;
                            //返回的是index
                        }
                    })
                    //删除对应index的元素
                    this.list.splice(index,1)
                },

全部代码

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="./lib/vue-2.4.0.js"></script>
  <link rel="stylesheet" href="./lib/bootstrap-3.3.7.css">
  <!-- 需要用到Jquery吗??? -->
</head>

<body>
    <div id="app">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">添加品牌</h3>
            </div>
            <div class="panel-body form-inline">
                <label>
                    Id:
                    <input type="text" class="form-control" v-model="id">
                </label>
                <label>
                    Name:
                    <input type="text" class="form-control" v-model="name">
                </label>
                <input type="button" value="添加" class="btn btn-primay" @click="add()">

                <label>
                    搜索名称关键字:
                    <input type="text" class="form-control" v-model="keywords">
                </label>
            </div>
        </div>
        <table class="table table-border table-hover table-striped">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>Ctime</th>
                    <th>Operation</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in search(keywords)":key="item.id">
                    <td>{{item.id}}</td>
                    <td v-text="item.name"></td>
                    <td>{{item.ctime}}</td>
                    <td><a href="" @click.prevent="del(item.id)">删除</a></td>
                </tr>
            </tbody>
        </table>


    </div>

    <script>
        var vm=new Vue({
            el:'#app',
            data:{
                id:'',
                name:'',
                keywords:'',
                list:[
                    {id:1,name:'宝马',ctime:new Date()},
                    {id:2,name:'保时捷',ctime:new Date()},
                    {id:3,name:'北京现代',ctime:new Date()}
                ]
            },
            // 注意:  forEach   some   filter   findIndex   这些都属于数组的新方法,
            //  都会对数组中的每一项,进行遍历,执行相关的操作;
            methods:{
                add(){
                    //组织出一个对象以便于赋值
                    var car={id:this.id,name:this.name,ctime:new Date()}
                    //push函数
                    this.list.push(car)
                    this.id=this.time=''
                },
                del(id){
                    //遍历所有元素
                    //item是v-for输出循环的item,因为每个元素包含在多有for循环输出的item里面
                    var index=this.list.findIndex(item=>{
                        if(item.id==id){
                            return true;
                            //返回的是index
                        }
                    })
                    //删除对应index的元素
                    this.list.splice(index,1)
                },
                search(keywords){
                    //遍历所有元素
                    return this.list.filter(item=>{
                        // 注意 :ES6中,为字符串提供了一个新方法,叫做  String.prototype.includes('要包含的字符串')
                        //  如果包含,则返回 true ,否则返回 false
                        if((item.name).includes(keywords)){
                            return item
                        }
                    })

                }
            }

        })
    </script>



</body>


</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值