3

I have a ngx-datatable with 4 data columns and one column with a delete button for remove the row from the table as below.

enter image description here

HTML code

<ngx-datatable
*ngIf="!isLoading"
#table
class="data-table"
[scrollbarH]="true"
[rows]="data"
[columnMode]="'force'"
[headerHeight]="50"
[footerHeight]="50"
[rowHeight]="'auto'"
[limit]="limit"
[selected]="selected"
[selectionType]="'single'"
(select)='onSelect($event)'>

    <ngx-datatable-column *ngFor="let columnData of columns" name={{columnData.name}} prop={{columnData.prop}}>
       <ng-template let-column="column" let-sort="sortFn" ngx-datatable-header-template>
          <span class="datatable-header-column datatable-header-cell-label" (click)="sort()"><i class="{{columnData.icon}}" ></i> {{columnData.name}}</span>
       </ng-template>
    </ngx-datatable-column>

    <ngx-datatable-column [sortable]="false" [maxWidth]="70">
        <ng-template let-column="column"  let-row="row" ngx-datatable-cell-template>
            <button class="btn btn-danger btn-mini" *ngIf='!row.unAssigning' (click)="onDelete(row, column)">
                <i class="icofont icofont-trash"></i>
            </button>
            <img class="mini-spinner" *ngIf="row.unAssigning" src="../assets/img/busy-red.gif">
        </ng-template>
    </ngx-datatable-column>     

</ngx-datatable>

TS file

onSelect({
    selected
}) {
    this.router.navigate(['/update', selected[0].id]);
}

Currently when select a row, redirecting user to another page. But I need to remove the redirecting feature when click on the delete button in the table. Redirect must perform only when click on a data columns.

How can I achieve this?

Bishan
  • 15,211
  • 52
  • 164
  • 258

1 Answers1

7

If i understood your problem you want to press the delete button without redirecting the user with the onSelect function.

You can try the following code:

<button class="btn btn-danger btn-mini" *ngIf='!row.unAssigning' (click)="$event.preventDefault(); $event.stopPropagation(); onDelete(row, column)">
    <i class="icofont icofont-trash"></i>
</button>
Tommy
  • 2,355
  • 1
  • 19
  • 48
  • 2
    My use case was a little different, for me it was a – Kyle Burkett May 09 '19 at 16:47