Skip to content

Commit 75e9ed7

Browse files
committed
better trackby function
1 parent 5267502 commit 75e9ed7

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

src/demo-app/data-table/data-table-demo.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<md-checkbox [(ngModel)]="changeReferences">Change references</md-checkbox>
88

99
<span>Track By</span>
10-
<md-radio-group [(ngModel)]="trackBy">
10+
<md-radio-group [(ngModel)]="trackByStrategy">
1111
<md-radio-button [value]="'id'">ID</md-radio-button>
1212
<md-radio-button [value]="'reference'">Reference</md-radio-button>
1313
<md-radio-button [value]="'index'">Index</md-radio-button>

src/demo-app/data-table/data-table-demo.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@ export type TrackByStrategy = 'id' | 'reference' | 'index';
1313
styleUrls: ['data-table-demo.css'],
1414
})
1515
export class DataTableDemo {
16-
static TRACK_BY: 'id' | 'reference' | 'index' = 'reference';
17-
18-
get trackBy(): TrackByStrategy { return DataTableDemo.TRACK_BY; }
19-
set trackBy(trackBy: TrackByStrategy) { DataTableDemo.TRACK_BY = trackBy; }
20-
2116
dataSource: PersonDataSource | null;
2217
propertiesToDisplay: UserProperties[] = [];
18+
trackByStrategy: TrackByStrategy = 'reference';
2319
changeReferences = false;
2420
highlights = new Set<string>();
2521

@@ -43,8 +39,8 @@ export class DataTableDemo {
4339
return distanceFromMiddle / 50 + .3;
4440
}
4541

46-
userTrackBy(index: number, item: UserData) {
47-
switch (DataTableDemo.TRACK_BY) {
42+
userTrackBy = (index: number, item: UserData) => {
43+
switch (this.trackByStrategy) {
4844
case 'id': return item.id;
4945
case 'reference': return item;
5046
case 'index': return index;

src/lib/core/data-table/data-table.spec.ts

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,21 @@ describe('CdkTable', () => {
148148

149149
describe('with trackBy', () => {
150150

151-
afterEach(() => {
152-
// Resetting the static value of the trackby function for TrackByCdkTableApp
153-
TrackByCdkTableApp.trackBy = 'reference';
154-
});
151+
let trackByComponent: TrackByCdkTableApp;
152+
let trackByFixture: ComponentFixture<TrackByCdkTableApp>;
153+
154+
function createTestComponentWithTrackyByTable(trackByStrategy) {
155+
trackByFixture = TestBed.createComponent(TrackByCdkTableApp);
155156

156-
function createTestComponentWithTrackyByTable() {
157-
fixture = TestBed.createComponent(TrackByCdkTableApp);
157+
trackByComponent = trackByFixture.componentInstance;
158+
trackByComponent.trackByStrategy = trackByStrategy;
158159

159-
component = fixture.componentInstance;
160-
dataSource = component.dataSource as FakeDataSource;
161-
table = component.table;
162-
tableElement = fixture.nativeElement.querySelector('cdk-table');
160+
dataSource = trackByComponent.dataSource as FakeDataSource;
161+
table = trackByComponent.table;
162+
tableElement = trackByFixture.nativeElement.querySelector('cdk-table');
163163

164-
fixture.detectChanges(); // Let the component and table create embedded views
165-
fixture.detectChanges(); // Let the cells render
164+
trackByFixture.detectChanges(); // Let the component and table create embedded views
165+
trackByFixture.detectChanges(); // Let the cells render
166166

167167
// Each row receives an attribute 'initialIndex' the element's original place
168168
getRows(tableElement).forEach((row: Element, index: number) => {
@@ -179,7 +179,7 @@ describe('CdkTable', () => {
179179
// Swap first two elements, remove the third, add new data
180180
function mutateData() {
181181
// Swap first and second data in data array
182-
const copiedData = component.dataSource.data.slice();
182+
const copiedData = trackByComponent.dataSource.data.slice();
183183
const temp = copiedData[0];
184184
copiedData[0] = copiedData[1];
185185
copiedData[1] = temp;
@@ -188,12 +188,12 @@ describe('CdkTable', () => {
188188
copiedData.splice(2, 1);
189189

190190
// Add new data
191-
component.dataSource.data = copiedData;
192-
component.dataSource.addData();
191+
trackByComponent.dataSource.data = copiedData;
192+
trackByComponent.dataSource.addData();
193193
}
194194

195195
it('should add/remove/move rows with reference-based trackBy', () => {
196-
createTestComponentWithTrackyByTable();
196+
createTestComponentWithTrackyByTable('reference');
197197
mutateData();
198198

199199
// Expect that the first and second rows were swapped and that the last row is new
@@ -205,11 +205,11 @@ describe('CdkTable', () => {
205205
});
206206

207207
it('should add/remove/move rows with changed references without property-based trackBy', () => {
208-
createTestComponentWithTrackyByTable();
208+
createTestComponentWithTrackyByTable('reference');
209209
mutateData();
210210

211211
// Change each item reference to show that the trackby is not checking the item properties.
212-
component.dataSource.data = component.dataSource.data
212+
trackByComponent.dataSource.data = trackByComponent.dataSource.data
213213
.map(item => { return {a: item.a, b: item.b, c: item.c}; });
214214

215215
// Expect that all the rows are considered new since their references are all different
@@ -221,13 +221,12 @@ describe('CdkTable', () => {
221221
});
222222

223223
it('should add/remove/move rows with changed references with property-based trackBy', () => {
224-
TrackByCdkTableApp.trackBy = 'propertyA';
225-
createTestComponentWithTrackyByTable();
224+
createTestComponentWithTrackyByTable('propertyA');
226225
mutateData();
227226

228227
// Change each item reference to show that the trackby is checking the item properties.
229228
// Otherwise this would cause them all to be removed/added.
230-
component.dataSource.data = component.dataSource.data
229+
trackByComponent.dataSource.data = trackByComponent.dataSource.data
231230
.map(item => { return {a: item.a, b: item.b, c: item.c}; });
232231

233232
// Expect that the first and second rows were swapped and that the last row is new
@@ -239,13 +238,12 @@ describe('CdkTable', () => {
239238
});
240239

241240
it('should add/remove/move rows with changed references with index-based trackBy', () => {
242-
TrackByCdkTableApp.trackBy = 'index';
243-
createTestComponentWithTrackyByTable();
241+
createTestComponentWithTrackyByTable('index');
244242
mutateData();
245243

246244
// Change each item reference to show that the trackby is checking the index.
247245
// Otherwise this would cause them all to be removed/added.
248-
component.dataSource.data = component.dataSource.data
246+
trackByComponent.dataSource.data = trackByComponent.dataSource.data
249247
.map(item => { return {a: item.a, b: item.b, c: item.c}; });
250248

251249
// Expect first two to be the same since they were swapped but indicies are consistent.
@@ -539,15 +537,15 @@ class DynamicDataSourceCdkTableApp {
539537
`
540538
})
541539
class TrackByCdkTableApp {
542-
static trackBy: 'reference' | 'propertyA' | 'index' = 'reference';
540+
trackByStrategy: 'reference' | 'propertyA' | 'index' = 'reference';
543541

544542
dataSource: FakeDataSource = new FakeDataSource();
545543
columnsToRender = ['column_a', 'column_b'];
546544

547545
@ViewChild(CdkTable) table: CdkTable<TestData>;
548546

549-
trackBy(index: number, item: TestData) {
550-
switch (TrackByCdkTableApp.trackBy) {
547+
trackBy = (index: number, item: TestData) => {
548+
switch (this.trackByStrategy) {
551549
case 'reference': return item;
552550
case 'propertyA': return item.a;
553551
case 'index': return index;

0 commit comments

Comments
 (0)