欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue2 前端搜索實(shí)現(xiàn)示例

 更新時(shí)間:2018年02月26日 14:59:29   作者:yue朔  
本篇文章主要介紹了vue2 前端搜索實(shí)現(xiàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

項(xiàng)目數(shù)據(jù)少的時(shí)候,搜索這樣的小事情就要交給咱們前端來(lái)做了,重要聲明,適用于小項(xiàng)目?。。。?!

其實(shí)原理很簡(jiǎn)單,小demo是做搜索市區(qū)名稱或者按照排名搜索。

<div>
   <input type="text" v-model="name" placeholder="點(diǎn)擊搜索按鈕篩選" />
   <input type="button" @click="search" />
</div>
<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>

頁(yè)面布局成功之后,就是要做js配置了,首先是data初始化。

data:{
  list0:[],//原始
  listt0:[],//處理過(guò)的
  name:'',//搜索框內(nèi)容
}, 

接下來(lái)獲取后臺(tái)數(shù)據(jù),后臺(tái)數(shù)據(jù)必須是一次性傳遞給前端,原因你懂的。

created:function(){
  //這獲取數(shù)據(jù)且list0以及l(fā)istt0都為獲取到的數(shù)據(jù)
},

搜索的實(shí)現(xiàn),判斷如果是數(shù)字就按照sort搜索,不是數(shù)字則按照city搜索。一個(gè)簡(jiǎn)單的搜索就完成了。

        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('請(qǐng)輸入篩選條件!')
             };
           }
        },  

小知識(shí)點(diǎn):

  1. :style="{'color':item.sort<=10?'#f2972e':''}" :style設(shè)置前10名的文字顏色。

  2. !isNaN(parseInt(_this.name)) 判斷輸入的是數(shù)字還是文字,如果有數(shù)字就會(huì)按照數(shù)字搜索。

    3.過(guò)濾器two

     filters: {//保留兩位小數(shù)點(diǎn)
          two : function(value){
            if (!value) { return ''};
            return value.toFixed(2);
          }
        }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論