This article mainly introduces the vue2 front-end search implementation example. Now I will share it with you and give you a reference.
When the project data is small, small things like searching have to be left to our front-end. Important statement, suitable for small projects! ! ! ! !
In fact, the principle is very simple. The small demo is to search for city names or search according to rankings.
<p> <input type="text" v-model="name" placeholder="点击搜索按钮筛选" /> <input type="button" @click="search" /> </p>
<table> <tbody> <tr v-for="item in listt0"> <td> <a v-text="item.sort"></a> </td> <td> <a v-text="item.City"></a> </td> <td> <a :style="{'color':item.sort<=10?'#f2972e':''}" v-cloak>{{item.Data | two}}</a> </td> <td><span v-text="item.Good"></span></td> </tr> </tbody> </table>
After the page layout is successful, it is time to configure the js. The first is data initialization.
data:{ list0:[],//原始 listt0:[],//处理过的 name:'',//搜索框内容 },
Next, get the background data. The background data must be passed to the front end all at once. You know the reason.
created:function(){ //这获取数据且list0以及listt0都为获取到的数据 },
The implementation of search, if it is a number, search according to sort, if it is not a number, search according to city. A simple search and you're done.
methods:{ search:function(){//搜索 var _this=this; var tab=this['list0']; if(this.name){ _this['listt0']=[]; if(!isNaN(parseInt(_this.name))) { for(i in tab) { if(tab[i].sort == parseInt(_this.name)) { _this['listt0'].push(tab[i]); }; }; } else { for(i in tab) { if(tab[i].City.indexOf(_this.name) >= 0) { _this['listt0'].push(tab[i]); }; }; }; }else{ alert('请输入筛选条件!') }; } },
Little knowledge points:
1. :style="{'color':item.sort<=10?'#f2972e ':''}" :style sets the text color of the top 10.
2. !isNaN(parseInt(_this.name)) Determines whether the input is a number or text. If there are numbers, it will search according to the numbers.
3. Filter two
filters: {//保留两位小数点 two : function(value){ if (!value) { return ''}; return value.toFixed(2); } }
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to learn the process and child_process modules in node (detailed tutorial)
By using Vue2.0 Implement http request and loading display in
Method of cross-domain request through jQuery JSONP (detailed tutorial)
The above is the detailed content of How to use front-end search in vue2?. For more information, please follow other related articles on the PHP Chinese website!