<body>
    <!-- v-bind 属性绑定 -->
    <!-- v-on 事件绑定事件 -->
    <div id="app">
        <!-- <p>{{ message }}</p> -->
        <!-- <input type="button" value="按钮" :title="misss" @click="show">  -->
         
         <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-primary" @click='add'>
                    <label>
                       搜索关键字:
                        <input type="text" class="form-control" v-model="keywords">
                    </label>
               </div>
         </div>
         
        
        <table class="table table-bordered 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,index) in seach(keywords)" :key='item.id'> 
                    <td>{{ item.id }}</td>
                    <td>{{ item.name }}</td>
                    <td>{{ item.time }}</td>
                    <td>
                        <a href="" @click.prevent='del(item.id)'>删除</a>
                    </td>
                </tr>
            </tbody>
        </table>
        
      </div>
</body>
<script>
new Vue({
    el:'#app',
    data:{
        id:"",
        name:"",
        // message:"woaini",
        // misss:"点一点",
        keywords:'',
        list:[
            {id:1,name:'宝马',time: new Date() },
            {id:2,name:'奔驰',time: new Date() }
        ],

    },
    methods: {
        // 添加
        add(){
            let car = {id:this.id, name:this.name,time:new Date()}
            this.list.push(car)
            

        },
        // 删除
        // del(id){
        //      this.list.some((item, i) => {
        //       if(item.id === id){
        //         this.list.splice(i, 1)
        //        return true;
        //       }
        //      })   
        // },
        del(id){
             var index = this.list.findIndex((item, i) => {
              if(item.id === id){
                 return true;
              }
             })  
             this.list.splice(index, 1) 
        },
        //搜索
        // seach(keywords){
        //     let  newList = []
        //     this.list.forEach(item => {
        //         if(item.name.indexOf(keywords) != -1){
        //             newList.push(item)
        //         }
        //     });
        //     return newList;
        // }

        //forEach some filter(过滤)  findIndex(查找索引值)  都是属于数组的新方法
        //都会对每个数组进行遍历,执行相关的操作
        seach(keywords){
             return this.list.filter(item =>{
                 //es6中新提供的字符串的方法 includes(要包含字符串),如果包含则返回true,
                 if(item.name.includes(keywords)){
                        return item           
                 }
             })
         }
    },
})

</script>

posted on 2019-12-04 16:55    阅读(376)  评论(0)    收藏  举报