vue實現(xiàn)input輸入模糊查詢的三種方式
1 計算屬性實現(xiàn)模糊查詢
vue 中通過計算屬性實現(xiàn)模糊查詢,創(chuàng)建 html 文件,代碼直接放入即可。
這里自己導入 vue,我是導入本地已經下載好的。
<script src="./lib/vue-2.6.12.js"></script>
演示:
打開默認顯示全部

輸入關鍵字模糊查詢,名字和年齡都可以

完整代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div id="app">
<h2>人員列表</h2>
<input type="text" placeholder="請輸入名字" v-model="keyWord">
<table>
<thead>
<tr>
<td>名字</td>
<td>年齡</td>
</tr>
</thead>
<tbody>
<tr v-for="(item,i) in fillist" :key="i">
<td>{{ item.name }}</td>
<td>{{ item.age }}</td>
</tr>
</tbody>
</table>
</div>
?
<script src="./lib/vue-2.6.12.js"></script>
?
<script>
const vm = new Vue({
el: '#app',
data: {
keyWord:'',
list:[
{ name: '張三', age: '18' },
{ name: '張四', age: '17' },
{ name: '張五', age: '17' },
{ name: '老六', age: '18' },
{ name: '老八', age: '18' },
{ name: '小三', age: '19' },
{ name: 'Xingyue', age: '18' },
]
},
computed:{
fillist(){
// 返回過濾后的數(shù)組
return this.list.filter((p)=>{
return p.name.indexOf(this.keyWord) !==-1 || p.age.indexOf(this.keyWord) !==-1
})
}
}
})
</script>
</body>
</html>2 watch 監(jiān)聽實現(xiàn)模糊查詢
vue 中通過watch 監(jiān)聽實現(xiàn)模糊查詢
vue 中通過計算屬性實現(xiàn)模糊查詢,創(chuàng)建 html 文件,代碼直接放入即可。
完整代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div id="app">
<h2>人員列表</h2>
<input type="text" placeholder="請輸入名字" v-model="keyWord">
<table>
<thead>
<tr>
<td>名字</td>
<td>年齡</td>
</tr>
</thead>
<tbody>
<tr v-for="(item,i) in fillist" :key="i">
<td>{{ item.name }}</td>
<td>{{ item.age }}</td>
</tr>
</tbody>
</table>
</div>
?
<script src="./lib/vue-2.6.12.js"></script>
?
<script>
const vm = new Vue({
el: '#app',
data: {
keyWord:'',
list:[
{ name: '張三', age: '18' },
{ name: '張四', age: '17' },
{ name: '張五', age: '17' },
{ name: '老六', age: '18' },
{ name: '老八', age: '18' },
{ name: '小三', age: '19' },
{ name: 'Xingyue', age: '18' },
],
fillist:[]
},
watch:{
keyWord:{
immediate:true,//在框的值還沒變化時執(zhí)行如下函數(shù)顯示出所有的情況
handler(val){
this.fillist = this.list.filter((p)=>{
return p.name.indexOf(val) !==-1 || p.age.indexOf(val) !==-1
})
}
}
}
})
</script>
</body>
</html>演示和計算屬性的一樣。。
3 通過按鈕點擊實現(xiàn)模糊查詢
這里我是在 vue-cli 中完成的,完整代碼如下。
vue.app 代碼:
<template>
<div id="app">
<!-- 輸入框 -->
<input type="text" v-model="value" placeholder="請輸入姓名/年齡" />
<!-- 查詢按鈕 -->
<button @click="search">查詢</button>
<!-- 給table表格賦值 -->
?
<table>
<thead>
<tr>
<td>姓名</td>
<td>年齡</td>
</tr>
</thead>
<tbody>
<tr v-for="(item,i) in tableData" :key="i">
<td>{{ item.name }}</td>
<td>{{ item.age }}</td>
</tr>
</tbody>
</table>
</div>
</template>
?
<script>
export default {
data() {
return {
value: '',
tableData: [
{ name: '張三', age: '18' },
{ name: '張四', age: '17' },
{ name: '張五', age: '17' },
{ name: '老六', age: '18' },
{ name: '老八', age: '18' },
{ name: '小三', age: '19' },
{ name: 'Xingyue', age: '18' },
],
//表格B用原表格的數(shù)據(jù)
tableDataB: [
{ name: '張三', age: '18' },
{ name: '張四', age: '17' },
{ name: '張五', age: '17' },
{ name: '老六', age: '18' },
{ name: '老八', age: '18' },
{ name: '小三', age: '19' },
{ name: 'Xingyue', age: '18' },
],
};
},
methods: {
// 點擊搜索 支持模糊查詢
search() {
//表格用原表格的數(shù)據(jù) 即 用于搜索的總數(shù)據(jù)
this.tableData = this.tableDataB;
//獲取到查詢的值,并使用toLowerCase():把字符串轉換成小寫,讓模糊查詢更加清晰
let _search = this.value.toLowerCase();
let newListData = []; // 用于存放搜索出來數(shù)據(jù)的新數(shù)組
if (_search) {
//filter 過濾數(shù)組
this.tableData.filter((item) => {
// newListData中 沒有查詢的內容,就添加到newListData中
if (
item.name.toLowerCase().indexOf(_search) !== -1 ||
item.age.toLowerCase().indexOf(_search) !== -1
) {
newListData.push(item);
}
});
}
//查詢后的表格 賦值過濾后的數(shù)據(jù)
this.tableData = newListData;
},
},
}
</script>
?
<style></style>main.js 代碼如下:
import Vue from 'vue'
import App from './App.vue'
?
Vue.config.productionTip = false
?
new Vue({
render: h => h(App),
}).$mount('#app')整體結構:

演示:
輸入關鍵字,點擊查詢:

大小寫模糊查詢:

到此這篇關于vue實現(xiàn)input輸入模糊查詢的三種方式的文章就介紹到這了,更多相關vue input輸入模糊查詢內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
利用Vue3 (一)創(chuàng)建Vue CLI 項目
這篇文章主要介紹利用Vue3 創(chuàng)建Vue CLI 項目,下面文章內容附有官方文檔鏈接,安裝過程,需要的可以參考一下2021-10-10
Vue3?Composition?API優(yōu)雅封裝第三方組件實例
這篇文章主要為大家介紹了Vue3?Composition?API優(yōu)雅封裝第三方組件實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07

