Choosing Table Rows and Transferring Selection Information to Another Element

I have a table in my Angular 4 component where each row represents a clinic. I want to make each row clickable so that when clicked, it opens an angular2-modal from https://github.com/shlomiassaf/angular2-modal displaying detailed information about the selected clinic. I am new to Angular and unsure of how to pass the clicked row's information to the new component and how to enable clickable rows in the first place. Additionally, it would be nice to add a stylish hover effect to the table.

<tbody *ngIf="!isEditing">
    <tr *ngFor="let clinic of clinics">
      <td><b>{{clinic.clinicCode}}</b><br>
      <font size="-2">Last Update: <br>
      {{clinic.lastUpdate | date:'yyyy-MM-dd hh:mm a' : 'UTC' }}</font></td>
      <td>{{clinic.clinicName}}</td>
      <td>{{clinic.street}}</td>
      <td>{{clinic.zip}}</td>
      <td>{{clinic.town}}</td>
      <td>{{clinic.countryCode}}</td>
      <td>
        <button class="btn btn-sm btn-warning" (click)="enableEditing(clinic)"><i class="fa fa-pencil"></i> Edit</button>
        <button class="btn btn-sm btn-danger" (click)="deleteClinic(clinic)"><i class="fa fa-trash"></i> Delete</button>
      </td>
    </tr>
  </tbody>

Answer №1

Starting with the bonus, the simplest:

CSS:

td:hover{
   background-color:red;
   color:white
}

HTML

1. Modify the button markup like this:

<button class="btn btn-sm btn-warning" (click)="enableEditing(clinic)"><i class="fa fa-pencil"></i> Edit</button>

This allows you to access the 'clinic' object inside enableEditing().

2. Obtain the id for each item by

<tr *ngFor="let clinic of clinics; let i=index">

where i represents the index. You can then do

<button class="btn btn-sm btn-warning" (click)="doSomething(i)"><i class="fa fa-pencil"></i> Edit</button>  

Typescript

doSomething(id){
   console.log(clinics[id]);
}

In the new component, you will need to use @Input to access the 'clinic' object/information.

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

Threejs BufferGeometry Index Update (faces added)

I have successfully added vertices to a BufferGeometry and updated the positions attribute, but now I am struggling with adding faces (updating the index). Does anyone know how to accomplish this task effectively? const vertices = new Float32Array(100 * 3) ...

Preventing Multiple Login Attempts in Angular.js Using Typescript

Is there a way to maintain the user login attempts limit even after page refresh? login() { /* Check if user has fewer than 5 failed login attempts */ if (this.failedAttempts < 4) { this.auth.login(this.credentials).subscribe(() => { this.rou ...

How do you implement radio button logic within an *ngFor loop?

When I populate options in a form with radio buttons, I am not seeing isChecked set to true when I select an option. Am I missing something in the following Angular code? app.component.html <div class="form-group"> <label>answerOption</la ...

When using `onClick` in React, the old state value is not captured even though it may appear to be

In order to enhance the modularity and generality of my tabs and tab slots, I have developed a somewhat intricate setup. To achieve this, I opted to create a context and provider that expose methods for manipulating tabs and slots (where slots represent wh ...

Problem with Extending Jest Matchers in VS Code TypeScript

I've developed unique Jest matchers to enhance expect for handling AxiosResponse objects. Although I've followed the standard method for expanding Jest's matcher types, my custom matchers are not being recognized by TypeScript. The error di ...

TypeScript declaration: discovering modules and defining namespaces

I'm currently in the process of creating a declaration file for h3. For guidance, you can refer to the function available here. Initially, I'm unsure of how typescript identifies declaration files. It seems to detect my declaration when placed ...

Is there a way to receive additional assistance?

Feeling a bit embarrassed, I must confess that I have just spent a considerable amount of time resolving the frustrating issue: **Syntax Error: Unexpected token <** The reason for this error was that I had overlooked adding the following line to my in ...

Is there a feature in VS Code that can automatically update import paths for JavaScript and TypeScript files when they are renamed or

Are there any extensions available for vscode that can automatically update file paths? For example, if I have the following import statement: import './someDir/somelib' and I rename or move the file somelib, will it update the file path in all ...

Drag-and-drop functionality in Angular JavaScript Tree View for rearranging nodes and inserting new nodes

Exploring the world of JavaScript Tree Views and Angular as a beginner. After scouring the internet for information, I'm struggling to find a solution to my specific query. Looking for a tree-view component that seamlessly integrates with Angular, c ...

Error: The template could not be parsed due to the following issues: Element 'mat-card' is not recognized

Every time I run Karma "ng test" for my project, an error keeps popping up: Error message: Failed - Template parse errors: 'mat-card' is not a known element Interestingly enough, the application seems to work perfectly fine with the mat-card ta ...

Determine percentage and classification using a pair of functions

My goal is to display the level as a number along with a progress bar that ranges from 0 to 99 based on the current level. After researching online, I came across a math formula for leveling which is: constant * Math.sqrt(xp); In order to achieve this, I ...

The images stored in the assets are failing to load and displaying a 404 error

Having an issue with displaying an image src/assets/images/test.png I added the image element in the main component as follows: <img src="assets/images/test.png"/> The assets folder is defined within angular.json "options": { ...

Exploring the Applications of Directives in Multiple Modules within Angular 2

When I declare the directive in two modules, I get an error that says Type PermissionDirective is part of the declarations of 2 modules. However, when I declare it in only one module, I receive an error stating Can't bind to 'isPermission' s ...

What is the best way to display toastr messages in an Angular application?

Can you guide me on how to include toastr in an angular app? Currently, I am enrolled in the Angular Fundamentals course and trying to use toastr.success within my export class: handleThumbnailClick(eventName){ toastr.success(eventName) } But, I kee ...

The most suitable TypeScript type for a screen being utilized again in react-navigation v5

When it comes to typing screens under react-navigation v5, I usually follow a simple pattern: // Params definition type RouteParamsList = { Screen1: { paramA: number } Screen2: undefined } // Screen1 type Props = StackScreenProps<R ...

Creating a custom filter with ngx-pagination

Having trouble with filtering in Angular 7 + Electron 4 while using ngx-pagination. I followed the steps mentioned in the documentation, but encountered an error Uncaught Error: Template parse errors: The pipe 'stringFilter' could not be found ...

Error in React Typescript: Attempted to access properties of an undefined value

The new Object is displayed in the console but I'm still encountering an error const GetNo = (): string => { console.log(record); if (record.no !== "") return record.no; //<-- Cannot read properties of und ...

Transmit information to a modal by utilizing the ngx-bootstrap modal component

I have been encountering issues while trying to use angular 8 and ngx-bootstrap to open modals and pass data from parent to modal. It is not working as expected. Can anyone provide guidance on what steps I should take to resolve this issue? Here is the lin ...

Dealing with Typescript Errors in Angular 12 and Material 12

I recently upgraded to Angular 12 and Angular Material 12 by following the guidelines provided on https://update.angular.io/. However, after the update, I started encountering numerous errors related to Angular Material. Specifically, Visual Studio 2019 sh ...

Error when using ES6 modules in ts-jest

I keep running into an 'unexpected token' error whenever I run my tests using ts-jest. To show you exactly what's going on, I've created a small repo with all of my current configurations available here: https://github.com/ramoneguru/t ...