I am having a weird problem all because of allIpRecords: any;
in which I want to maintain a copy of the ipRecords: any;
because in the original code the ipRecords
may change and I want to retain the previous copy in allIpRecords
The problem is whenever the insert function runs and if I have this.allIpRecords = res.data;
inside the insert function then in both the arrays ipRecords
and this.allIpRecords
duplicate entries of the inserted row gets created and in the table the same row is shown two times. If I remove the this.allIpRecords
then nothing of this sort happens only single entry of the inserted items is there in the array.
Even though in the database it is only inserted once but in the local array its shown two times.
<td>
<span></span>
</td>
<td>
<input type="text" [(ngModel)]="newData.ip" />
</td>
<td>
<input type="text" [(ngModel)]="newData.forDomain" />
</td>
<td>
<input type="text" [(ngModel)]="newData.status" />
</td>
<td>
<input type="text" [(ngModel)]="newData.senderEmail" />
</td>
<td>
<button class="common-btn" type="button" (click)="insert()">Insert</button>
</td>
</tr>
<tr *ngFor="let record of ipRecords" class="colour02">
<td>{{record.id}}</td>
<td *ngIf="record.id === editRowId"><input type="text" [(ngModel)]="record.ip" ></td>
<td *ngIf="record.id !== editRowId">{{record.ip}}</td>
<td *ngIf="record.id === editRowId"><input type="text" [(ngModel)]="record.forDomain" ></td>
<td *ngIf="record.id !== editRowId">{{record.forDomain}}</td>
<td *ngIf="record.id === editRowId"><input type="text" [(ngModel)]="record.status" ></td>
<td *ngIf="record.id !== editRowId">{{record.status}}</td>
<td *ngIf="record.id === editRowId"><input type="text" [(ngModel)]="record.senderEmail" ></td>
<td *ngIf="record.id !== editRowId">{{record.senderEmail}}</td>
<td>
<br>
Ts
private newData: any = {};
ipRecords: any;
allIpRecords: any;
ngOnInit() {
this.auth.getAllIpRecords().subscribe((res: any)=>{
this.ipRecords = res.data;
this.allIpRecords = res.data;
}
}
insert() {
if(Object.keys(this.newData).length === 0) {
this.toastr.error("Empty record");
return;
}
this.auth.addIpRecord(this.newData).subscribe((res: any)=>{
if(res.status === "Success") {
this.newData = {};
this.ipRecords.push(res.data);
this.allIpRecords.push(res.data);
}
}