最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

這里介紹兩種方法: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:'知花實(shí)央',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、計算屬性實(shí)時更新,不用像watch方法,新建的filPerson初始值為空,還需要手動開啟設(shè)置immediate=true初始化,令handler在vue實(shí)例創(chuàng)建后先運(yùn)行一次,賦予初始值。

<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:'知花實(shí)央',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>

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

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

實(shí)現(xiàn)原理

  • 將相應(yīng)的關(guān)鍵字用標(biāo)簽包裹,添加相應(yīng)的樣式,
  • 使用 dom.scrollIntoView() 方法實(shí)現(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)鍵字搜索功能實(shí)戰(zhàn)案例的文章就介紹到這了,更多相關(guān)Vue關(guān)鍵字搜索內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue實(shí)現(xiàn)批量下載文件

    vue實(shí)現(xiàn)批量下載文件

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)批量下載文件的方法(不走后端接口的方法),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12
  • vue實(shí)現(xiàn)標(biāo)簽頁切換/制作tab組件詳細(xì)教程

    vue實(shí)現(xiàn)標(biāo)簽頁切換/制作tab組件詳細(xì)教程

    在項目開發(fā)中需要使用vue實(shí)現(xiàn)tab頁簽切換功能,所以這里總結(jié)下,這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)標(biāo)簽頁切換/制作tab組件的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • Vue實(shí)現(xiàn)計數(shù)器案例

    Vue實(shí)現(xiàn)計數(shù)器案例

    這篇文章主要為大家詳細(xì)介紹了Vue計數(shù)器案例的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • vue如何通過router-link或者button跳轉(zhuǎn)到一個新的頁面

    vue如何通過router-link或者button跳轉(zhuǎn)到一個新的頁面

    這篇文章主要介紹了vue如何通過router-link或者button跳轉(zhuǎn)到一個新的頁面,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • vue 組件中slot插口的具體用法

    vue 組件中slot插口的具體用法

    這篇文章主要介紹了vue 中slot 的具體用法,包括子組件父組件的代碼介紹,需要的朋友可以參考下
    2018-04-04
  • 詳談vue中router-link和傳統(tǒng)a鏈接的區(qū)別

    詳談vue中router-link和傳統(tǒng)a鏈接的區(qū)別

    這篇文章主要介紹了詳談vue中router-link和傳統(tǒng)a鏈接的區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • 如何在 vue3 中使用高德地圖

    如何在 vue3 中使用高德地圖

    這篇文章主要介紹了如何在 vue3 中使用高德地圖,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • vscode下的vue文件格式化問題

    vscode下的vue文件格式化問題

    這篇文章主要介紹了vscode下的vue文件格式化問題,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-11-11
  • Vue 組件參數(shù)校驗(yàn)與非props特性的方法

    Vue 組件參數(shù)校驗(yàn)與非props特性的方法

    這篇文章主要介紹了Vue 組件參數(shù)校驗(yàn)與非props特性的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • vue3中安裝并使用CSS預(yù)處理器Sass的方法詳解

    vue3中安裝并使用CSS預(yù)處理器Sass的方法詳解

    Sass是一種CSS預(yù)處理器,它擴(kuò)展了CSS的功能,提供了更高級的語法和特性,例如變量、嵌套、混合、繼承和顏色功能等,這些特性可以幫助開發(fā)者更高效地管理和維護(hù)樣式表,本文介紹vue3中安裝并使用CSS預(yù)處理器Sass的方法,感興趣的朋友一起看看吧
    2024-01-01

最新評論

西昌市| 平山县| 察雅县| 新建县| 崇左市| 宁远县| 双辽市| 剑川县| 宣城市| 浦城县| 当涂县| 崇左市| 高陵县| 长春市| 苍南县| 大新县| 依兰县| 德州市| 清镇市| 革吉县| 迁西县| 甘孜| 彭泽县| 孝义市| 泸西县| 札达县| 即墨市| 扬中市| 德江县| 五大连池市| 潞西市| 东至县| 祥云县| 北宁市| 鄂温| 格尔木市| 永靖县| 固始县| 五寨县| 札达县| 伊金霍洛旗|