@@ -44,7 +44,7 @@ export class MatTableDataSource<T> implements DataSource<T> {
44
44
45
45
/**
46
46
* Filter term that should be used to filter out objects from the data array. To override how
47
- * the filter matches data objects , provide a custom function on filterTermAccessor .
47
+ * data objects match to this filter string , provide a custom function for filterPredicate .
48
48
*/
49
49
set filter ( filter : string ) { this . _filter . next ( filter ) ; }
50
50
get filter ( ) : string { return this . _filter . value ; }
@@ -91,14 +91,24 @@ export class MatTableDataSource<T> implements DataSource<T> {
91
91
}
92
92
93
93
/**
94
- * Transforms data objects into a filter term that will be used to check against the filter if
95
- * a filter is set. By default, the function will iterate over the values of the data object
96
- * and convert them to a lowercase string.
97
- * @param data Data object to convert to a string that checked for containing the filter term.
94
+ * Checks if a data object matches the data source's filter string. By default, each data object
95
+ * is converted to a string of its properties and returns true if the filter has
96
+ * at least one occurrence in that string. By default, the filter string has its whitespace
97
+ * trimmed and the match is case-insensitive. May be overriden for a custom implementation of
98
+ * filter matching.
99
+ * @param data Data object used to check against the filter.
100
+ * @param filter Filter string that has been set on the data source.
101
+ * @returns Whether the filter matches against the data
98
102
*/
99
- filterTermAccessor : ( ( data : T ) => string ) = ( data : T ) : string => {
103
+ filterPredicate : ( ( data : T , filter : string ) => boolean ) = ( data : T , filter : string ) : boolean => {
104
+ // Transform the data into a lowercase string of all property values.
100
105
const accumulator = ( currentTerm , key ) => currentTerm + data [ key ] ;
101
- return Object . keys ( data ) . reduce ( accumulator , '' ) . toLowerCase ( ) ;
106
+ const dataStr = Object . keys ( data ) . reduce ( accumulator , '' ) . toLowerCase ( ) ;
107
+
108
+ // Transform the filter by converting it to lowercase and removing whitespace.
109
+ const transformedFilter = filter . trim ( ) . toLowerCase ( ) ;
110
+
111
+ return dataStr . indexOf ( transformedFilter ) != - 1 ;
102
112
}
103
113
104
114
constructor ( initialData : T [ ] = [ ] ) {
@@ -145,9 +155,8 @@ export class MatTableDataSource<T> implements DataSource<T> {
145
155
// If there is a filter string, filter out data that does not contain it.
146
156
// Each data object is converted to a string using the function defined by filterTermAccessor.
147
157
// May be overriden for customization.
148
- const filteredData = ! this . filter ? data : data . filter ( obj => {
149
- return this . filterTermAccessor ( obj ) . indexOf ( this . filter ) != - 1 ;
150
- } ) ;
158
+ const filteredData =
159
+ ! this . filter ? data : data . filter ( obj => this . filterPredicate ( obj , this . filter ) ) ;
151
160
152
161
if ( this . paginator ) { this . _updatePaginator ( filteredData . length ) ; }
153
162
0 commit comments