1
votes

I'm trying to delete my first row from my ngx-datatable. I have created a button at the top of my work with this html :

 <button (click)="onRemoveRow(0)">Remove First Row</button>
<ngx-datatable
#table
class='material'
[columns]="columns"
[columnMode]="'force'"
[rowHeight]="'auto'"
[rows]='temp'
[headerHeight]="50"
[footerHeight]="50"
[limit]="5"
[rowClass]="getRowClass"
[selected]="selected"
[selectionType]="'checkbox'">
  </ngx-datatable-column>
  <ngx-datatable-column name="Site" >
    <ng-template let-value="value" let-sort="sortFn" ngx-datatable-header-template>
     Site 
    </ng-template>
    <ng-template let-value="value" ngx-datatable-cell-template>
      {{ value }}
    </ng-template>
  </ngx-datatable-column>
  <ngx-datatable-column name="Filiere" >
    <ng-template let-value="value" let-sort="sortFn" ngx-datatable-header-template>
     Filiere
    </ng-template>
    <ng-template let-value="value" ngx-datatable-cell-template>
      {{ value }}
    </ng-template>
  </ngx-datatable-column>
  <ngx-datatable-column name="Type" >
    <ng-template let-value="value" let-sort="sortFn" ngx-datatable-header-template>
     Type
    </ng-template>
    <ng-template let-value="value" ngx-datatable-cell-template>
      {{ value }}
    </ng-template>
  </ngx-datatable-column>
  <ngx-datatable-column name="TypeSource" >
    <ng-template let-value="value" let-sort="sortFn" ngx-datatable-header-template>
     TypeSource
    </ng-template>
    <ng-template let-value="value" ngx-datatable-cell-template>
      {{ value }}
    </ng-template>
  </ngx-datatable-column>
  <ngx-datatable-column name="MotifNotif" >
    <ng-template let-value="value" let-sort="sortFn" ngx-datatable-header-template>
     MotifNotif
    </ng-template>
    <ng-template let-value="value" ngx-datatable-cell-template>
      {{ value }}
    </ng-template>
  </ngx-datatable-column>
  <ngx-datatable-column name="Canal">
    <ng-template let-value="value" let-sort="sortFn" ngx-datatable-header-template>
     Canal
    </ng-template>
    <ng-template let-value="value" ngx-datatable-cell-template>
      {{ value }}
    </ng-template>
  </ngx-datatable-column>
  <ngx-datatable-column name="Modele" >
    <ng-template let-value="value" let-sort="sortFn" ngx-datatable-header-template>
     Modele
    </ng-template>
    <ng-template let-value="value" ngx-datatable-cell-template>
      {{ value }}
    </ng-template>
  </ngx-datatable-column>
  <ngx-datatable-column name="ChoixDefaut" >
    <ng-template let-value="value" let-sort="sortFn" ngx-datatable-header-template>
     ChoixDefaut
    </ng-template>
    <ng-template let-value="value" ngx-datatable-cell-template>
      {{ value }}
    </ng-template>
  </ngx-datatable-column>
  </ngx-datatable>

And I tried to use the splice method :

 rows = [
{ site : 'Site1', filiere: 'ADE', type : 'AAAB', typeSource : 'ADH', motifNotif : 'AR',
 canal: 'EMAIL', modele: 'ME1', choixDefaut: ''},
{ site: 'Site1', filiere: 'ADP', type: 'AAAA', typeSource : 'ADH', motifNotif : 'AR',
canal: 'EMAIL', modele: 'ME2'},
{ site: 'Site1', filiere: 'ADP', type: 'AAAA', typeSource : 'ADH', motifNotif : 'AR',
canal: 'EMAIL', modele: 'ME22', choixDefaut: ''},
{ site: 'Site1', filiere: 'ADP', type: 'AAAA', typeSource : 'ADH', motifNotif : 'RET',
canal: 'EMAIL', modele: 'ADH'},
{ site: 'Site1', filiere: 'ADP', type: 'AAAA', typeSource : 'ADH', motifNotif : 'RET',
canal: 'EMAIL', modele: 'MEX', choixDefaut: ''},
{ site: 'Site1', filiere: 'ADP', type: 'AAAA', typeSource : 'ADH', motifNotif : 'TRT',
canal: 'SMS', modele: 'MS1'},
{ site: 'Site1', filiere: 'ADP', type: 'AAAA', typeSource : 'ADH', motifNotif : 'TRT',
canal: 'EMAIL', modele: 'EMY', choixDefaut: ''},
{ site: 'Site1', filiere: 'ADP', type: 'AAAA', typeSource : 'RH', motifNotif : 'AR',
canal: 'SMS', modele: 'MSA', choixDefaut: '' },
{ site: 'Site1', filiere: 'ADP', type: 'AAAA', typeSource : 'RH', motifNotif : 'RET',
canal: 'EMAIL', modele: 'MEF'},
];

onRemoveRow(index : number) {
this.rows.splice(0, 1);
this.rows = [...this.rows];
}

However, i don't know why it is my last row which is deleted. Even if I change the index of my function, the result is the same. First of all, i wonder if there is a better way to remove rows from my table. Also I'm pretty interested learning why my code is not working properly.

Thanks.

2
post your templateSajeetharan
I edited my post.Jonor

2 Answers

2
votes

So after I've done some research my array was "filtered".

// Remove file
  //row is passed by (click)="removeFile(row) in html"
  removeFile(row) {
    // you need this due to  https://swimlane.gitbook.io/ngx-datatable/change-detection
    this.filetitle = this.arrayRemove(this.filetitle, row.name)
  }

  arrayRemove(array, rowName) {

    return array.filter(function(element){
        return element.name != rowName;
    });

 }

You can get your values by passing $event in (click) and furthermore get event.target.value.

2
votes

It removes the first element only, since you have same name for all sites, you see it as the last element.

STACKBLITZ DEMO