本文實例為大家分享了vue實現(xiàn)Input輸入框模糊查詢方法的具體代碼,供大家參考,具體內(nèi)容如下
原理:原生js的indexOf() 方法,該方法將從頭到尾地檢索數(shù)組,看它是否含有對應(yīng)的元素。開始檢索的位置在數(shù)組 start 處或數(shù)組的開頭(沒有指定 start 參數(shù)時)。如果找到一個 item,則返回 item 的第一次出現(xiàn)的位置。開始位置的索引為 0。
如果在數(shù)組中沒找到指定元素則返回 -1。
下面先看示例:
搜索前:
搜索后:
實現(xiàn)方法:
methods:{ // 點擊搜索工程 search(){ // 支持模糊查詢 // this.xmgcqkJsonsData:用于搜索的總數(shù)據(jù) // toLowerCase():用于把字符串轉(zhuǎn)為小寫,讓模糊查詢更加清晰 let _search = this.jobNo.toLowerCase(); let newListData = []; // 用于存放搜索出來數(shù)據(jù)的新數(shù)組 if (_search) { this.xmgcqkJsonsData.filter(item => { if (item.code.toLowerCase().indexOf(_search) !== -1) { newListData.push(item); } }) } this.xmgcqkJsonsData = newListData; // console.log(‘新數(shù)組',newListData) }, }