Display a button in ngx-datatable on mouse hover in Angular 9

My goal is to display a link on mouseover of the corresponding cell in ngx-datatable. However, the link appears on all rows within that column instead. Below is the code I am currently using:

<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> 

TypeScript Code:

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

I attempted to assign an ID to the div to inspect the event for potential solutions, but have not found any hints.

Answer №1

The reasoning behind this behavior is that the utilization of the same flag for all rows is causing the issue, so it's essential to assign a unique flag to each row:

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

ts:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A Guide to Handling Errors while Converting a Map to a Promise in Typescript

I am attempting to capture errors if any of the asynchronous code within my map function fails. It seems to enter the error block but does not log anything, as the error variable remains null. Is there an alternative method for handling errors within map ...

Using Angular to make an HTTP POST request to fetch data

My trusty .net backpack has been working flawlessly. However, I encountered an issue when trying to connect it with the Angular front end. All backend requests are post requests and require passing an ApiKey in the body of each request. Interestingly, ever ...

Tips for effectively hydrating Redux state in Next.js upon page refresh?

I'm experiencing an issue with hydrating user data from local storage upon app reload or page refresh. My project utilizes NextJS for the frontend, and for managing state across the application I rely on redux-toolkit and next-redux-wrapper. Upon us ...

Executing asynchronous functions within synchronous functions in TypeScript and returning a boolean value

Is there a way to call an asynchronous function from a synchronous function without encountering issues? function showOpenCaseDialog(): boolean { let result = false; var regardingobjectid = (<Xrm.LookupAttribute<string>>Xrm.Page.getA ...

An error has occurred: Cannot locate a difference supporting the object '[object Object]' of type 'object'. The NgFor only enables binding to Iterables like Arrays

I've already checked for similar questions, but none of them provided a solution that worked for me. I'm facing an issue when receiving an object with the following structure: { "_embedded": { "students": [ { ...

Converting custom JSON data into Highcharts Angular Series data: A simple guide

I am struggling to convert a JSON Data object into Highcharts Series data. Despite my limited knowledge in JS, I have attempted multiple times without success: Json Object: {"Matrix":[{"mdate":2002-02-09,"mvalue":33.0,"m ...

How can I iterate through a variable in TypeScript?

initialize() { var elements = []; for (let i = 1; i <= 4; i++) { let node = { name: `Node ${i}` }; elements.push({ [`node${i}`]: node }); if (i < 4) { let edge = { source: `node${i}`, target: ...

Guide on deploying a web application with Angular 6 as the front end, ASP.NET Core in the backend, and SQL Server as the database on Nginx using Docker

I am currently working on a web application that utilizes Angular 6 for the client side, ASP .NET Core for the backend, and SQL Server as the database. My goal is to deploy this web application on an Nginx server using Docker technology. I have successfull ...

Execute the CountUp function when the element becomes visible

Currently, I am implementing the following library: https://github.com/inorganik/ngx-countUp Is there a way to activate the counting animation only when the section of numbers is reached? In other words, can the count be triggered (<h1 [countUp]="345 ...

Struggling to incorporate res.send(result) into my Node.js code to effectively pass data to my Angular application

I have created a Nodejs and angular2 application where I need to integrate them with a Neo4j application. Currently, I am able to access the database from my NodeJS code via Angular2, but I am facing issues in sending the data back to the Angular2 app. Whe ...

Join our exclusive membership program for table enthusiasts

I'm facing a situation where I have two specific questions: 1) Why is my method returning the same object twice when I use console.log? 2) Why does my method not work when I add my filter? The console.log shows the object, but nothing appears in my ...

Using masonry-layout with Next Js leads to a ReferenceError stating that window is not defined

Implementing the masonry-layout library by David Desandro in my Next app has been a smooth process. You can find the link here. When I apply it, the masonry layout functions perfectly as intended. Here's how I'm incorporating it successfully: imp ...

Issue with Node.js: Interface array not correctly populated

Having trouble reading and storing 4 files into an array of objects. The objects in the array all end up being the same as the last object. Looking for guidance on what I did wrong. Here is the simplified code snippet: import * as fs from "fs"; interfac ...

Modify the data type of an object member based on its original type

I am seeking to convert the data type of each member in an object based on the specific member variable. Here is an example: class A { fct = (): string => 'blabla'; } class B { fct = (): number => 1; } class C { fct = (): { o ...

What is the best way to incorporate an "or" operation into the loop structure?

Is there a way to loop the code provided below while keeping the column names in the array and returning by loop? return Students.filter((singleItem) => { singleItem["id"].toLowerCase().includes(value.toLowerCase()) || singleItem[" ...

"Linking a Next.js application with Azure's Application Insights for advanced insights

Trying to include my Next.js (TypeScript) app logs in Azure Application Insights has been a bit challenging. The documentation provided poor guidance, so I decided to follow this solution: https://medium.com/@nirbhayluthra/integrating-azure-application-ins ...

Tips for implementing a page redirect when encountering a 400 bad request during an API call in Angular

I need to implement a page redirect in case I receive a 400 bad request from an API call. Should I include this in the service or the component.ts file? Here is the code I have written so far: Service.ts getIncidents(customerId): Observable<any> ...

When calling undefined typescript, the async function does not return a value but displays its result afterwards

When I debug my function, it waits until the return statement, but when I call the function, it returns undefined and the errors are also undefined. I'm not sure why this is happening. import userModel from '../Models/user.model'; const bcr ...

Arranging Alphanumeric Characters in Typescript

Given input -> const s = ['2.1.1.a', '2.1.a', '2.1.1.d', 'A2.2', 'A2.1', 2.1.1.c', '2.1.b', '2.1.1.b'] Expected output after sorting -> const s = ['2.1.a', '2. ...

The promise was not handled properly as it was rejected with the message: "The constructor xhr2

Currently, I am working on developing an application using ASPNetCore and Angular 5 with Webpack. However, after upgrading to Angular 5, I encountered the following error: Unhandled Promise rejection: xhr2.XMLHttpRequest is not a constructor ; Zone: <r ...