Skip to content

Commit c530524

Browse files
committed
better trackby function
1 parent 86e2a91 commit c530524

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
@@ -6,7 +6,7 @@
66
<md-checkbox [(ngModel)]="changeReferences">Change references</md-checkbox>
77

88
<span>Track By</span>
9-
<md-radio-group [(ngModel)]="trackBy">
9+
<md-radio-group [(ngModel)]="trackByStrategy">
1010
<md-radio-button [value]="'id'">ID</md-radio-button>
1111
<md-radio-button [value]="'reference'">Reference</md-radio-button>
1212
<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;
2217
propertiesToDisplay: UserProperties[] = [];
18+
trackByStrategy: TrackByStrategy = 'reference';
2319
changeReferences = false;
2420

2521
constructor(public _peopleDatabase: PeopleDatabase) {
@@ -42,8 +38,8 @@ export class DataTableDemo {
4238
return distanceFromMiddle / 50 + .3;
4339
}
4440

45-
userTrackBy(index: number, item: UserData) {
46-
switch (DataTableDemo.TRACK_BY) {
41+
userTrackBy = (index: number, item: UserData) => {
42+
switch (this.trackByStrategy) {
4743
case 'id': return item.id;
4844
case 'reference': return item;
4945
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
@@ -145,21 +145,21 @@ describe('CdkTable', () => {
145145

146146
describe('with trackBy', () => {
147147

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

153-
function createTestComponentWithTrackyByTable() {
154-
fixture = TestBed.createComponent(TrackByCdkTableApp);
154+
trackByComponent = trackByFixture.componentInstance;
155+
trackByComponent.trackByStrategy = trackByStrategy;
155156

156-
component = fixture.componentInstance;
157-
dataSource = component.dataSource as FakeDataSource;
158-
table = component.table;
159-
tableElement = fixture.nativeElement.querySelector('cdk-table');
157+
dataSource = trackByComponent.dataSource as FakeDataSource;
158+
table = trackByComponent.table;
159+
tableElement = trackByFixture.nativeElement.querySelector('cdk-table');
160160

161-
fixture.detectChanges(); // Let the component and table create embedded views
162-
fixture.detectChanges(); // Let the cells render
161+
trackByFixture.detectChanges(); // Let the component and table create embedded views
162+
trackByFixture.detectChanges(); // Let the cells render
163163

164164
// Each row receives an attribute 'initialIndex' the element's original place
165165
getRows(tableElement).forEach((row: Element, index: number) => {
@@ -176,7 +176,7 @@ describe('CdkTable', () => {
176176
// Swap first two elements, remove the third, add new data
177177
function mutateData() {
178178
// Swap first and second data in data array
179-
const copiedData = component.dataSource.data.slice();
179+
const copiedData = trackByComponent.dataSource.data.slice();
180180
const temp = copiedData[0];
181181
copiedData[0] = copiedData[1];
182182
copiedData[1] = temp;
@@ -185,12 +185,12 @@ describe('CdkTable', () => {
185185
copiedData.splice(2, 1);
186186

187187
// Add new data
188-
component.dataSource.data = copiedData;
189-
component.dataSource.addData();
188+
trackByComponent.dataSource.data = copiedData;
189+
trackByComponent.dataSource.addData();
190190
}
191191

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

196196
// Expect that the first and second rows were swapped and that the last row is new
@@ -202,11 +202,11 @@ describe('CdkTable', () => {
202202
});
203203

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

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

212212
// Expect that all the rows are considered new since their references are all different
@@ -218,13 +218,12 @@ describe('CdkTable', () => {
218218
});
219219

220220
it('should add/remove/move rows with changed references with property-based trackBy', () => {
221-
TrackByCdkTableApp.trackBy = 'propertyA';
222-
createTestComponentWithTrackyByTable();
221+
createTestComponentWithTrackyByTable('propertyA');
223222
mutateData();
224223

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

230229
// Expect that the first and second rows were swapped and that the last row is new
@@ -236,13 +235,12 @@ describe('CdkTable', () => {
236235
});
237236

238237
it('should add/remove/move rows with changed references with index-based trackBy', () => {
239-
TrackByCdkTableApp.trackBy = 'index';
240-
createTestComponentWithTrackyByTable();
238+
createTestComponentWithTrackyByTable('index');
241239
mutateData();
242240

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

248246
// Expect first two to be the same since they were swapped but indicies are consistent.
@@ -453,15 +451,15 @@ class DynamicDataSourceCdkTableApp {
453451
`
454452
})
455453
class TrackByCdkTableApp {
456-
static trackBy: 'reference' | 'propertyA' | 'index' = 'reference';
454+
trackByStrategy: 'reference' | 'propertyA' | 'index' = 'reference';
457455

458456
dataSource: FakeDataSource = new FakeDataSource();
459457
columnsToRender = ['column_a', 'column_b'];
460458

461459
@ViewChild(CdkTable) table: CdkTable<TestData>;
462460

463-
trackBy(index: number, item: TestData) {
464-
switch (TrackByCdkTableApp.trackBy) {
461+
trackBy = (index: number, item: TestData) => {
462+
switch (this.trackByStrategy) {
465463
case 'reference': return item;
466464
case 'propertyA': return item.a;
467465
case 'index': return index;

0 commit comments

Comments
 (0)