How 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.)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue测试2</title> <script type="text/javascript" src="vue.min.js"></script> <style type="text/css"> *{ 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> </head> <body> <p id="box"> <p class="search"> <input type="text" class="searchBox" v-model="searchVal"> <button class="btn">搜 索</button> </p> <table class="goodsheet"> <tr> <th>商品名</th> <th>单价 <span @click="orderFn('price', false)">↑</span> <span @click="orderFn('price', true)">↓</span> </th> <th>销量 <span @click="orderFn('sales', false)">↑</span> <span @click="orderFn('sales', true)">↓</span> </th> </tr> <tr v-for='(item, key) in list'> <td>{{item.name}}</td> <td>{{item.price}}</td> <td>{{item.sales}}万</td> </tr> </table> </p> <script type="text/javascript"> var myVueTest = new Vue({ el:'#box', 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:'', //默认输入为空 letter:'', //默认不排序 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 != ''){ 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> </body> </html>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

PHP Regular Expressions: Exact Matching and Exclusion Fuzzy inclusion regular expressions are a powerful text matching tool that can help programmers perform efficient search, replacement and filtering when processing text. In PHP, regular expressions are also widely used in string processing and data matching. This article will focus on how to perform exact matching and exclude fuzzy inclusion operations in PHP, and will illustrate it with specific code examples. Exact match Exact match means matching only strings that meet the exact condition, not any variations or extra words.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

PHP String Matching Tips: Avoid Ambiguous Included Expressions In PHP development, string matching is a common task, usually used to find specific text content or to verify the format of input. However, sometimes we need to avoid using ambiguous inclusion expressions to ensure match accuracy. This article will introduce some techniques to avoid ambiguous inclusion expressions when doing string matching in PHP, and provide specific code examples. Use preg_match() function for exact matching In PHP, you can use preg_mat
