原理:原生js的indexOf() 方法,該方法將從頭到尾地檢索數組,看它是否含有對應的元素。開始檢索的位置在數組 start 處或數組的開頭(沒有指定 start 參數時)。如果找到一個 item,則返回 item 的第一次出現的位置。開始位置的索引為 0。
如果在數組中沒找到指定元素則返回 -1。
下面先看示例:
搜索前:
搜索后:
實現方法:
1 methods:{ 2 // 點擊搜索工程 3 search(){ 4 // 支持模糊查詢 5 // this.xmgcqkJsonsData:用於搜索的總數據 6 // toLowerCase():用於把字符串轉為小寫,讓模糊查詢更加清晰 7 let _search = this.jobNo.toLowerCase(); 8 let newListData = []; // 用於存放搜索出來數據的新數組 9 if (_search) { 10 this.xmgcqkJsonsData.filter(item => { 11 if (item.code.toLowerCase().indexOf(_search) !== -1) { 12 newListData.push(item); 13 } 14 }) 15 } 16 this.xmgcqkJsonsData = newListData; 17 // console.log(‘新數組’,newListData) 18 }, 19 }
以上方法是根據 工單號/流水號 進行查詢的,如果在當前基礎上增加對其它條件的搜索,比如 項目/工程名稱,那只需要在原來的代碼上增加一個判斷條件即可,如:
1 if (item.code.toLowerCase().indexOf(_search) !== -1 || item.name.toLowerCase().indexOf(_search) !== -1) { 2 newListData.push(item); 3 }
這就是如何實現vue input輸入框模糊查詢的方法。
版權聲明:本文為博主原創文章,轉載需注明出處。https://www.cnblogs.com/silent007/p/10238112.html
*************************************** END ***************************************