溫馨提示×

vue實現搜索框模糊查詢的方法有哪些

vue
小億
403
2023-08-03 23:54:29
欄目: 編程語言

Vue實現搜索框模糊查詢的方法有以下幾種:

  1. 使用computed屬性:在Vue組件的computed選項中定義一個過濾函數,根據輸入的關鍵詞對數據進行篩選。
computed: {
filteredData() {
return this.dataList.filter(item => item.name.includes(this.keyword));
}
}
  1. 使用watch屬性:在Vue組件的watch選項中監聽輸入框的變化,然后根據關鍵詞進行篩選。
watch: {
keyword: {
handler(newKeyword) {
this.filteredData = this.dataList.filter(item => item.name.includes(newKeyword));
},
immediate: true
}
}
  1. 使用自定義指令:自定義一個v-filter指令,通過鉤子函數bind和update監聽輸入框的變化,然后根據關鍵詞進行篩選。
Vue.directive('filter', {
bind(el, binding) {
el.addEventListener('input', function() {
const keyword = el.value;
binding.value(keyword);
});
},
update(el, binding) {
const keyword = el.value;
binding.value(keyword);
}
});
<template>
<input v-filter="filterData" />
</template>
methods: {
filterData(keyword) {
this.filteredData = this.dataList.filter(item => item.name.includes(keyword));
}
}

以上是一些常見的實現搜索框模糊查詢的方法,具體可以根據自己的需求選擇適合的方式。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女