溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Vuejs中怎么實(shí)現(xiàn)一個(gè)搜索匹配功能

發(fā)布時(shí)間:2021-07-09 14:55:00 來源:億速云 閱讀:152 作者:Leah 欄目:web開發(fā)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)Vuejs中怎么實(shí)現(xiàn)一個(gè)搜索匹配功能,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Vue測(cè)試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>
    <div id="box">
      <div class="search">
        <input type="text" class="searchBox" v-model="searchVal">
        <button class="btn">搜 索</button>
      </div>
      <table class="goodsheet">
        <tr>
          <th>商品名</th>
          <th>單價(jià)
            <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>
    </div>
    <script type="text/javascript">
      var myVueTest = new Vue({
        el:'#box',
        data:{
          goodsList:[
            //假數(shù)據(jù)
            {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:'',  //默認(rèn)輸入為空
          letter:'',    //默認(rèn)不排序
          original:false  //默認(rèn)從小到大排列
        },
        methods:{
          orderFn(letter,original){
            this.letter = letter;    //排序字段 price or sales 
            this.original = original;  //排序方式 up or down
          }
        },
        //通過計(jì)算屬性過濾數(shù)據(jù)
        computed:{
          list: function(){
            var _this = this;
            //邏輯-->根據(jù)input的value值篩選goodsList中的數(shù)據(jù)
            var arrByZM = [];//聲明一個(gè)空數(shù)組來存放數(shù)據(jù)
            for (var i=0;i<this.goodsList.length;i++){
              //for循環(huán)數(shù)據(jù)中的每一項(xiàng)(根據(jù)name值)
              if(this.goodsList[i].name.search(this.searchVal) != -1){
                //判斷輸入框中的值是否可以匹配到數(shù)據(jù),如果匹配成功
                arrByZM.push(this.goodsList[i]);
                //向空數(shù)組中添加數(shù)據(jù)
              }
            }
            //邏輯-->升序降序排列 false: 默認(rèn)從小到大 true:默認(rèn)從大到小
            //判斷,如果要letter不為空,說明要進(jìn)行排序
            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];
                }
              });
            }
            //一定要記得返回篩選后的數(shù)據(jù)
            return arrByZM;
          }
        }
      });
    </script>
  </body>
</html>

上述就是小編為大家分享的Vuejs中怎么實(shí)現(xiàn)一個(gè)搜索匹配功能了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI