Exploring the process of obtaining dynamic table row information by clicking a button in a table using TypeScript

I am currently working with a dynamic table that has a button in each row. When the button is clicked, I need to retrieve the data from that specific row.

I attempted to use the (click) event binding on <tr>, but the output I received was undefined.

<table>
<thead> <tr> <th> Name </th> <th> Company </th> </tr> </thead>
<tr *ngFor = "let employee of employees" (click) = "removeEmployee(row)">
            <td> <input type="text" [(ngModel)]= "employee.name"></td>
            <td> <input type="text" [(ngModel)] = "employee.companyName"> </td>
</tr>
</table>

.ts file

removeEmployee(tr) {
      console.log(tr);
    }

Expected: Upon clicking the button, the data from the respective row should be displayed.

Actual: The output shows undefined.

Answer №1

modify the row for each employee:

<tr *ngFor = "let employee of employees" (click) = "deleteEmployee(employee)">

Answer №2

Here is the code snippet to solve the given problem:

<table>
<thead> <tr> <th> Name </th> <th> Company </th> </tr> </thead>
<tr *ngFor = "let employee of employees" (click) = "removeEmployee(employee)">
            <td> <input type="text" [(ngModel)]= "employee.name"></td>
            <td> <input type="text" [(ngModel)] = "employee.companyName"> </td>
</tr>
</table>

Within the .ts file, define the following method:

removeEmployee(tr) {
      console.log(tr);
    }

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

Utilizing NestJS: Integrating Mongoose Schema Custom Validation with Service Methods

Presently, I am managing two different modules: First Module: courses -courses.module.ts -courses.services.ts -courses.schema.ts -courses.controller.ts Second Module: groups -groups.module.ts -groups.services.ts -groups.schema.ts -groups.controller.ts ...

Showing information from page's table using find_element_by_xpath (Selenium with Python)

I am currently exploring the implementation of the find_element_by_xpath() method to locate table elements on a webpage and present them in either a display or print format. Below is an excerpt from the page content: <tbody id="listContainer_databody" ...

Is it possible to access your app directly from the browser without requiring any user prompts?

After successfully setting up my app for both android and ios with the necessary app link and universal link, I am now focusing on redirecting users from a specific website to my app. The mobile aspect is all set, but I need to work on the browser/server s ...

Why does React / NextJS throw a "Cannot read properties of null" error?

In my NextJS application, I am using useState and useEffect to conditionally render a set of data tables: const [board,setBoard] = useState("AllTime"); const [AllTimeLeaderboardVisible, setAllTimeLeaderboardVisible] = useState(false); const [TrendingCreat ...

Display an error message whenever the mouse leaves the input field

Can someone please help me with how to display an error message in Angular4 when the input field is required and the mouse leaves the input? I want the error to show only when the mouse leaves the input, not while I am typing within the input field. Here ...

Determining the type of an overloaded method within a generic function

I am currently working on developing a versatile function that can subscribe to an event emitter. The function subscribe is designed to take 3 arguments: event name, event handler, and the event emitter to connect to. I am looking for ways to ensure accur ...

Wrapper around union function in TypeScript with generics

I'm struggling to find a solution for typing a wrapper function. My goal is to enhance a form control's onChange callback by adding a console.log. Can someone please point out what I might be overlooking? interface TextInput { type: 'Tex ...

Angular Owl Carousel doesn't slide horizontally, it slides vertically

Within my Angular project, I incorporated an Owl Carousel into the home-component.html file. Here is a snippet of the code: <section> <div class="container"> <h1 class="products-title">New Arrivals</h1> ...

What is the best way to obtain the variable value?

What is the best way to pass a variable to the component sibling1? <parentComponent> <sibling1 [data]="parentData"></sibling1> </parentComponent> ...

Tips for locating precise information within nested object formations using Javascript

Within my code, I have showcased two distinct types of response. Upon closer examination of the following code snippets, it becomes evident that the structure of the response from a service differs slightly between the two types. In the first type, there i ...

Tips for organizing a multi-dimensional array based on various column indexes

I am looking to organize a multidimensional array by multiple column index. Take, for instance, the test data provided below: var source = [ ["Jack","A","B1", 4], ["AVicky","M", "B2", 2], [ ...

Error occurs with navctrl when using jquery in Ionic 3

Recently, I started using Ionic framework, I have been integrating jQuery in Ionic to make REST API calls. However, whenever I try to use navCtrl with jQuery, an error pops up in the Chrome console, ERROR TypeError: Cannot read property 'push' ...

Handling functions in Ant Design Select component with TypeScript types

I have a query. Antd offers a custom Select input with functions like onSelect, onChange, etc. I am utilizing the onSelect function which requires the following arguments: (JSX attribute) onSelect?: ((value: string | number | LabeledValue, option: OptionDa ...

Choose the initial item from a dropdown menu in Angular 7

I have a dropdown list containing data in the form of a Map<number, string>. By default, the data is sorted based on the keys in ascending order. When displaying this data in HTML, I am using the keyvalue pipe and a function to sort the items by the ...

When using TypeScript's array intersection type, properties are not accessible when using methods like Array.forEach or Array.some. However, they can be accessed within a for loop

It was challenging to search for this problem because I may not have the correct technical terms, but I hope my example can help clarify it. Background: I am using query data selectors in react-query to preprocess query results and add some properties tha ...

Conceal the search bar for a specific route in Angular6

I am looking to show an input in my header only when the route is /home and hide it for all other routes. I have attempted some code but it does not seem to be working as expected. .html <div *ngIf="searchBarVisible"> <input class="form-cont ...

What is the best approach for setting up a global pipe that can be utilized across various modules?

One of my Angular projects includes a custom pipe called CurrConvertPipe. import {Pipe, PipeTransform} from '@angular/core'; import {LocalStorageService} from './local-storage'; @Pipe({name: 'currConvert', pure: false}) expor ...

Issue with alignment of Ionic 4 toolbar

Has anyone encountered an issue with the toolbar alignment in angular 8/ionic 4? https://i.sstatic.net/QGc6K.png Below is the code snippet for the footer of tab2.page.html: <ion-footer> <ion-toolbar> <ion-buttons start> < ...

Implementing infinite scrolling with Angularfire2

I've been working on incorporating infinite scrolling in my Angular 2 project while fetching data from Firebase. I've been using Observables, but it seems to be pulling in all the records from the database. Here's the code snippet: getUsers ...

Is there a way for me to trigger the opening of a modal component through code?

Is there a way to programmatically open a modal similar to a modal service? In Angular 1, I used the following code: uibModal.open({template:'url.html', controller: MyController}). This method allowed me to avoid adding modal HTML code to my par ...