Set a specific row in an Angular FormArray as readonly while allowing other rows to remain editable

I am currently facing a situation in which I have an HTML table connected to a Reactive Form's FormArray. The information retrieved from the server is displayed in the table and should always be read-only. However, I wish to allow the addition of a new row that is editable, with input fields and dropdowns.

Check out this Stackblitz example where I can add a new row, but it remains read-only. I would like the newly added row to contain editable input controls.

Is there a way to achieve this?

Answer №1

There are multiple approaches to accomplish this:

One option is to include the editable property in a new user object and then adjust your td rendering based on whether it is editable or not. Here is a pseudo code example:

<td *ngIf="!user.editable">{{user.name}}</td>
<td *ngIf="user.editable"><input matInput ... ></td>

Another approach is to utilize an editableIndex within your component. If the editableIndex is -1, nothing is editable. Otherwise, it represents the index of the editable row.

<td *ngIf="editableIndex!==i">{{user.name}}</td>
<td *ngIf="editableIndex===i"><input matInput ... ></td>

Instead of having two tds in the template, you can make your input readonly (or disabled for selects):

<td><input [readonly]="editableIndex!==i" matInput ... ></td>

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

What sets a module apart from a script?

As I delve into the depths of TypeScript documentation to grasp the concept of modules, particularly ES6 modules, I stumbled upon some interesting insights. typescript-modules - this documentation talks about typescript modules and highlights an important ...

Sharing a callback function with a child component results in the error message "Invalid type: <func> is not recognized as a function"

I have been trying to pass a callback function down to my app-autocomplete component using the displayFn input parameter: <app-autocomplete [displayFn]="displayWith" formControlName="starter"> </app-autocomplete> The parent component simply i ...

Are Angular 2 animations and transitions exclusively compatible with Chrome browsers?

I've recently developed a web application using Angular2 and wanted to test its cross-browser compatibility. However, I noticed that the cool animations are only working in Chrome. Below is an excerpt from one of my components: @Component({ selec ...

The upload cannot be completed as the file upload is not in a multipart request format

This file upload code was referenced from this source. The issue lies in the fact that it is sending the request as JSON instead of multipart form data, causing the server side code to reject the request and resulting in a failed upload. I have confirmed ...

Tips for properly waiting for an AngularFire2 subscription to complete before executing the subsequent lines of code within Angular2

Having an issue with my Angular2 Type Script code. The goal is to access Questions from a Firebase Database by subscribing to a FirebaseListObserver: this.af.list('/questions').subscribe( val => { this.questions = val console.log(th ...

The error message "The type 'DynamicModule' from Nest.js cannot be assigned to the type 'ForwardReference' within the nest-modules/mailer" was encountered during development

Recently, I decided to enhance my Nest.js application by integrating the MailerModule. I thought of using the helpful guide provided at this link: Acting on this idea, I went ahead and performed the following steps: To start with, I executed the command ...

The width of the Bootstrap tooltip changes when hovered over

Currently, I am facing a peculiar issue with my angular web-application. Within the application, there is a matrix displaying data. When I hover over the data in this matrix, some basic information about the object pops up. Strangely, every time I hover ov ...

I'm curious about the location of sqlite data storage in Ionic when utilizing SqlStorage

I'm simply intrigued by the default key-value usage in this code snippet. import {Storage, SqlStorage } from 'ionic-angular'; let storage = new Storage(SqlStorage); storage.set(key, value); storage.get(key).then((value) => { ... }); Wh ...

Transforming JavaScript into TypeScript within an Angular 4 component class

Here is the Javascript code I currently have. I need to convert this into a component class within an Angular component. As far as I understand, the Character.prototype.placeAt() code is used to add a new method or attribute to an existing object. In this ...

Can Jest be used to mock an internal class object within a method?

I am facing an issue with a class dedicated to handling a data type known as ProductMetadata. This class interacts with a lookup service that selects a specific handler based on the type of ProductMetadata received from a LookupService object. I have been ...

What's the best way to insert values into data binding within a Typescript/ Angular mat Table?

Objective: Create a dynamic table using JSON data <mat-table class="mat-elevation-z8" *ngIf="carrierRates" [dataSource]="carrierRates"> <ng-container *ngFor="let columnName of columnsList" matColumn ...

The specified control name formControlName in the Angular reactive form cannot be located

Having encountered this issue multiple times on Stack Overflow without any success, I am reaching out for help to identify what I might be doing wrong. In my component : ngOnInit() { this.companyCreatForm = this._formBuilder.group({ name: [null, ...

Creating multiple copies of a form div in Angular using Typescript

I'm attempting to replicate a form using Angular, but I keep getting the error message "Object is possibly 'null'". HTML: <div class="form-container"> <form class="example"> <mat-form-field> ...

Provide users with the option to select the email they want to use for signing up while utilizing Angular Firebase's Google signup

My implementation involves using Angular with Firebase for sign up with Google. var result = await this.afAuth.auth.signInWithPopup( new auth.GoogleAuthProvider() ); When I visit my website in Google Chrome while logged into multiple Gmail accounts ...

Angular index.html file can include a conditional script

I am currently working on an Angular project, where the index.html serves as the main entry point for the application, just like in any other Angular project. This file contains important links and configurations. Within the HTML code snippet below, you w ...

Unit testing an Angular 2+ service that makes an httpclient get request through another service

While there are numerous resources available online for testing a service that sends an API request, I am facing challenges in testing a service that: invokes another service that sends an HTTP request and then returns the response to the original servic ...

Exploring Nested Routes with Parameters in Angular 4

I have set up routing for user profiles in Angular4 /* Frame Default */ { path: '', component: FrameDefaultComponent, children: [ {path: 'home', component: SiteHomeComponent}, {path: 'home/:page', comp ...

Maximizing the efficiency of enums in a React TypeScript application

In my React application, I have a boolean called 'isValid' set like this: const isValid = response.headers.get('Content-Type')?.includes('application/json'); To enhance it, I would like to introduce some enums: export enum Re ...

Issue in Angular/Jasmine: TypeError - Attempting to access the 'labels' property of an undefined value in Angular

While writing unit tests for components, I encountered an error in the terminal when running the tests. The error message displayed was: TypeError: Cannot read property 'labels' of undefined. There were a total of 3 function errors that ...

Unable to receive a response from the HttpClient

I've encountered an issue while trying to manage error responses from my API. What I expect to receive: What I'm actually receiving: My service.ts: import { HttpClient, HttpErrorResponse } from '@angular/common/http'; import { Inject ...