此案例在vue中實現
搜索設備ID示例
<input
type="text"
name=""
placeholder="搜索設備ID"
v-model="searchData"
@input="search()" />
search() {
if (this.searchData != '') {
this.isSearch = true //當用戶有輸入時,顯示搜索結果組件
} else {
this.isSearch = false
}
let data = []
if (this.deviceList.length != 0 && this.searchData) {
//當設備列表不為空且用戶有輸入時
let str = `\S*${this.searchData}\S*`
let reg = new RegExp(str)
this.deviceList.map((item) => {
if (reg.test(item.nDeviceID)) {
data.push(item)
}
})
//使用正則和map方法,遍歷數組對象,將匹配項push到新數組中
}
//渲染新數組
this.searchRes = data
}