1.新建query.js,封装模糊查询方法。

export function filterItems(list, keyword) {
	const lowerCaseQuery = keyword.toLowerCase()
	return list.filter((item) => {
		// 遍历每个字段,看是否有任意一个字段包含查询字符串
		for (const key in item) {
			if (typeof item[key] === 'string' item[key].toLowerCase().includes(lowerCaseQuery)) {
				return true
			}
		}
		return false
	})
}

// 其中
// list: 用来筛选的完整的数组
// keyword: 用来查询的关键字

2.在需要用到该方法的页面引入并使用。

import { filterItems } from './query'    //根据自己封装方法所在的路径引入


watch: {
   inputVal(newValue) {
      this.tableData = filterItems(this.list, newValue)
   }
},
// this.tableData : 筛选后的数据
// this.list : 用来筛选的存放的最完整的数据
// newValue : input框输入的关键字

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐