关键字搜索品牌案例

(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. sh_02_第2个Python程序

    sh_02_第2个Python程序 print("hello")

  2. JS onclick中this用法

    当在dom元素中使用onclick绑定事件的时候,可以使用this来指向该元素对象. 打印输出的内容为: 所以可以通过该this对象来获取子元素 //通过element获取该对象下的一个audio标签 ...

  3. php服务器环境安装及项目搭建

    2安装运行环境及搭建项目2.1安装apache及测试1)安装apacheyum install httpd 启动apache systemctl start httpd.service 查看运行状态 ...

  4. RHEL6 kernel bug在hadoop上的测试

    最近给hadoop集群升级了RHEL6,发现性能比之前的差了不少.发现淘宝内核组发现并解决了这个问题 原文链接:http://blog.donghao.org/2013/03/20/hadoop%E9 ...

  5. RabbitMQ + Springboot +“Hello Word”

    https://www.rabbitmq.com/getstarted.html 官网文档 我们将呼叫我们的消息发布者(发送者)发送和我们的消息消费者(接收者) Recv.发布者将连接到RabbitM ...

  6. 2018-2019-2 20175214 实验四《Android程序设计》实验报告

    实验四<Android程序设计>实验报告 一.前期准备 安装Android Studio 参考http://www.cnblogs.com/rocedu/p/6371315.html#SE ...

  7. Linux超全实用指令大全

    参考 Linux超全实用指令大全

  8. 逻辑回归模型(Logistic Regression, LR)--分类

    逻辑回归(Logistic Regression, LR)模型其实仅在线性回归的基础上,套用了一个逻辑函数,但也就由于这个逻辑函数,使得逻辑回归模型成为了机器学习领域一颗耀眼的明星,更是计算广告学的核 ...

  9. ORACLE内存管理之ASMM AMM

    ORACLE ASMM ORACLE AMM ASMM转换至AMM AMM转换至ASMM

  10. 永久关闭Linux的防火墙

    重启网络服务,加载网卡配置文件systemctl restart network 清空防火墙规则iptables -F 关闭selinux防火墙vi /etc/selinux/config修改如下配置 ...