zoukankan      html  css  js  c++  java
  • 第二章 Vue快速入门-- 23 品牌案例-根据关键字实现数组的过滤

      1 <!DOCTYPE html>
      2 <html lang="en">
      3 
      4   <head>
      5     <meta charset="utf-8">
      6     <meta name="viewport" content="width=device-width,initial-scale=1.0">
      7     <title>Document</title>
      8     <!--1.导入Vue的包-->
      9     <script src=" https://cdn.jsdelivr.net/npm/vue"></script>   
     10     <!-- <link rel="stylesheet" href="./lib/bootstrap.css"> -->
     11     <link rel="stylesheet" href=" https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">   
     12     <!-- 需要用到Jquery吗???不推荐在vue中使用Jquery -->
     13 
     14   </head>
     15 
     16   <body>
     17       <div id="app">
     18 
     19       <div class="panel panel-primary">
     20         <div class="panel-heading">
     21           <h3 class="panel-title">添加品牌</h3>
     22         </div>
     23         <div class="panel-body form-inline">
     24           <label>
     25             Id:
     26             <input type="text" class="form-control" v-model="id">
     27           </label>
     28 
     29            <label>
     30             Name:
     31             <input type="text" class="form-control" v-model="name">
     32           </label>
     33           <!-- 在vue中,使用事件绑定机制,为元素指定处理函数的时候,如果加了小括号,就可以给函数传参了 -->
     34           <input type="button" value="添加" class="btn btn-primary" @click="add()">
     35             
     36             <label>
     37             搜索名称关键字:
     38             <input type="text" class="form-control" v-model="keywords">
     39           </label>
     40 
     41         </div>
     42       </div>
     43 
     44 
     45 
     46       <table class="table table-bordered table-hover table-striped">
     47         <thead>
     48           <tr>
     49             <th>Id</th>
     50             <th>Name</th>
     51             <th>Ctime</th>
     52             <th>Operation</th>
     53           </tr>
     54         </thead>
     55         <tbody>
     56          <!--  之前,v-for中的数据,都是直接从data上的list中直接渲染过来的 -->
     57          <!--  现在,我们自定义了一个方法,同时,把所有的关键字,通过传参的形式,传递给了search方法 -->
     58          <!-- 在search方法内部,通过执行for循环,把所有符合搜索关键字的数据,保存到一个新数组中,返回 -->
     59           <tr v-for="item in search(keywords)" :key="item.id">
     60             <td>{{item.id}}</td>
     61             <td v-text="item.name"></td>
     62             <td>{{item.ctime}}</td>
     63             <td>
     64               <a href="" @click.prevent="del(item.id)">删除</a>
     65             </td>
     66           </tr>
     67         </tbody>
     68         </table>
     69 
     70 
     71       </div>
     72 
     73 
     74       <script>
     75           //创建 Vue 实例,得到 ViewModel
     76           var vm =  new Vue({
     77               el:'#app',
     78         data:{
     79           id:'',
     80           name:'',
     81           keywords:'',//搜索的关键字
     82           list:[
     83           {id:1,name:'奔驰',ctime:new Date()},
     84           {id:2,name:'宝马',ctime:new Date()}
     85           ]
     86         },
     87         methods:{
     88           add(){//添加的方法
     89             // console.log('ok')
     90             //分析:
     91             //1.获取到id 和name ,直接从data上面获取
     92             //2.组织出一个对象
     93             //3.把这个对下,调用数组的相关方法,添加到当前data上的list中
     94             //4.注意:在Vue中,已经实现了数据的双向绑定,每当我们修改了data中的数据,Vue会默认监听到数据的改动,自动把最新的数据,应用到页面上;
     95             //5.当我们意识到上面的第四部的时候,就证明大家已经入门Vue了,我们更多的是在进行VM中Model数据操作,同时,在操作Model数据的时候,指定的业务逻辑操作;
     96 
     97             var car={id:this.id,name:this.name,ctime:new Date()}
     98             this.list.push(car)
     99             this.id=this.name=''
    100           },
    101           del(id){//根据Id删除数据
    102             //分析:
    103             //1.如何根据Id,找到要删除这一项的索引
    104             //2.如果找到索引了,直接调用数组的splice方法
    105 
    106            /* this.list.some((item,i)=>{
    107               if(item.id==id){
    108                 this.list.splice(i,1)
    109                 //在数组的some方法中,如果return true,就会立即终止这个数组的后续循环
    110                 return true;
    111               }
    112             })*/
    113 
    114            var index = this.list.findIndex(item=>{
    115               if(item.id==id){
    116                 return true;
    117               }
    118             })
    119             // console.log(index)
    120             this.list.splice(index,1)
    121           },
    122 
    123           search(keywords){//根据关键字,进行数据的搜索
    124           /*  var newList=[]
    125             this.list.forEach(item=>{
    126               if(item.name.indexOf(keywords)!=-1){
    127                 newList.push(item)
    128               }
    129             })
    130             return newList*/
    131             //注意:forEach some filter findIndex   这些都属于数组的新方法,
    132             //都会对数组中的每一项,进行遍历,执行相关操作;
    133            return this.list.filter(item=>{
    134               // if(item.name.indexOf(keywords)!=-1)
    135               //注意:在ES6中,为字符串提供了一个新方法,叫做String.prototype.includes('要包含的字符串')
    136               //如果包含,则返回true,否则返回false
    137               //contains
    138               if(item.name.includes(keywords)){
    139                 return item
    140               }
    141             })
    142           }
    143         }
    144           });
    145       </script>
    146   </body>
    147 </html>
  • 相关阅读:
    SpringBoot启动过程中,候选类的过滤和加载
    Dubbo发布过程中,扩展点的加载
    Dubbo发布过程中,服务发布的实现
    Dubbo发布过程中,服务端调用过程
    SpringBean加载过程中,循环依赖的问题(一)
    Dubbo发布过程中,消费者的初始化过程
    DiscuzQ构建/发布小程序与H5前端
    Delphi写COM+的心得体会
    DBGridEh导出Excel等格式文件
    数据库直接通过bcp导出xml文件
  • 原文地址:https://www.cnblogs.com/songsongblue/p/10990501.html
Copyright © 2011-2022 走看看