Vue關(guān)鍵字搜索功能實戰(zhà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進行依賴注入的代碼詳解
在現(xiàn)代前端開發(fā)中,Vue.js已經(jīng)成為了非常流行的框架之一,它提供了極大的靈活性和可維護性,今天我們要探討的是Vue?3中的provide和inject功能,這是一種用于在組件樹中進行依賴注入的方法,需要的朋友可以參考下2024-06-06
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實現(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問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03

