Update of Angular Material Table rows triggers a popup, however only the values from the first array are populated in all edited rows

Developed an application with two components (A & B) that includes a popup dialog for editing:

  • Component A fetches the data from a service and loads it into a data table

  • Component B initializes the data when a pop event is triggered from A.

Usually, multiple records are loaded with an array response into the table.

However, there seems to be an issue where when a pop event is fired, the data is correctly loaded for record 1 but for record 2, the same value as record 1 is displayed instead of its own.

We utilized MAT_DIALOG_DATA to import the table data into Component B.

The goal here is to have a unique record populate in form B

A_component.ts:

open() const dialogconfig = new MatDialogConfig(); 
dialogconfig.diableClose =true; 

this.dialog.open(BComponent,
{ 
  data: this.datasource
});

BComponent.ts

constructor(@inject((MAT_DIALOG_DATA public) data:any;)

and HTML Code:

<input matInput placeholder="First Name " id="" name=" [ngModel]="data[0].empName">

Answer №1

The dialog displays the same value because you are directly binding the input element's value using ngModel to the first row of your data at this line:

<input matInput placeholder="First Name " id="" name=" [ngModel]="data[0].empName">

data[0].empName will always show the empName of the first record since it is accessed directly through data[0].

Instead, you should only pass the specific row that you want to edit, not the entire datasource.

In your component A, replace the complete datasource with the row that triggered the edit action:

this.dialog.open(BComponent,
{ 
  data: this.datasource // --> change this to the row you want to edit
});

Then in your dialog, you can access the data without an index, as it is not an array but just the individual row:

<input matInput placeholder="First Name " id="" name=" [ngModel]="data.empName">

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

Enhancing supertest functionality with Typescript

Currently, I am working on extending the functionality of supertest. After referencing a solution from Extending SuperTest, I was able to implement the following example using javascript: const request = require('supertest'); const Test = reque ...

Is it feasible for a React-based shell to host or load an Angular component using Module Federation in Webpack 5?

I am currently developing a web application using Angular that will be embedded or loaded from another web application built with React. I am unsure if this integration can be achieved using webpack 5's module federation. Module federation involves l ...

At what juncture is the TypeScript compiler commonly used to generate JavaScript code?

Is typescript primarily used as a pre-code deployment tool or run-time tool in its typical applications? If it's a run-time tool, is the compiling done on the client side (which seems unlikely because of the need to send down the compiler) or on the s ...

Service in Angular 2 failing to push updates to component

I am currently working on a project where I need to retrieve data from MongoDB using a Service call. So far, the Service call is functioning correctly and logging the data in the console as expected. The challenge arises when dealing with a large response ...

Understanding how to pinpoint a particular request within an Angular 5 HTTP Interceptor

Currently utilizing the HTTPInterceptor feature in Angular 5 and things are running smoothly when it comes to cloning http-requests and sending them to the backend server. The issue arises with a particular GET request that polls the server for data every ...

Using TypeScript to declare ambient types with imported declarations

In my TypeScript project, I have a declaration file set up like this: // myapp.d.ts declare namespace MyApp { interface MyThing { prop1: string prop2: number } } It works perfectly and I can access this namespace throughout my project without ...

Unlock the Power of EmailJS with Vue.js 2 and TypeScript

I couldn't find a similar issue online, so here's my problem. I'm trying to create a form for receiving contact from an app using Vue.js 2 and TypeScript. Here is my code: <form ref="form" class="form-data" @submit.pr ...

RxJS: the art of triggering and handling errors

This is more of a syntax question rather than a bug I'm facing. The process is straightforward: Send an HTTP request that returns a boolean value If the boolean is true, proceed If the boolean is false, log a warning and stop the flow. To handle ...

Tips for incorporating the observer design pattern in REST APIs (Communication between front-end and back-end)

Is it possible to subscribe once to an API and receive multiple responses until I unsubscribe from that event? If so, how can this be achieved? If not, why does this approach not align with the observer pattern's guidelines? I attempted using the yie ...

Issue with Angular modal not opening as expected when triggered programmatically

I am working with the ng-bootstrap modal component import { NgbModal, ModalCloseReasons } from "@ng-bootstrap/ng-bootstrap"; When I click on a button, the modal opens as expected <button class="btn labelbtn accountbtn customnavbtn" ...

Ensuring Type Compatibility Between Classes and Object Literals in TypeScript

When working with TypeScript, it is important to note that an object literal can be assigned to a class typed variable as long as the object provides all properties and methods required by the class. class MyClass { a: number; b: string; } // The co ...

Tips and techniques for updating the form value in Angular 4 Material while maintaining binding characteristics

import {Component,ViewChild} from '@angular/core'; import {NgForm} from '@angular/forms' @Component({ selector: 'checkbox-configurable-example', templateUrl: 'checkbox-configurable-example.html', styleUrls: [& ...

Using Material UI Slider along with Typescript for handling onChange event with either a single number or an

Just diving into Typescript and encountered an issue with a Material UI Slider. I'm trying to update my age state variable, but running into a Typescript error due to the typing of age being number and onChange value being number | number[]. How can I ...

Develop StoryShots for integrating with Angular in the Storybook framework

I am currently working on an Angular v8 application integrated with Storybook. My goal is to incorporate automated visual testing using StoryShots. After following the instructions detailed in this guide, I made necessary adjustments in the Jest configurat ...

Unlocking The Mystery of Disappearing Inputs on Ionic 5

How can I prevent Ionic 6 (Angular) from hiding inputs when the keyboard shows? Whenever I focus on an input, the keyboard covers it. Is there a way to automatically scroll so the keyboard is positioned below the selected input? View Image of Input/Keyboa ...

Is there documentation available for the gcloud output formats, such as the JSON output for each command?

As I work to script the gcloud tool in a TypeScript-aware JavaScript environment known as SLIME, I am utilizing the --format json feature for formatting. The integration is smooth, but I find myself manual analyzing the JSON output of each command to und ...

Steps to resolve the issue with "Error: StaticInjectorError(AppModule)[NgbDropdown -> ChangeDetectorRef]"

My attempt at creating a web app using Angular resulted in successful compilation with no errors. However, upon execution, the browser displays a blank page accompanied by the following error message: ERROR Error: Uncaught(in promise): Error: St ...

In Angular 8, the routes for children were determined based on a specific condition

Is there a way to dynamically load a component in child routes based on a condition? Here are my routes: const mainRoutes: Routes = [ { path: '', component: MainComponent, canActivate: [AuthGuard], children: [ ... { ...

Using Rxjs to dynamically map values from an array with forkJoin

Greetings! I have a collection of Boolean observables and would like to apply a logical AND operation. Currently, I am passing static values 'a' and 'b', but I am unsure of the number of elements in the totalKeys array. import { forkJoi ...

Discover which references are yet to be resolved within a tsx file

I have been tasked with developing a custom builder for a web application. The challenge now is to automatically detect imports in the code so that the right modules can be imported. My current solution involves traversing the AST of the scripts, keeping ...