vue之实现列表的渲染,增加,删除和查询

vue之实现列表的渲染,增加,删除和查询

1.列表的渲染

首先创建 Vue 实例,得到 ViewModel,然后在列表中渲染数据

 <tbody>
        <tr v-for="item in list" :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>
 var vm = new Vue({
      el: '#app',
      data: {
        list: [
          { id: 1, name: '小可爱', ctime: new Date() },
          { id: 2, name: '大可爱', ctime: new Date() },
		  { id: 3, name: '哈哈哈', ctime: new Date() },
		  { id: 4, name: '三人行', ctime: new Date() }
        ]
      }

在这里插入图片描述

2.实现添加功能

在Vue中,使用事件绑定机制,为元素指定处理函数

        <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-success" @click="add()">

添加的方法
1. 获取到 id 和 name ,直接从 data 上面获取
2. 组织出一个对象
3. 把这个对象,调用 数组的 相关方法,添加到 当前 data 上的 list 中

 data: {
       id: '',
       name: ''
      }
add() { 
         var car = { id: this.id, name: this.name, ctime: new Date() }
         this.list.push(car)
         this.id = this.name = ''
       },

注意:在Vue中,已经实现了数据的双向绑定,每当修改了 data 中的数据,Vue会默认监听到数据的改动,自动把最新的数据,应用到页面上;
在这里插入图片描述

3.实现删除功能

在删除按钮上绑定事件机制

    <td>
      <a href="" @click.prevent="del(item.id)">删除</a>
    </td>

根据Id删除数据
1. 根据Id,找到要删除这一项的索引
2. 如果找到索引了,直接调用 数组的 splice 方法

del(id) {
          var index = this.list.findIndex(item => {
            if (item.id == id) {
            // 在 数组的 some 方法中,如果 return true,就会立即终止这个数组的后续循环
              return true;
            }
          })
          this.list.splice(index, 1)
        },

在这里插入图片描述

4.实现查询功能

添加绑定机制关键词keywords

 <label>
  搜索名称关键字:
  <input type="text" class="form-control" v-model="keywords">
</label>
 <!-- 之前, v-for 中的数据,都是直接从 data 上的list中直接渲染过来的 -->
 
 <!-- 现在, 我们自定义了一个 search 方法,同时,把 所有的关键字,通过传参的形式,传递给了 search 方法 -->
 
<!-- 在 search 方法内部,通过 执行 for 循环, 把所有符合 搜索关键字的数据,保存到 一个新数组中,返回 -->

<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>

在data中设置搜索关键词,根据关键字,进行数据的搜索

keywords: '', // 搜索的关键字

运用 filter 会对数组中的每一项,进行遍历,执行相关的操作;

 search(keywords) { 
          return this.list.filter(item => {
            // ES6中,为字符串提供了一个新方法,叫做  String.prototype.includes('要包含的字符串')
            //  如果包含,则返回 true ,否则返回 false
            if (item.name.includes(keywords)) {
              return item
            }
          })
        }
      }

在这里插入图片描述

代码整合

  <div id="app">
    <div class="panel panel-success">
      <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-success" @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>序号</th>
          <th>名字</th>
          <th>时间</th>
          <th>删除操作</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() },
		  { id: 4, name: '三人行', ctime: new Date() }
        ]
      },
      methods: {
        add() { 
          var car = { id: this.id, name: this.name, ctime: new Date() }
          this.list.push(car)
          this.id = this.name = ''
        },
        del(id) {
          var index = this.list.findIndex(item => {
            if (item.id == id) {
              return true;
            }
          })
          this.list.splice(index, 1)
        },
        search(keywords) { 
          return this.list.filter(item => {
            if (item.name.includes(keywords)) {
              return item
            }
          })
        }
      }
    });
  </script>
发布了87 篇原创文章 · 获赞 198 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/low666/article/details/104684073