关键字搜索品牌案例

(1)页面布局

    <div class="app">
<div class="panel panel-primary">
<div class="panel-heading">
<h2>品牌管理</h2>
</div>
<div class="panel-body form-inline">
<label for="">id:<input type="text" class="form-control" v-model="id"></label>
<label for="">品牌名称:<input type="text" class="form-control" v-model="name"></label>
<input type="button" value="添加" class="btn btn-primary" @click="add">
<label for="">搜索关键字:<input type="text" class="form-control" v-model="keywords"></label>
</div>
</div>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>id</th>
<th>品牌名称</th>
<th>添加时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="item in search(keywords)" :key="item.id">
<!-- 之前直接通过data中的数据直接渲染到页面,
现在自定义一个方法search,通过函数传参的形式把搜索到符合条件的数据渲染到页面 -->
<td v-text="item.id"></td>
<td v-text="item.name"></td>
<td>{{ item.time | timeFormat}}</td>
<td><a href="" @click.prevent="del(item.id)">删除</a></td>
</tr>
</tbody>
</table>
</div>

(2)搜索逻辑的实现

<script>
var vm= new Vue({ //创建vue实例
el:'.app',
data:{
arr:[
{'id':1,'name':'iPhone','time':new Date()},
{'id':2,'name':'华为','time':new Date()},
{'id':3,'name':'OPPO','time':new Date()}
], //创建一些初始数据与格式
id:'',
name:'',
keywords:'' //初始化参数keywords为空
},
methods:{
          search(keywords){
// var newList = []
// this.arr.forEach(item => {
// if(item.name.indexOf(keywords) != -1) {//如果包含keywords,增加到数组中,渲染到页面
// newList.push(item)
// }
// });
// return newList
return this.arr.filter(item=>{
if(item.name.indexOf(keywords)!= -1){
return item
}//filter方法来循环数组返回的是一个符合条件的新数组
})
}
    }
</script>

(3)小结与数组的新方法

定义一个search(keywords)方法,通过参数keywords绑定搜索框,接收关键字,然后通过循环遍历数组来判断符合条件的列表项,作为返回值渲染到页面中。

数组方法:some((item,i)=>{条件{return ture}}) 当条件成立时终止循环,i为查找到的索引,可通过return ture终止循环

     forEach() 方法用于调用数组的每个元素,并将元素传递给回调函数。

        注意: forEach() 对于空数组是不会执行回调函数的。

     filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。

        注意: filter() 不会对空数组进行检测。

        注意: filter() 不会改变原始数组。

     findIndex((item)=>{条件函数})查找符合条件的索引

        findIndex() 方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置。

        findIndex() 方法为数组中的每个元素都调用一次函数执行:

      • 当数组中的元素在测试条件时返回 true 时, findIndex() 返回符合条件的元素的索引位置,之后的值不会再调用执行函数。
      • 如果没有符合条件的元素返回 -1

        注意: findIndex() 对于空数组,函数是不会执行的。

        注意: findIndex() 并没有改变数组的原始值。

<div class="app">
<div class="panel panel-primary">
<div class="panel-heading">
<h2>品牌管理</h2>
</div>
<div class="panel-body form-inline">
<label for="">id:<input type="text" class="form-control" v-model="id"></label>
<label for="">品牌名称:<input type="text" class="form-control" v-model="name"></label>
<input type="button" value="添加" class="btn btn-primary" @click="add">
<label for="">搜索关键字:<input type="text" class="form-control" v-model="keywords"></label>
</div>
</div>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>id</th>
<th>品牌名称</th>
<th>添加时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="item in search(keywords)" :key="item.id">
<!-- 之前直接通过data中的数据直接渲染到页面,
现在自定义一个方法search,通过函数传参的形式把搜索到符合条件的数据渲染到页面 -->
<td v-text="item.id"></td>
<td v-text="item.name"></td>
<td>{{ item.time | timeFormat}}</td>
<td><a href="" @click.prevent="del(item.id)">删除</a></td>
</tr>
</tbody>
</table>
</div>

vue.js(11)--案例--关键字搜索列表的更多相关文章

  1. 【Vue.js实战案例】- Vue.js递归组件实现组织架构树和选人功能

    大家好!先上图看看本次案例的整体效果. 浪奔,浪流,万里涛涛江水永不休.如果在jq时代来实这个功能简直有些噩梦了,但是自从前端思想发展到现在的以MVVM为主流的大背景下,来实现一个这样繁杂的功能简直不 ...

  2. vue 音乐App QQ音乐搜索列表最新接口跨域设置

    在 webpack.dev.config.js中 'use strict' const utils = require('./utils') const webpack = require('webp ...

  3. Vue.js小案例(2)

    即时搜索 这个例子主要应用了vue.js的自定义过滤器,可以通过Vue.filter()注册一个全局过滤器,具体用法可以参考这里,vue.js也提供了一些内置过滤器. CSS代码: [v-cloak] ...

  4. Vue.js小案例(1)

    数据绑定 数据绑定是vue.js的基础.本例中就是利用了vue.js的v-model指令在表单元素上创建双向数据绑定. <!--这是我们的View--> <div id=" ...

  5. Vue(九)小案例 - 百度搜索列表(跨域)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Vue.js小案例、生命周期函数及axios的使用

    一.调色框小案例: 随着三个滑动框的变化,颜色框的颜色随之改变 1.1.实例代码 <!DOCTYPE html> <html lang="en" xmlns:v- ...

  7. js 模拟百度关键字搜索与跳转

    测试效果: css样式: ul{ display:none; } html代码: <input type="text" id="text" /> & ...

  8. js 生成树以及关键字搜索生成树

    function main(keywords,data){ function fn(arr){ var flag = false; for(var i = 0;i <arr.length;i++ ...

  9. vue.js组件化开发实践

    前言 公司目前制作一个H5活动,特别是有一定统一结构的活动,都要码一个重复的轮子.后来接到一个基于模板的活动设计系统的需求,便有了下面的内容.借油开车. 组件化 需求一到,接就是怎么实现,技术选型自然 ...

随机推荐

  1. 算法题常见的BUG错误(总结)

    1. 快排的partition if(l >= r) return ; int i = l, j = r; int tmp = v[i]; while(i < j) { while(i & ...

  2. 大数据与mysql

    mysql优化:……

  3. 一本通&&洛谷——P1120 小木棍 [数据加强版]——题解

    题目传送 一道特别毒瘤能提醒人不要忘记剪枝的题. 首先不要忘了管理员的话.忘把长度大于50的木棍过滤掉真的坑了不少人(包括我). 显然是一道DFS题 .考虑剪枝. 找找搜索要面临的维度.状态:原始木棍 ...

  4. [CSP-S模拟测试]:梦境(贪心+小根堆)

    题目描述 智者奥尔曼曾说过:有缘的人即使相隔海角天涯,也会在梦境中相遇. $IcePrince\text{_}1968$和$IcePrincess\text{_}1968$便是如此.有一天$IcePr ...

  5. spring MVC junit单元测试 各test之间共享变量

    使用静态变量   private static String iPSetCode=null;

  6. Spring Boot 集成 JPA 的步骤

    Spring Boot 集成 JPA 的步骤 配置依赖 compile group: 'org.springframework.boot', name: 'spring-boot-starter-da ...

  7. SpringBoot项目的前端+thymeleaf模板引擎

    SpringBoot项目创建之后,后台的框架是SpringMVC.但前端的resource和template目录都是空的.这个时候需要创建前台页面. 习惯上,我们会创建JSP,但是,SpringBoo ...

  8. How to run a function when the page is loaded?

    How to run a function when the page is loaded? window.onload = codeAddress; should work - here's a d ...

  9. Python 列表反转显示方法

    第一种,使用reversed 函数,reversed返回的结果是一个反转的迭代器,我们需要对其进行 list 转换 listNode = [1,2,3,4,5] newList = list(reve ...

  10. ps 等程序的选项的三种风格

    unix options bsd options gnu long options unix options, which may be grouped and must be preceded by ...