简体   繁体   中英

Angular 9, ngx-datatable show button on mouseover

I am trying to show link on mouse over of corresponding cell of ngx-datatable but instead it is showing the link on all the rows of that particular column, here is the code I am doing:

<ngx-datatable
        class="material ml-0 mr-0"
        [rows]="users"
        [columnMode]="'flex'"
        [headerHeight]="50"
        [footerHeight]="50"
        [limit]="10"
        [rowHeight]="'auto'">
        <ngx-datatable-column name="User Name" [flexGrow]="1">
            <ng-template let-row="row" ngx-datatable-cell-template>
            {{ row?.userName }}
            </ng-template>
        </ngx-datatable-column>
        <ngx-datatable-column name="Role" [flexGrow]="1">
            <ng-template let-row="row" let-rowIndex="rowIndex" ngx-datatable-cell-template let-value="value">
                <div (mouseenter)="showRoleOpt(rowIndex,true, $event)" (mouseleave)="showRoleOpt(rowIndex,false, $event)">{{ row?.role }} <a *ngIf="showRoleOptions">Change</a></div>
            </ng-template>
        </ngx-datatable-column>
        <ngx-datatable-column name="First Name" [flexGrow]="1">
            <ng-template let-row="row" ngx-datatable-cell-template>
                {{ row?.firstName }}
            </ng-template>
        </ngx-datatable-column>
        <ngx-datatable-column name="Last Name" [flexGrow]="1">
            <ng-template let-row="row" ngx-datatable-cell-template>
                {{ row?.lastName }}
            </ng-template>
        </ngx-datatable-column>
        <ngx-datatable-column name="Email" [flexGrow]="1">
            <ng-template let-row="row" ngx-datatable-cell-template>
                {{ row?.email }}
            </ng-template>
        </ngx-datatable-column>
        <ngx-datatable-column name="Phone Number" [flexGrow]="1">
            <ng-template let-row="row" ngx-datatable-cell-template>
                {{ row?.phoneNumber }}
            </ng-template>
        </ngx-datatable-column>
        <ngx-datatable-column name="Actions" [flexGrow]="1">
            <ng-template let-row="row" ngx-datatable-cell-template>
            <button mat-icon-button mat-sm-button color="primary" class="mr-1" (click)="openPopUp(row)"><mat-icon>edit</mat-icon></button>
            <button mat-icon-button mat-sm-button color="warn" (click)="deleteItem(row)"><mat-icon>delete</mat-icon></button>
            </ng-template>
        </ngx-datatable-column>
</ngx-datatable> 

TS code:

showRoleOpt(row, opt: boolean, $event) {
    console.log($event.target);
    this.showRoleOptions = opt;
  }

I tried to put some id to div, looking to check the event if there is anything I can do with. But not getting any clue.

The reason behind this behavior is that you are using same flag for all rows , so you can have flag on each row:

<ng-template let-row="row" let-rowIndex="rowIndex" ngx-datatable-cell-template let-value="value">
                <div (mouseenter)="showRoleOpt(row,true, $event)" (mouseleave)="showRoleOpt(row,false, $event)">{{ row?.role }} <a *ngIf="row.showRoleOptions">Change</a></div>
</ng-template>

ts:

showRoleOpt(row:any, opt: boolean, $event) {
    console.log($event.target);
    row.showRoleOptions = opt;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM