search
HomeWeb Front-endH5 TutorialHow Vuejs implements search matching function

This time I will show you how Vuejs implements the search matching function. What are the precautions for Vuejs to implement the search matching function. The following is a practical case, let's take a look.

I have been looking at vue recently. I checked a lot of information, read a lot of documents and blogs, and probably had a partial understanding of it. Then I used the knowledge I understood to write a simple search and matching function.

It probably looks like this:

The data is all fake

Code part

(Note that I am referencing the local vue.min.js file, please pay attention to the file path.)

nbsp;html>

  
    <meta>
    <title>Vue测试2</title>
    <script></script>
    <style>
      *{
        padding: 0;
        margin: 0;
        font-size: 14px;
        font-family: "微软雅黑";
      }
      #box{
        width: 500px;
        height: auto;
        border: 1px solid #ccc;
        margin: 50px auto;
        padding: 10px;
      }
      .search{
        width: 480px;
        height: 100px;
        text-align: center;
      }
      .searchBox{
        width: 230px;
        height: 40px;
        outline: none;
        text-indent: 10px;
        margin-right: 20px;
      }
      .btn{
        width: 100px;
        height: 50px;
        cursor: pointer;
        font-size: 18px;
      }
      .goodsheet{
        width: 500px;
        height: auto;
        border: 1px solid #eee;
      }
      .goodsheet tr td,
      .goodsheet tr th{
        width: 33%;
        border: 1px solid #eee;
        padding: 5px 10px;
        text-align: left;
      }
      .goodsheet tr th span{
        background: #ff9900;
        padding: 0 6px;
        color: #fff;
        cursor: pointer;
      }
    </style>
  
  
    <p>
      </p><p>
        <input>
        <button>搜 索</button>
      </p>
      
                                                                                                            
商品名单价                                     销量                                     
{{item.name}}{{item.price}}{{item.sales}}万
         <script> var myVueTest = new Vue({ el:&#39;#box&#39;, data:{ goodsList:[ //假数据 {name:"三星Galaxy Note8",price:5200,sales:2.6}, {name:"iphone5s",price:2500,sales:2.2}, {name:"iphone6",price:2800,sales:1.6}, {name:"iphone6s",price:3200,sales:2.9}, {name:"iphone7",price:3800,sales:12.6}, {name:"iphone7plus",price:4200,sales:2.1}, {name:"iphone8",price:5500,sales:10.6}, {name:"华为",price:4600,sales:7.6}, {name:"小米",price:1200,sales:32.6}, {name:"OPPOR11",price:3000,sales:1.2}, {name:"vivoX20",price:3250,sales:2.9} ], searchVal:&#39;&#39;, //默认输入为空 letter:&#39;&#39;, //默认不排序 original:false //默认从小到大排列 }, methods:{ orderFn(letter,original){ this.letter = letter; //排序字段 price or sales this.original = original; //排序方式 up or down } }, //通过计算属性过滤数据 computed:{ list: function(){ var _this = this; //逻辑-->根据input的value值筛选goodsList中的数据 var arrByZM = [];//声明一个空数组来存放数据 for (var i=0;i<this.goodsList.length;i++){ //for循环数据中的每一项(根据name值) if(this.goodsList[i].name.search(this.searchVal) != -1){ //判断输入框中的值是否可以匹配到数据,如果匹配成功 arrByZM.push(this.goodsList[i]); //向空数组中添加数据 } } //逻辑-->升序降序排列 false: 默认从小到大 true:默认从大到小 //判断,如果要letter不为空,说明要进行排序 if(this.letter != &#39;&#39;){ arrByZM.sort(function( a , b){ if(_this.original){ return b[_this.letter] - a[_this.letter]; }else{ return a[_this.letter] - b[_this.letter]; } }); } //一定要记得返回筛选后的数据 return arrByZM; } } }); </script>   

In fact, the core algorithm is still written in native JS. Vue provides a very powerful data binding method, but if you only know the vue framework and do not have your own core ideas, it is still of little use. So the author said in the official document that we hope we have a certain JS foundation. I still think that no matter how many front-end frameworks there are, the most powerful one will always be native JS.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed graphic explanation of Vue.directive()

Module loader using javascript

The above is the detailed content of How Vuejs implements search matching function. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)