vue实现添加,删除,搜索功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!--new 的 vue 实例会控制这个元素中的所有内容,也是MVVM中的 V -->
    <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-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>
            <!--自定义 search 方法,把关键字通过传参的形式,传递给 search方法,在 search 方法内部,通过 执行 for循环,把所有符合 搜索关键字的数据,保存到一个新数据中,返回 -->
            <tr v-for="item in search(keywords)" :key="item.id">
                <td>{{ item.id }}</td>
                <td>{{ item.name }}</td>
                <td>{{ item.ctime }}</td>
                <td>
                    <a href="" @click.prevent="del(item.id)">删除</a>
                </td>
            </tr>
            </tbody>
        </table>
    </div>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<script>
    //创建一个vue的实例,也是MVVM中的 VM调度者,里面可传配置对象
         var vm = new Vue({
             el:"#app",
             //data指MVVM中的 M ,用来存 el 中用的数据
             data:{
                 id:"",
                 name:"",
                 keywords:"",
                 list:[
                     {id:1, name:'名字1', ctime:new Date()},
                     {id:2, name:'名字2', ctime:new Date()}
                 ]
             },
             methods:{
                 add:function(){
                     var abc = {id:this.id, name:this.name, ctime:new Date()}
                     this.list.push(abc);
                     this.id = this.name = ""
                 },
     //        删除事件逻辑:
     //          1.删除的时候一般是根据 id 来进行删除的
     //          2.给个点击事件,取消默认事件,因为要通过id来删,所以将id传个参    @click.prevent="del(item.id)"
     //          3.定义 del 的方法(函数,点击事件)
     //          4.函数中 传参id,根据id删除数据
     //          5.分析:
     //              i:根据id,找到当前的索引
     //              ii:找到索引了,根据 数组的 splice 方法进行删除
     //          6.注意: forEach  some  filter  findIndex     这些都属于数组的新方法
     //              forEach : 没有办法终止
     //              some : 通过 return:true 可以终止
     //              filter : 过滤,符合条件的
     //              findIndex : 找到数组对应的索引,专门查找索引
     //          7.之所以用到some ,是因为找到要删除的 id后,就终止了循环
     //              some和findIndex的区别:some方法中可以做任何事情,但是findIndex是专门用来查索引的
                 del:function(id){
     //              一种写法:
     //              this.list.some((item,i)=>{
     //                 if(item.id == id){
     //                   .splice(2,3) 从索引为 i(或者2) 开始,删除 1(或者3) 个
     //                   this.list.splice(i,1)
     //                   终止循环
     //                   return true;
     //                 }
     //              });
     //              另一种写法:
                     var index = this.list.findIndex(item =>{
                         if(item.id == id){
                             return true;
                         }
                     });
                     this.list.splice(index,1)
                  },
 
                 search:function(keywords){
                     var newList = []
                     this.list.forEach(item=>{
                       if(item.name.indexOf(keywords) != -1){
                         newList.push(item)
                      }
                     })
                    return newList;
 
 
         //          return this.list.filter(item =>{
         //            if(item.name.indexOf(keywords) != -1)
         //              ES6中,为字符串提供了一个新的方法,String.prototype.includes('要包含的字符串'),如果包含,则返回 true ,否则返回 false
         //              if(item.name.includes(keywords)){
         //                 return  item
         //              }
         //           })
               }
             }
          })
     </script>

  

posted @   “好”久不见  阅读(3630)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示