@@ -78,7 +78,7 @@ export class _MatTableDataSource<
78
78
* For example, a 'selectAll()' function would likely want to select the set of filtered data
79
79
* shown to the user rather than all the data.
80
80
*/
81
- filteredData : T [ ] ;
81
+ filteredData : T [ ] = [ ] ;
82
82
83
83
/** Array of data that should be rendered by the table, where each object represents one row. */
84
84
get data ( ) {
@@ -151,7 +151,30 @@ export class _MatTableDataSource<
151
151
* @param data Data object that is being accessed.
152
152
* @param sortHeaderId The name of the column that represents the data.
153
153
*/
154
- sortingDataAccessor : ( data : T , sortHeaderId : string ) => string | number = (
154
+ sortingDataAccessor : ( data : T , sortHeaderId : string ) => string | number ;
155
+
156
+ /**
157
+ * `sortingDataAccessor` is currently a regular property, however we need to know when it is
158
+ * updated so that we can re-render the data. The problem is that we can't turn it into a
159
+ * getter/setter, because it'll break existing users that define the accessor by extending the
160
+ * class and initializing it to a new value. This is a workaround that'll preserve the old
161
+ * behavior for users who override it and apply the fix for ones who don't. Eventually we should
162
+ * reconcile these behaviors.
163
+ * @breaking -change 15.0.0
164
+ */
165
+ private _defineSortingDataAccessor ( ) {
166
+ if ( ! this . sortingDataAccessor ) {
167
+ Object . defineProperty ( this , 'sortingDataAccessor' , {
168
+ get : ( ) => this . _sortingDataAccessor ,
169
+ set : accessor => {
170
+ this . _sortingDataAccessor = accessor ;
171
+ this . _updateChangeSubscription ( ) ;
172
+ } ,
173
+ } ) ;
174
+ }
175
+ }
176
+
177
+ private _sortingDataAccessor : ( data : T , sortHeaderId : string ) => string | number = (
155
178
data : T ,
156
179
sortHeaderId : string ,
157
180
) : string | number => {
@@ -258,7 +281,6 @@ export class _MatTableDataSource<
258
281
constructor ( initialData : T [ ] = [ ] ) {
259
282
super ( ) ;
260
283
this . _data = new BehaviorSubject < T [ ] > ( initialData ) ;
261
- this . _updateChangeSubscription ( ) ;
262
284
}
263
285
264
286
/**
@@ -267,6 +289,8 @@ export class _MatTableDataSource<
267
289
* the provided base data and send it to the table for rendering.
268
290
*/
269
291
_updateChangeSubscription ( ) {
292
+ this . _defineSortingDataAccessor ( ) ;
293
+
270
294
// Sorting and/or pagination should be watched if MatSort and/or MatPaginator are provided.
271
295
// The events should emit whenever the component emits a change or initializes, or if no
272
296
// component is provided, a stream with just a null event should be provided.
@@ -385,6 +409,8 @@ export class _MatTableDataSource<
385
409
* @docs -private
386
410
*/
387
411
connect ( ) {
412
+ this . _defineSortingDataAccessor ( ) ;
413
+
388
414
if ( ! this . _renderChangesSubscription ) {
389
415
this . _updateChangeSubscription ( ) ;
390
416
}
0 commit comments