You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.2 KiB

2 years ago
# 前端进行模糊搜索的方法
数据量小的时候,模糊搜索功能可以由前端来实现. FineUI中提供了模糊搜索的工具方法`BI.Func.getSearchResult`
先看方法描述`getSearchResult: (items: any, keyword: any, param?: string) => { find: any[], match: any[] };`
分别是需要搜索的集合,需要搜索的关键字,搜索的属性(默认是text属性),返回结果为一个带有`match`和`find`属性的对象,分别记录完全匹配和部分匹配的结果
```javascript
const items = [
{
text: "财务部",
value: 1,
}, {
text: "采购部",
value: 2,
}, {
text: "财务",
value: 3,
}
];
const { match, find } = BI.Func.getSearchResult(items, "cw");
console.log(match.map(m => m.value)); // [3]
console.log(find.map(f => f.value)); // [1,2]
```
指定搜索属性示例:
```javascript
const items = [
{
name: "财务部",
value: 1,
}, {
name: "采购部",
value: 2,
}, {
name: "财务",
value: 3,
}
];
const { match, find } = BI.Func.getSearchResult(items, "cw","name");
console.log(match.map(m => m.value)); // [3]
console.log(find.map(f => f.value)); // [1,2]
```