bug: When splicing item from array of data displayed in Virtual Scroll list, end item is duplicated.
See original GitHub issueBug Report
Ionic version: [x] 4.1
Current behavior: Splicing an item out of ion-virtual-scroll list results in duplicate end items. If deleting end item, it fails to remove.
I am deleting Department 3:
This is what is left after deleting Department 3:
Expected behavior: Item should be removed.
Steps to reproduce: Bind ion-items to an array of data Set trackBy to detect changes Delete an item from the array. (Example uses a sliding delete option)
Related code:
<ion-content>
<div class="ion-padding">
<ion-virtual-scroll [trackBy]="trackBy" [items]="departments" approxItemHeight="61px">
<ion-item-sliding *virtualItem="let d">
<ion-item>
<ion-label>{{ d.name }}</ion-label>
</ion-item>
<ion-item-options side="start">
<ion-item-option (click)="deleteDepartment(d, $event)" expandable="true" color="danger">
Delete
</ion-item-option>
</ion-item-options>
</ion-item-sliding>
</ion-virtual-scroll>
</div>
</ion-content>
import { Component } from '@angular/core';
export interface Department {
id: number;
name: string;
}
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
departments: Department[] = [
{ id: 1, name: 'Department 1' },
{ id: 2, name: 'Department 2' },
{ id: 3, name: 'Department 3' },
{ id: 4, name: 'Department 4' }];
/** Track By */
public trackBy(index: number, value: Department) {
return value.id;
}
private deleteDepartment(dept: Department, evt: Event) {
evt.stopPropagation();
evt.preventDefault();
const idx = this.departments.findIndex(m => m.id === dept.id);
this.departments.splice(idx, 1);
}
}
Other information: I am not sure, but it could be related to this issue: 18275
Ionic info:
Ionic:
ionic (Ionic CLI) : 4.2.1 (C:\Users\cindy\AppData\Roaming\npm\node_modules\ionic)
Ionic Framework : @ionic/angular 4.4.0
@angular-devkit/build-angular : 0.13.9
@angular-devkit/schematics : 7.3.9
@angular/cli : 7.3.9
@ionic/angular-toolkit : 1.5.1
System:
NodeJS : v8.11.3 (C:\Program Files\nodejs\node.exe)
npm : 6.2.0
OS : Windows 10
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
bug: When splicing item from array of data displayed in Virtual ...
Current behavior: Splicing an item out of ion-virtual-scroll list results in duplicate end items. If deleting end item, it fails to remove. I...
Read more >angular virtual scroll doesn't re-render the list when list item ...
Many lists seem to render well using the virtual scroll of the angle, but there is a problem when a large number of...
Read more >ngx-in-progress-virtual-scroller - npm
This module displays a small subset of records just enough to fill the viewport and uses the same DOM elements as the user...
Read more >Safari Technology Preview Release Notes - Apple Developer
It marks both the free space and gaps between flex items to reveal how they affect the result. If you see bugs or...
Read more >IBM Informix Messages and Corrections
-100 ISAM error: duplicate value for a record with unique key. ... it saves the contents of the second element of the SQLAWARN...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes, this does seem to resolve our issues. Thank you!
Thanks for the update! I checked the repo and am able to reproduce this issue.
It looks like this is due to how Stencil (what Ionic is built with) handles updating arrays with change detection: https://stenciljs.com/docs/reactive-data#updating-arrays
The following code does not work:
the follow code does work:
In general, when mutating arrays you will want to create a new array in order for the components to detect changes properly. Does this solve your issue?
Thanks!