我有用于网格搜索的过滤管道逻辑
results.filter(item => Object.keys(item)
.some(key => searchTerm.split(',').some(arg =>item[key]? item[key].toString().toLowerCase().includes(arg.toString().toLowerCase()):""))
);
上面的代码将在数组列表的所有列中搜索关键字,该关键字绑定了网格,但是我不必搜索数组列表中的所有列。
我只需要搜索网格中显示的列。
因此,逻辑将如下所示:
results
.filter(item => (item.ProxyName.toLowerCase().includes(searchTerm.toLowerCase()) ||
item.ProxyType.toLowerCase().includes(searchTerm.toLowerCase())||
item.SA_SPOC_DL.toLowerCase().includes(searchTerm.toLowerCase())||
item.AssignmentGroup.toLowerCase().includes(searchTerm.toLowerCase()))
);
.filter(item => Object.keys(item)
.some(key => searchTerm.split(',').some(arg =>item[key]? item[key].toString().toLowerCase().includes(arg.toString().toLowerCase()):""))
);
为此,我决定通过逗号分隔的列数组进行过滤,例如
string searchcolumns = [“ ProxyName,ProxyType,SA_SPOC_DL”]
所以,如果我通过逗号分隔的列数组,我该如何重写逻辑?
答案 0 :(得分:0)
我没有碰过您的其他逻辑,只是重构了需要使用列的部分。您只需要遍历列列表即可:
const itemList: any[] = [
{ ProxyName: 'AG', AssignmentGroup: 'AG', ProxyType: 'PT', SA_SPOC_DL: 'SA2' },
{ ProxyName: 'B1', AssignmentGroup: 'AG123', ProxyType: 'AG', SA_SPOC_DL: 'SB2' },
{ ProxyName: 'C1', AssignmentGroup: 'AG', ProxyType: 'PT', SA_SPOC_DL: 'SC2' },
{ ProxyName: 'D1', AssignmentGroup: 'A', ProxyType: 'PT', SA_SPOC_DL: 'AG' },
{ ProxyName: 'E1', AssignmentGroup: 'AG', ProxyType: 'PT', SA_SPOC_DL: '' },
{ ProxyName: 'F1', AssignmentGroup: 'A', ProxyType: 'PT', SA_SPOC_DL: '' }
];
const searchColumns = ['ProxyName', 'ProxyType', 'SA_SPOC_DL', 'AssignmentGroup'];
const result = filterResult('AG', searchColumns, itemList);
function filterResult(searchTerm: String, searchcolumns: string[], results: any[]) {
const resultSet: any[] = [];
searchcolumns.forEach(column => {
results.filter(item =>
item[column].toLowerCase().includes(searchTerm.toLowerCase())
&& !resultSet.includes(column)
).filter(item => Object.keys(item)
.some(key => searchTerm.split(',')
.some(arg => item[key] ? item[key].toString().toLowerCase().includes(arg.toString().toLowerCase()) : '')))
.forEach(element => { if (!resultSet.includes(element)) { resultSet.push(element); } });
});
return resultSet;
}