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.
 

1.2 KiB

前端进行模糊搜索的方法

数据量小的时候,模糊搜索功能可以由前端来实现. FineUI中提供了模糊搜索的工具方法BI.Func.getSearchResult

先看方法描述getSearchResult: (items: any, keyword: any, param?: string) => { find: any[], match: any[] };

分别是需要搜索的集合,需要搜索的关键字,搜索的属性(默认是text属性),返回结果为一个带有matchfind属性的对象,分别记录完全匹配和部分匹配的结果

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]

指定搜索属性示例:

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]