Commit 88ff9f4d199635923c4e18c78db89b5b649ae5b3
1 parent
4832f85f
UI: Improve page link default search function
Showing
1 changed file
with
32 additions
and
14 deletions
... | ... | @@ -21,25 +21,43 @@ import { SortDirection } from '@angular/material/sort'; |
21 | 21 | |
22 | 22 | export const MAX_SAFE_PAGE_SIZE = 2147483647; |
23 | 23 | |
24 | -export type PageLinkSearchFunction<T> = (entity: T, textSearch: string) => boolean; | |
24 | +export type PageLinkSearchFunction<T> = (entity: T, textSearch: string, searchProperty?: string) => boolean; | |
25 | 25 | |
26 | -const defaultPageLinkSearchFunction: PageLinkSearchFunction<any> = | |
27 | - (entity: any, textSearch: string) => { | |
26 | +export function defaultPageLinkSearchFunction(searchProperty?: string): PageLinkSearchFunction<any> { | |
27 | + return (entity, textSearch) => defaultPageLinkSearch(entity, textSearch, searchProperty); | |
28 | +} | |
29 | + | |
30 | +const defaultPageLinkSearch: PageLinkSearchFunction<any> = | |
31 | + (entity: any, textSearch: string, searchProperty?: string) => { | |
28 | 32 | if (textSearch === null || !textSearch.length) { |
29 | 33 | return true; |
30 | 34 | } |
31 | 35 | const expected = ('' + textSearch).toLowerCase(); |
32 | - for (const key of Object.keys(entity)) { | |
33 | - const val = entity[key]; | |
34 | - if (val !== null) { | |
35 | - if (val !== Object(val)) { | |
36 | - const actual = ('' + val).toLowerCase(); | |
37 | - if (actual.indexOf(expected) !== -1) { | |
38 | - return true; | |
36 | + if (searchProperty && searchProperty.length) { | |
37 | + if (Object.prototype.hasOwnProperty.call(entity, searchProperty)) { | |
38 | + const val = entity[searchProperty]; | |
39 | + if (val !== null) { | |
40 | + if (val !== Object(val)) { | |
41 | + const actual = ('' + val).toLowerCase(); | |
42 | + if (actual.indexOf(expected) !== -1) { | |
43 | + return true; | |
44 | + } | |
39 | 45 | } |
40 | - } else if (isObject(val)) { | |
41 | - if (defaultPageLinkSearchFunction(val, textSearch)) { | |
42 | - return true; | |
46 | + } | |
47 | + } | |
48 | + } else { | |
49 | + for (const key of Object.keys(entity)) { | |
50 | + const val = entity[key]; | |
51 | + if (val !== null) { | |
52 | + if (val !== Object(val)) { | |
53 | + const actual = ('' + val).toLowerCase(); | |
54 | + if (actual.indexOf(expected) !== -1) { | |
55 | + return true; | |
56 | + } | |
57 | + } else if (isObject(val)) { | |
58 | + if (defaultPageLinkSearch(val, textSearch)) { | |
59 | + return true; | |
60 | + } | |
43 | 61 | } |
44 | 62 | } |
45 | 63 | } |
... | ... | @@ -103,7 +121,7 @@ export class PageLink { |
103 | 121 | } |
104 | 122 | |
105 | 123 | public filterData<T>(data: Array<T>, |
106 | - searchFunction: PageLinkSearchFunction<T> = defaultPageLinkSearchFunction): PageData<T> { | |
124 | + searchFunction: PageLinkSearchFunction<T> = defaultPageLinkSearchFunction()): PageData<T> { | |
107 | 125 | const pageData = emptyPageData<T>(); |
108 | 126 | pageData.data = [...data]; |
109 | 127 | if (this.textSearch && this.textSearch.length) { | ... | ... |