collaborating on data within closely related components in a table display

I am trying to figure out the best way to pass data among sibling components. For example, let's say I have a data entry grid structured like this:

<tr *ngFor="let item of frm.controls.items.controls; let i=index" [formGroupName]="i">
   <td><input type="text" formControlName="id" /></td>
   <td><input type="text" formControlName="name" /></td>
</tr>

Now, imagine there is some TypeScript code that reacts to either the valueChanges or the focusout event for each field. If I want to access the data in the id field when a focusout event occurs on the name field, how can I achieve that?

onNameFocusout = function(nameControl) {
  // Need to retrieve the id control for the same row as the name control

My challenge is that I only have access to the name control and need to get the corresponding id control for the same row.

I've considered a few options:

  1. Add the id control as a parameter to the focus out event in the HTML of the name control:

    <input type="text" formControlName="name" onNameFocusout="(frm.controls.items.controls[i].controls.id, frm.controls.items.controls[i].controls.name)" />

  2. Utilize a data sharing service where all values entered in the grid are stored and can be retrieved by any component. However, this method still requires knowledge of the row index to fetch the correct data.

If you have any suggestions on a more efficient approach or if one of these methods seems suitable, I would appreciate your input.

Thank you.

Answer №1

To retrieve the value on blur event:

Example:

<tr *ngFor="let item of frm.get('items'); let i=index" [formGroupName]="i">
   <td><input type="text" formControlName="id" /></td>
   <td><input type="text" (blur)="onBlur(frm.get('items.' + i + '.id'))" formControlName="name" /></td>
</tr>

In Component class:

onBlur(control: FormControl) {
   let value = control.value;
   // value will be the id's value
}

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

Is there a method to dynamically insert commas into numeric values within an input field using an Angular Reactive Form?

Is there a way to automatically insert commas after every 3 digits as numbers are being typed into the input field? Currently, I am using the (blur) function which adds commas only after clicking away from the input field. However, I would like this proc ...

Hold off on proceeding until the subscription loop has finished

Is there a way to verify that all results have been successfully pushed to nodesToExpand after running the for loop? The getFilterResult function is being called via an HTTP request in the nodeService service. for(var step in paths) { this.nodeSe ...

Leverage the key-value pairs in JSON to automatically suggest types within the function parameters

If we have data structured like this: { "key1": "hardcoded string", "key2": "another hardcoded string", } Imagine a function with 2 parameters where the first parameter should refer to key1 and the second to i ...

Gather keyboard information continuously

Currently working with Angular 10 and attempting to capture window:keyup events over a specific period using RXJS. So far, I've been facing some challenges in achieving the desired outcome. Essentially, my goal is to input data and submit the request ...

Document: include checksum in HTML

I have a set of three files. The file named loader.js is responsible for creating an iframe that loads another file called content.html, which in turn loads content.js. I have made loader.js publicly available so that other users can include it on their ow ...

Sending an array of data using Angular in a web service

Here is my model object from model.ts name_id: string[]; public generateUrlencodedParameters(token: string, id?: number): string { let urlSearchParams = new URLSearchParams(); urlSearchParams.append('name_id', this.name_id.toS ...

Invalid component prop provided to ButtonBase in Material UI. Ensure that the children prop is correctly rendered in this custom component

Forgive me for asking a basic question, as I am not the most proficient frontend developer and have searched extensively online. Whenever I inspect my frontend application in Chrome, I keep encountering this error. (3) Material-UI: The component prop pro ...

Ways to obtain the file path of the compiled worker.js loaded from the worker loader, along with its hash

Currently, I am working on a TypeScript React application that utilizes Babel and Webpack for compilation. I have implemented a rule to load my worker with the following configuration: config.module.rules.unshift({ test: /gif\.worker\.js$/, ...

Selecting the checkbox to populate the input field

In my application, there is an input field that can be filled either by searching for an item or by clicking on a checkbox. When the user clicks on the checkbox, the input should be automatically filled with the default value valueText. How can I detect ...

Using Typescript to reduce an array of objects results in concatenation

In my quest to calculate the sum of integers from an array of objects stored in a redux slice, I have encountered a challenge. Here is the code snippet in question: export type Expense = { id: string; title: string; amount: number; date: st ...

What is the best way to retrieve all the keys from an array?

I am looking to retrieve the address, latitude, and longitude data dynamically: let Orders= [{ pedido: this.listAddress[0].address, lat: this.listAddress[0].lat, lng: this.listAddress[0].lng }] The above code only fetches the first item from the lis ...

Retrieving information from a JSON reply within an Angular 2 service

I am currently utilizing a service in angular 2 that retrieves JSON data from an API (). The code for fetching the data is depicted below: getImages() { return this.http.get(`${this.Url}`) .toPromise() .then(response => response.json() ...

Property initialization status not being inherited by class

I'm encountering a situation where the properties of the base class are not being recognized as initialized in the extended class when I inherit a class. I'm unsure if this is a TypeScript or TSLint issue, as my Google search didn't yield re ...

How to stop a method in Angular2 when a specific response is received?

I've been grappling with the idea of unsubscribing from a method in Angular2 once it receives a specific response. settings.component.ts Within my component, the method in question is connectToBridge, where the value of this.selectedBridge is a stri ...

Issue: "The specified function type '(num: number) => number' cannot be assigned to type 'number'.(2322)"

Whenever I use a function like this, everything works smoothly: const roundToTwo = (num: number) => { return +(Math.round(+(num + "e+2")) + "e-2"); }; Upon hovering over the function name in VS Code, I can observe that it returns ...

Generate an array by filtering out null properties from a Javascript/Typescript object

I have a TypeScript plain old JavaScript object (POJO) structured as shown below: export interface Items { firstName?: String; lastName?: String; address?: String; phoneNumber?: number; city?: String; stat ...

Angular allows you to easily download zip files by incorporating its built

After spending hours trying to download a zip file using Angular, I encountered an issue where the downloaded file was smaller than the original. I followed guidance from this link on downloading files with Angular2. I opted not to use the <a> tag d ...

Instructions on how to sign up for a worldwide technique that is known as

I have a file called globalvars.ts where I added a global method. How can I subscribe to this method in the ts page where it is being called? globalvars.ts; httpgetmethod(url:string) { var veri; var headers = new Headers(); headers.append(' ...

Ways to merge two arrays into one in React JS

Here are two arrays presented below: const arrayA = { 0: { id: XXXXXXX, name: "test" }, 1: { id: YYYYYYY, name: "example" } } const arrayB = { 0: { id: XXXXXXX, category: "sea", } 1: { id: YYYYY ...

What is the best way for me to examine [...more] closely?

import * as Joi from 'joi'; import 'joi-extract-type'; const schema = { aaaaaaa: Joi.number() .integer() .positive() .allow(null), bbbbbb: Joi.number() .integer() .positive() .all ...