What is the method to deactivate input using index?

Is it necessary to only allow editing on the selected row in the input form, rather than enabling editing for the entire group?

View the full code here: https://stackblitz.com/edit/inline-edit-change?file=app/app.component.html

<div class="form-group" [ngClass]="{'editing': editing}">
            <label> Name</label>
            <input type="text" [(ngModel)]="test.name">
            <div class="value">{{ test.name }}</div>
            <button (click)="toggleEdit()">Edit</button>
          </div>

        <div class="form-group" [ngClass]="{'editing': editing}">
            <label> Last Name</label>
            <input type="text" [(ngModel)]="test.lastname">
            <div class="value">{{ test.lastname }}</div>
            <button (click)="toggleEdit()">Edit</button>
          </div>

In the ts file:

public editing:boolean = false;
  toggleEdit() {
    this.editing = !this.editing;
  }


.form-group .value {
    display: none;
  }
  
  .editing .value {
    display: flex;
  }
  
  .editing input {
    display: none;

I want to be able to edit each field individually rather than all at once.

Answer №1

After reviewing your issue, it became clear to me. While there are several suggested improvements from others, I managed to resolve the issue by making modifications to your stackblitz project. Here is the link:

Edited stackblitz

In addition, I am providing the updated code below:

HTML:

<form [formGroup]="form" (ngSubmit)="onSubmit()" novalidate>

<div class="form-group" [ngClass]="{'editing': editing.given_name}">
    <label for="number">First Name</label>
    <input type="text" class="form-control" formControlName="given_name" id="given_name" placeholder="Jane">
    <div class="value">{{user.given_name}}</div>
    <button (click)="toggleEdit('given_name')">Edit</button>
</div>

<div class="form-group" [ngClass]="{'editing': editing.family_name}">
    <label for="street">Last Name</label>
    <input type="text" class="form-control" formControlName="family_name" id="family_name" placeholder="Doe">
    <div class="value">{{user.family_name}}</div>
    <button (click)="toggleEdit('family_name')">Edit</button>
</div>

<div class="form-group" [ngClass]="{'editing': editing.nickname}">
    <label for="city">Public Name</label>
    <input type="text" class="form-control" formControlName="nickname" id="nickname">
    <div class="value">{{user.nickname}}</div>
    <button (click)="toggleEdit('nickname')">Edit</button>
</div>

<div class="form-group" [ngClass]="{'editing': editing.gender}">
    <label for="zip">Gender</label>
    <input type="text" class="form-control" formControlName="gender" id="gender">
    <div class="value">{{user.gender}}</div>
    <button (click)="toggleEdit('gender')">Edit</button>
</div>

<div class="form-button-goup">
    <button type="submit" class="btn sml submit" [disabled]="form.invalid">Save</button>
</div>

TS:

    import { Component, Input, OnDestroy, OnInit } from "@angular/core";
    import {
    FormBuilder,
    FormControl,
    FormGroup,
    Validators
    } from "@angular/forms";

    @Component({
    selector: "my-app",
    templateUrl: "./app.component.html",
    styleUrls: ["./app.component.css"]
    })
    export class AppComponent {
    editing = {
    given_name: false,
    family_name: false,
    nickname: false,
    gender: false
  };

  user = {
    given_name: "Ted",
    family_name: "Mosley",
    nickname: "Tedster",
    gender: "Male"
  };

  form = new FormGroup({
    given_name: new FormControl(this.user.given_name, Validators.required),
    family_name: new FormControl(this.user.family_name, Validators.required),
    nickname: new FormControl(this.user.nickname),
    gender: new FormControl(this.user.gender, Validators.required)
  });

  toggleEdit(attribute) {
    console.log(attribute);
    this.editing[attribute] = !this.editing[attribute];
  }
}

The issue stemmed from using a boolean 'editing' variable for all fields, which disabled everything once one field was edited. I made it more specific in order to address this.

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

Encountering an async issue with npm exiftool in JavaScript

I'm facing issues with npm exiftool usage. (https://www.npmjs.com/package/exiftool) I'm attempting to perform some tasks using it. Iterate through image files in a specific folder Retrieve 'xpKeywords' data of each image file Write th ...

Mastering the art of efficient navigation between various pages

In the create_session.php page, I have included a simple form below. The form action method is set to navigate the user to the "QandATable.php" page upon form submission. However, I would like to implement a conditional logic - if the user enters '1&a ...

Filtering records by a joined association in Rails using Ajax

My goal is to enable an end user to filter a data grid based on an associated record. The data grid currently displays a list of Activities: The model for activity.rb (the list of records) class Activity < ApplicationRecord belongs_to :student ...

Modifying the className attribute is not feasible in JavaScript

Looking to change the class of the first child inside div#programmation <div id="programmationEtat"> <div class="tab-pane ng-scope">...</div> <div class="tab-pane ng-scope">...</div> <div class="tab-pane ng-scope" ...

Encountered a JavaScript error when trying to trigger an alert using PHP

I've incorporated fusion maps into one of my applications. In a particular example, I need to transfer values from one map to another chart, However, I encountered an issue where if the data passed is numeric, the alert message displays correctly, b ...

What makes Safari for Windows stand out when it comes to AJAX?

What sets Safari for Windows apart when it comes to handling AJAX requests? Moreover, are there any potential issues or challenges I should be aware of? ...

Enhance chat functionality by integrating MySQL database to store and update chat messages

I'm currently working on developing a chat system that allows users to enter the chat and send messages. The messages are being stored in a MySQL database, and here is a snippet of my code... <script> $('input[type=text]').on('ke ...

I can't seem to figure out what's causing this error to pop up in my coding setup (Tailwind + Next.js). It

I've been struggling with this problem for a week now and despite my efforts, I haven't been able to find a solution yet. However, I have made some progress in narrowing down the issue. The problem arises when I try to execute the yarn build comm ...

Saving filters in URL for easy bookmarking

My latest project involves developing a straightforward single-page application that allows users to sort entries by name and other attributes. What I want to achieve is a method of storing the current sorting selection (similar to PHP Get) without navig ...

Is there a way to maintain scroll position on a dynamically refreshing div?

After searching for hours, I still can't find a solution to my problem. I am using a simple JQuery script to refresh a div and display my PHP output in it. $script = " <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery ...

Executing a mousedown event in Ajax while simultaneously running another JavaScript function using JQuery

One issue I am facing involves a JavaScript method. There is a date slider for months that triggers an AJAX call when the month is changed (mouseup event). Within this AJAX call, there is another JavaScript code ($.blockUI) which seems to interfere with th ...

Sorting tables with jQuery UI sortable() and rowspan功能

Can jQuery UI's sortable() be configured to sort rows based on their "containing" element only? I have a table with rowspanned cells that should only be sorted within their respective spanned columns. var $sortable = $('.nested-sortable tbody&ap ...

Utilize an index to retrieve the data stored within a div that consists of numerous rows (divs)

I have a main container with several rows nested inside, here is an example: <div id="containerDiv1"> <div class="paramRow"> <input type="text" value="Foo" id="param1" /> </div> <div class="paramRow"> ...

Determining the cursor location within a character in a div that is content editable using AngularJS

I am facing challenges with obtaining the cursor caret position within a contenteditable div. Currently, I am utilizing the following function : onBlurArea (field, ev) { ev.preventDefault() const editable = ev.target.childNodes[1].childNodes[2] ...

How to pass an array as parameters in an Angular HTTP GET request to an API

Hey there! I'm relatively new to Angular and I've hit a roadblock. I need to send an array as parameters to a backend API, which specifically expects an array of strings. const params = new HttpParams(); const depKey = ['deploymentInprogre ...

PHP email script encounters error message despite receiving a network response code of 200

I found this PHP code online and it seems to be correct, but I keep encountering an error message every time I attempt to send a test email. However, the network message indicates that the email was sent successfully. Please refer to the provided images an ...

fewer security personnel leads to a higher success rate

I've tried searching for it online, but unfortunately, I couldn't find any helpful results. What I'm attempting to achieve is a mixin that can lighten or darken a color based on a given percentage. If the percentage is less than 0, then I w ...

Activate HTML5 video playback one time only

How can I use a script to play an HTML5 video only once when an element is in viewport, and then pause it? When I start scrolling again, the video should resume playing. Any suggestions on how to trigger play just once? Script: jQuery.fn.isInViewport = fu ...

Customizing the appearance of Angular's ng attributes with attribute selectors

What are your thoughts on styling an element based on Angular's ng attributes? Is it considered poor practice, and if so, why? Please provide reasoning for your opinion. For example: <!-- HTML --> <ul ng-controller="todoCtrl"> <li ...

Using a combination of React and Redux, we can set up a timer that does not dispatch actions using SetInterval within the ComponentDidMount lifecycle method

I am working on implementing a timer in my App using React + Redux. I have a parent component: import React, { Component } from "react"; import { connect } from "react-redux"; import { compose } from "redux"; import QuestionCounter from "../question-coun ...