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

Vue關(guān)鍵字搜索功能實戰(zhàn)小案例

 更新時間:2023年06月07日 14:35:25   作者:y17757773039  
在vue項目中,搜索功能是我們經(jīng)常需要使用的一個場景,下面這篇文章主要給大家介紹了關(guān)于Vue關(guān)鍵字搜索功能的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

這里介紹兩種方法:1、使用watch偵聽方法 2、computed計算屬性方法

頁面結(jié)果:

第一種

<body>
    <div id="app">
        <!-- 搜索框 -->
        <input type="text" v-model:value="keyword">
        <!-- 數(shù)據(jù),遍歷filPerson-->
        <ul>
            <li v-for="p in filPerson" :key="p.id">{{p.name}}-{{p.age}}</li>
        </ul>
    </div>

    <script>
        var vm=new Vue({
            el:'#app',
            data:{
                keyword:'',
                persons:[
                    {id:1,name:'知花實央',age:20},
                    {id:2,name:'虎杖悠仁',age:18},
                    {id:3,name:'切嗣',age:16},
                    {id:4,name:'衛(wèi)宮切嗣',age:33}
                ],
                filPerson:[]
            },
            //第一種寫法
            watch:{
                keyword:{
                     //初始化,在生成vue時,先執(zhí)行一遍handler
                    immediate:true,//作用:剛開始filPerson為空,所以要先給filPerson賦一次值
                    handler(val){
                        //person中包含val數(shù)據(jù),賦值給filPerson
                        this.filPerson=this.persons.filter((p)=>{
                            return p.name.indexOf(val)!=-1
                        })
                    }
                }
            }

            //第二種寫法
            // computed:{
            //     filPerson(){
            //         return this.persons.filter((p)=>{
            //             return p.name.indexOf(this.keyword)!=-1
            //         })
            //     }
            // }
        })
    </script>
</body>

第二種

相較于watch寫法,computed寫法看上去更加簡潔,比如:

1、computed自身就是一種計算屬性,不必再去data中新建一個屬性。

2、計算屬性實時更新,不用像watch方法,新建的filPerson初始值為空,還需要手動開啟設(shè)置immediate=true初始化,令handler在vue實例創(chuàng)建后先運行一次,賦予初始值。

<body>
    <div id="app">
        <!-- 搜索框 -->
        <input type="text" v-model:value="keyword">
        <!-- 數(shù)據(jù) -->
        <ul>
            <li v-for="p in filPerson" :key="p.id">{{p.name}}-{{p.age}}</li>
        </ul>
    </div>
    <script>
        var vm=new Vue({
            el:'#app',
            data:{
                keyword:'',
                persons:[
                    {id:1,name:'知花實央',age:20},
                    {id:2,name:'虎杖悠仁',age:18},
                    {id:3,name:'切嗣',age:16},
                    {id:4,name:'衛(wèi)宮切嗣',age:33}
                ],
                // filPerson:[]
            },
            //第一種寫法
            // watch:{
            //     keyword:{
            //          //初始化,在生成vue時,先執(zhí)行一遍handler
            //         immediate:true,//作用:剛開始filPerson為空,所以要先給filPerson賦一次值
            //         handler(val){
            //             //過濾掉不包含keyword數(shù)據(jù),再賦值給filPerson
            //             this.filPerson=this.persons.filter((p)=>{
            //                 return p.name.indexOf(val)!=-1
            //             })
            //         }
            //     }
            // }
            //第二種寫法
            computed:{
                filPerson(){
                    return this.persons.filter((p)=>{
                        return p.name.indexOf(this.keyword)!=-1
                    })
                }
            }
        })
    </script>
</body>

其實watch方法和computed方法各有優(yōu)劣,computed方法自己就是一種計算屬性,很多時候直接給自己賦值,省去很多代碼;但是watch方法能夠做到跟多的細節(jié)操作,甚至computed能實現(xiàn)的,它都能實現(xiàn),還能實現(xiàn)更多computed實現(xiàn)不了的細節(jié)。

補充:vue頁面實現(xiàn)文本關(guān)鍵字檢索,關(guān)鍵字高亮顯示及定位功能

實現(xiàn)原理

  • 將相應(yīng)的關(guān)鍵字用標簽包裹,添加相應(yīng)的樣式,
  • 使用 dom.scrollIntoView() 方法實現(xiàn)定位功能

代碼

html部分

<template>
    <div class="serach-box">                        
      <el-input                        
      placeholder="請輸入關(guān)鍵字"                        
      prefix-icon="el-icon-search"                        
      v-model="inputLogValue"                        
      clearable                        
      size="small"                        
      @keyup.enter.native='clickSearch'>                        
      </el-input>                        
      <el-button type="primary" size="small" icon="el-icon-search" @click="clickSearch"></el-button>                        
    <div class="searchRight">                            
       <span class="item" style='margin-right:5px'>{{curIndex}}/{{matchCount}}</span>                                                           
       <el-tooltip class="item" effect="dark" content="上一個" placement="top">                                                
         <el-button @click="searchLast" icon="el-icon-arrow-up" circle type="text" size="mini" :disabled="matchCount==0"></el-button>                            
       </el-tooltip>                            
       <el-tooltip class="item" effect="dark" content="下一個" placement="top">                                                              
         <el-button @click="searchNext" icon="el-icon-arrow-down" circle type="text" size="mini" :disabled="matchCount==0"></el-button>                            
       </el-tooltip>                         
     </div>                    
   </div>
   <div class="log_content" ref='log_content' v-if="searchStatus" v-html="contentShow"></div>
<template>
<style lang="scss" scoped>
  .serach-box{        
    display: flex;        
    .el-input{            
      width: 300px;            
      .el-input__inner{                
        border-right: none;            
      }        
    }        
    .el-button{            
      border-radius: 0;            
      height: 32px;            
      box-sizing: border-box;        
    }        
    .searchRight{            
      height: 32px;            
      line-height: 32px;            
      padding: 0 10px;            
      background: #eee;            
      font-size: 14px;            
      color: #666;           
        .el-button+.el-button{                
          margin-left: 0;            
        }        
      }    
    }
    .log_content{            
      width: 100%;            
      height: calc(100% - 40px);            
      line-height: 20px;            
      background-color: #333333;            
      color: #f0f0f0;            
      font-size: 13px;            
      overflow: overlay;            
      padding: 0 20px ;            
      overflow-x:hidden;            
      word-wrap: break-word;            
      white-space: pre-wrap;            
      box-sizing: border-box;            
      padding-bottom: 20px;
      &::-webkit-scrollbar {                
        width: 10px;                
        height: 7px;                
        background-color: rgba(245, 245, 245, 0.1);                
        cursor: pointer;                
        z-index: 10;            
      }
      &::-webkit-scrollbar-thumb {                
        height: 20px;                
        border-radius: 10px;                
        background-color: #dddee0;                
        cursor: pointer;                
        z-index: 10;
      }
      &::-webkit-scrollbar-thumb:hover {                
        cursor: pointer;                
        background-color: #c7c9cc;            
      }        
   }
</style>

js部分

data() {        
   return {            
    logMessage:'',            
    inputLogValue:'',            
    lightIndex: 0,            
    curIndex:0,            
    matchCount: 0,            
    highlightStyle:'background: #ffff00;color:red;',            
    currentStyle:'background: #ff9632',
  }
},
watch:{
  inputLogValue: {            
    immediate: true,            
    handler (val) {                
      if(val==''){                            
        this.lightIndex=0                    
        this.curIndex=0                    
        this.matchCount=0                    
        this.searchStatus=0                
      }            
    }        
  },
},
methods:{
  scrollTo (index) {            
    this.$nextTick(() => {                
      let list = this.$el.querySelectorAll(`.search-hightlight`)                
      if (list[index]) {                    
         this.lightIndex = index                     
         list[index].scrollIntoView()                
      }            
    })       
  },        
  searchNext () {            
    this.$nextTick(() => {                
      let idx                
      if(this.lightIndex >= this.matchCount-1){                    
        this.lightIndex = 0                    
        idx = 0                
      }else{                    
        idx = this.lightIndex + 1                
      }                
        this.scrollTo(idx)            
      })        
    },        
  searchLast () {            
    this.$nextTick(() => {                
      let idx                
      if(this.lightIndex <= 0){                    
        idx = this.matchCount - 1                    
        this.lightIndex = this.matchCount - 1                
      }else{                    
        idx = this.lightIndex - 1                
      }                
      this.scrollTo(idx)            
    })        
  },        
  getMatchCount () {            
    this.$nextTick(() => {                
      let list = this.$el.querySelectorAll(`.search-hightlight`)                
      this.matchCount = list.length            
    })        
  },
  clickSearch(){            
    let reg = new RegExp(this.inputLogValue, 'ig')            
    let stringList = this.logMessage.split(reg)            
    if (stringList.length) {                
      this.searchStatus = 1                
      this.contentShow = ''                
      this.lightIndex = 0                
      for (let i = 0; i < stringList.length - 1; i++) {                    
        let style = i === this.lightIndex ? this.currentStyle : this.highlightStyle                       
        this.contentShow += `${stringList[i]}<font style="${style}" class='search-hightlight'>${this.inputLogValue}</font>`                
      }                
      this.contentShow += stringList[stringList.length - 1]                
      this.getMatchCount()                
      this.scrollTo(this.lightIndex)            
    }        
  },
}            

總結(jié)

到此這篇關(guān)于Vue關(guān)鍵字搜索功能實戰(zhàn)案例的文章就介紹到這了,更多相關(guān)Vue關(guān)鍵字搜索內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 在Vue3中使用provide和inject進行依賴注入的代碼詳解

    在Vue3中使用provide和inject進行依賴注入的代碼詳解

    在現(xiàn)代前端開發(fā)中,Vue.js已經(jīng)成為了非常流行的框架之一,它提供了極大的靈活性和可維護性,今天我們要探討的是Vue?3中的provide和inject功能,這是一種用于在組件樹中進行依賴注入的方法,需要的朋友可以參考下
    2024-06-06
  • vue中狀態(tài)管理器Pinia的用法詳解

    vue中狀態(tài)管理器Pinia的用法詳解

    Pinia?是?Vue.js?的輕量級狀態(tài)管理庫,最近很受歡迎,它使用?Vue?3?中的新反應(yīng)系統(tǒng)來構(gòu)建一個直觀且完全類型化的狀態(tài)管理庫,下面就跟隨小編一起來學(xué)習(xí)一下它的具體使用吧
    2023-10-10
  • VUE中this.$router.push點了后hash地址改變了頁面不跳轉(zhuǎn)

    VUE中this.$router.push點了后hash地址改變了頁面不跳轉(zhuǎn)

    本文主要介紹了VUE中this.$router.push點了后hash地址改變了頁面不跳轉(zhuǎn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-11-11
  • vue中v-cloak的作用和原理解析

    vue中v-cloak的作用和原理解析

    v-cloak原理是先通過樣式隱藏內(nèi)容,然后在內(nèi)存中進行值的替換,將替換的內(nèi)容再反饋給界面,數(shù)據(jù)渲染完場之后,v-cloak 屬性會被自動去除,本文詳細介紹vue中v-cloak的作用和原理解析,感興趣的朋友一起看看吧
    2023-09-09
  • vue動態(tài)設(shè)置img的src路徑實例

    vue動態(tài)設(shè)置img的src路徑實例

    今天小編就為大家分享一篇vue動態(tài)設(shè)置img的src路徑實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • VUE的數(shù)據(jù)代理與事件詳解

    VUE的數(shù)據(jù)代理與事件詳解

    這篇文章主要為大家介紹了VUE的數(shù)據(jù)代理與事件,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11
  • vue實現(xiàn)網(wǎng)頁語言國際化切換

    vue實現(xiàn)網(wǎng)頁語言國際化切換

    這篇文章介紹了vue實現(xiàn)網(wǎng)頁語言國際化切換的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-11-11
  • Vue.js學(xué)習(xí)教程之列表渲染詳解

    Vue.js學(xué)習(xí)教程之列表渲染詳解

    這篇文章主要給大家介紹了關(guān)于Vue.js列表渲染的相關(guān)資料,文中介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2017-05-05
  • 利用Vue實現(xiàn)將圖片轉(zhuǎn)換為Base64編碼的方法

    利用Vue實現(xiàn)將圖片轉(zhuǎn)換為Base64編碼的方法

    這篇文章主要介紹了利用Vue實現(xiàn)將圖片轉(zhuǎn)換為Base64編碼的方法,Base64是一種編碼方式,用于將二進制數(shù)據(jù)轉(zhuǎn)換成64個基于ASCII的可打印字符,這種編碼可嵌入圖像到HTML或CSS中,減少加載時間,解決跨域問題,并支持離線應(yīng)用開發(fā),需要的朋友可以參考下
    2024-10-10
  • vuex刷新之后數(shù)據(jù)丟失,數(shù)據(jù)持久化,vuex-persistedstate問題

    vuex刷新之后數(shù)據(jù)丟失,數(shù)據(jù)持久化,vuex-persistedstate問題

    這篇文章主要介紹了vuex刷新之后數(shù)據(jù)丟失,數(shù)據(jù)持久化,vuex-persistedstate問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評論