Home > Web Front-end > JS Tutorial > How to use front-end search in vue2?

How to use front-end search in vue2?

亚连
Release: 2018-06-04 14:13:32
Original
1881 people have browsed it

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>
Copy after login

<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="{&#39;color&#39;:item.sort<=10?&#39;#f2972e&#39;:&#39;&#39;}" v-cloak>{{item.Data | two}}</a>
         </td>
          <td><span v-text="item.Good"></span></td>
          </tr>
        </tbody>
</table>
Copy after login

After the page layout is successful, it is time to configure the js. The first is data initialization.

data:{
  list0:[],//原始
  listt0:[],//处理过的
  name:&#39;&#39;,//搜索框内容
},
Copy after login

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都为获取到的数据
},
Copy after login

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[&#39;list0&#39;];
             if(this.name){                   
              _this[&#39;listt0&#39;]=[];           
               if(!isNaN(parseInt(_this.name))) {
                for(i in tab) {
                  if(tab[i].sort == parseInt(_this.name)) {
                    _this[&#39;listt0&#39;].push(tab[i]);
                  };
                };
              } else {
                for(i in tab) {
                  if(tab[i].City.indexOf(_this.name) >= 0) {
                    _this[&#39;listt0&#39;].push(tab[i]);
                  };
                };
              };
             }else{
               alert(&#39;请输入筛选条件!&#39;)
             };
           }
        },
Copy after login

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 &#39;&#39;};
            return value.toFixed(2);
          }
        }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template