Skip to content

Commit eb9de60

Browse files
committed
fix(virtual-list): empty list crashes
fixes #11093
1 parent 26d10c4 commit eb9de60

File tree

3 files changed

+46
-35
lines changed

3 files changed

+46
-35
lines changed

src/components/virtual-scroll/test/basic/app.module.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ export class E2EPage {
1212
counter: number = 0;
1313

1414
constructor(plt: Platform, public navCtrl: NavController) {
15-
for (var i = 0; i < 200; i++) {
16-
this.addItem();
17-
}
18-
1915
if (plt.is('ios')) {
2016
if (plt.testUserAgent('Safari')) {
2117
this.webview = ': iOS Safari';
@@ -29,6 +25,14 @@ export class E2EPage {
2925
}
3026
}
3127

28+
addItems() {
29+
if (this.items.length === 0) {
30+
for (var i = 0; i < 200; i++) {
31+
this.addItem();
32+
}
33+
}
34+
}
35+
3236
headerFn(record: any, index: number, records: any[]) {
3337
if (index % 4 === 0) {
3438
return index + ' is divisible by 4';

src/components/virtual-scroll/test/basic/main.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232

3333
</ion-list>
3434

35+
<div padding>
36+
<button ion-button (click)="addItems()">Add items</button>
37+
</div>
38+
3539
<div padding>
3640
<button ion-button (click)="pushPage()">Push Virtual Scroll Page</button>
3741
</div>

src/components/virtual-scroll/virtual-util.ts

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -317,40 +317,43 @@ export function updateDimensions(plt: Platform, nodes: VirtualNode[], cells: Vir
317317
data.topViewCell = totalCells;
318318
data.bottomViewCell = 0;
319319

320-
// completely realign position to ensure they're all accurately placed
321-
cell = cells[0];
322-
previousCell = {
323-
row: 0,
324-
width: 0,
325-
height: 0,
326-
top: cell.top,
327-
left: 0,
328-
tmpl: -1
329-
};
330-
for (var i = 0; i < totalCells; i++) {
331-
cell = cells[i];
332-
333-
if (previousCell.left + previousCell.width + cell.width > data.viewWidth) {
334-
// new row
335-
cell.row++;
336-
cell.top = (previousCell.top + previousCell.height);
337-
cell.left = 0;
338-
339-
} else {
340-
// same row
341-
cell.row = previousCell.row;
342-
cell.top = previousCell.top;
343-
cell.left = (previousCell.left + previousCell.width);
344-
}
320+
if (totalCells > 0) {
321+
// completely realign position to ensure they're all accurately placed
322+
cell = cells[0];
323+
previousCell = {
324+
row: 0,
325+
width: 0,
326+
height: 0,
327+
top: cell.top,
328+
left: 0,
329+
tmpl: -1
330+
};
331+
332+
for (var i = 0; i < totalCells; i++) {
333+
cell = cells[i];
345334

346-
// figure out which cells are viewable within the viewport
347-
if (cell.top + cell.height > data.scrollTop && i < data.topViewCell) {
348-
data.topViewCell = i;
335+
if (previousCell.left + previousCell.width + cell.width > data.viewWidth) {
336+
// new row
337+
cell.row++;
338+
cell.top = (previousCell.top + previousCell.height);
339+
cell.left = 0;
340+
341+
} else {
342+
// same row
343+
cell.row = previousCell.row;
344+
cell.top = previousCell.top;
345+
cell.left = (previousCell.left + previousCell.width);
346+
}
347+
348+
// figure out which cells are viewable within the viewport
349+
if (cell.top + cell.height > data.scrollTop && i < data.topViewCell) {
350+
data.topViewCell = i;
349351

350-
} else if (cell.top < viewableBottom && i > data.bottomViewCell) {
351-
data.bottomViewCell = i;
352+
} else if (cell.top < viewableBottom && i > data.bottomViewCell) {
353+
data.bottomViewCell = i;
354+
}
355+
previousCell = cell;
352356
}
353-
previousCell = cell;
354357
}
355358

356359
}

0 commit comments

Comments
 (0)