UI: Improve page link default search function
This commit is contained in:
parent
4832f85f5d
commit
88ff9f4d19
@ -21,14 +21,31 @@ import { SortDirection } from '@angular/material/sort';
|
|||||||
|
|
||||||
export const MAX_SAFE_PAGE_SIZE = 2147483647;
|
export const MAX_SAFE_PAGE_SIZE = 2147483647;
|
||||||
|
|
||||||
export type PageLinkSearchFunction<T> = (entity: T, textSearch: string) => boolean;
|
export type PageLinkSearchFunction<T> = (entity: T, textSearch: string, searchProperty?: string) => boolean;
|
||||||
|
|
||||||
const defaultPageLinkSearchFunction: PageLinkSearchFunction<any> =
|
export function defaultPageLinkSearchFunction(searchProperty?: string): PageLinkSearchFunction<any> {
|
||||||
(entity: any, textSearch: string) => {
|
return (entity, textSearch) => defaultPageLinkSearch(entity, textSearch, searchProperty);
|
||||||
|
}
|
||||||
|
|
||||||
|
const defaultPageLinkSearch: PageLinkSearchFunction<any> =
|
||||||
|
(entity: any, textSearch: string, searchProperty?: string) => {
|
||||||
if (textSearch === null || !textSearch.length) {
|
if (textSearch === null || !textSearch.length) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
const expected = ('' + textSearch).toLowerCase();
|
const expected = ('' + textSearch).toLowerCase();
|
||||||
|
if (searchProperty && searchProperty.length) {
|
||||||
|
if (Object.prototype.hasOwnProperty.call(entity, searchProperty)) {
|
||||||
|
const val = entity[searchProperty];
|
||||||
|
if (val !== null) {
|
||||||
|
if (val !== Object(val)) {
|
||||||
|
const actual = ('' + val).toLowerCase();
|
||||||
|
if (actual.indexOf(expected) !== -1) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
for (const key of Object.keys(entity)) {
|
for (const key of Object.keys(entity)) {
|
||||||
const val = entity[key];
|
const val = entity[key];
|
||||||
if (val !== null) {
|
if (val !== null) {
|
||||||
@ -38,12 +55,13 @@ const defaultPageLinkSearchFunction: PageLinkSearchFunction<any> =
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else if (isObject(val)) {
|
} else if (isObject(val)) {
|
||||||
if (defaultPageLinkSearchFunction(val, textSearch)) {
|
if (defaultPageLinkSearch(val, textSearch)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -103,7 +121,7 @@ export class PageLink {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public filterData<T>(data: Array<T>,
|
public filterData<T>(data: Array<T>,
|
||||||
searchFunction: PageLinkSearchFunction<T> = defaultPageLinkSearchFunction): PageData<T> {
|
searchFunction: PageLinkSearchFunction<T> = defaultPageLinkSearchFunction()): PageData<T> {
|
||||||
const pageData = emptyPageData<T>();
|
const pageData = emptyPageData<T>();
|
||||||
pageData.data = [...data];
|
pageData.data = [...data];
|
||||||
if (this.textSearch && this.textSearch.length) {
|
if (this.textSearch && this.textSearch.length) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user