Clicking on the edit button will open the form for editing the selected item

I am facing an issue with my HTML code. I have a list of data where each row has an edit button to update the specific record of that row. However, when I click on the edit button, I want only the form under that particular row to open for updating the record. I have used ngFor for the list and ngIf for the div in my HTML code.

Here is my HTML CODE:

<div class="col-sm-4" *ngFor="let album of calls | async ; index as i">
    <div class="card">
      <div class="card-body">
        <a style="border:1px solid red">{{album.id}}</a>
{{i}}
        <a  style="border:1px solid black">{{album.title}}</a>
        <button (click)="onClick(album)">Edit Click</button>
        <form *ngIf="subdiv">
            <a  style="border:1px solid black">{{album.title}}</a>
 <button (click)="onEdit(album)">Submit</button>
        </form>
      </div>
    </div>
  </div>

My TS Code:

onClick(user: any) {
  this.subdiv= true
  console.log(user);
}

Currently, the problem I am facing is that whenever I click on the edit button of any list item, all the forms from the list items are showing up at once.

Answer №1

To ensure only one item is being edited at a time, set the ID of the album that is currently being edited.

HTML

<form *ngIf="subdiv === album.id">
            <a  style="border:1px solid black">{{album.title}}</a>
 <button (click)="onEdit(album)">Submit</button>
        </form>

TS

onClick(album: any) {
  this.subdiv = album.id;
}

Consider renaming the variable to 'currentEditAlbumId` for clarity.

Another approach

Add an additional property to the album model:

Album.ts

export class Album {
 id: number;
 title: string;
 edit: boolean;
}

Utilize the edit property in the following way:

HTML

<form *ngIf="album.edit">
            <a  style="border:1px solid black">{{album.title}}</a>
 <button (click)="onEdit(album)">Submit</button>
        </form>

TS

onClick(album: any) {
  album.edit = true;
}

This approach allows for editing multiple items simultaneously. Special thanks to @pc_coder for highlighting that the initial solution may not support simultaneous edits.

Answer №2

if the subdiv is set to true, all elements become editable due to its general nature.

It is recommended to include the subdiv variable within the album model.

onClick(user: any) {
  user.subdiv= true
}
onCancel(user: any) {
  user.subdiv= false
}

within the html structure

<div class="col-sm-4" *ngFor="let album of calls | async ; index as i">
    <div class="card">
      <div class="card-body">
        <a style="border:1px solid red">{{album.id}}</a>
{{i}}
        <a  style="border:1px solid black">{{album.title}}</a>
        <button *ngIf="!album.subdiv" (click)="onClick(album)">Edit Click</button>
        <button *ngIf="album.subdiv"  (click)="onCancel(album)">Cancel Click</button>
        <form *ngIf="album.subdiv">
            <a  style="border:1px solid black">{{album.title}}</a>
 <button (click)="onEdit(album)">Submit</button>
        </form>
      </div>
    </div>
  </div>

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

Error: JSON encountered circular structure when attempting to serialize an object of type 'ClientRequest' with a property 'socket' that references an object of type 'Socket'

Encountering an error while attempting to make a POST request to my TypeORM API using axios: TypeError: Converting circular structure to JSON --> starting at object with constructor 'ClientRequest' | property 'socket' -&g ...

Finding out the specific row that was clicked in a Jquery Mobile listview

I've searched everywhere and can't find a solution. How can I retrieve the value of a row tapped in a listview? This could be anything from the name to the index within the object. Currently, I have a function handling the tap event. I need to pa ...

Transmit HTTP headers when making an HTTP request, similar to using Postman

Encountered an issue with Angular while attempting to send an HTTP POST request, resulting in the following error: Http failure during parsing for service.ts currentUse(SVF){ //let options = {headers: new HttpHeaders({'Content-Type': &apos ...

Refresh React Components on the Fly (Solr)

I am relatively new to ReactJS In my React class, I have a function that is rendering multiple items: (Sample) var app = app || {}; app.Results = React.createClass({ componentDidMount: function () { }, handleUpdateEvent: function(id) ...

Encountering obstacles when attempting to initiate a node application due to an error

Upon starting my node application, I encountered an error. The error message reads: "ERROR in Metadata version mismatch for module ../node_modules/@ng-bootstrap/ng-bootstrap/index.d.ts, found version 4, expected 3" Here is the full error trace: ERROR ...

Stop the propagation of jQuery by moving the href of a link within a table to a click on a tr element

I am facing an issue with my table, <table id="theTable"> <tr><th>Theader</th><th></th></tr> <tr class="ahover"> <td><a href="pankaka">Im a link</a></td> <td>Im ...

How to import a module from the root path using TypeScript in IntelliJ IDEA

Despite this topic being widely discussed, I still struggle to understand it. Below is my tsconfig.json file: { "compilerOptions": { "module": "commonjs", "target": "es2017", "sourceMap": true, "declaration": true, "allowSyntheticDe ...

Compiling the project using ng build without requiring the download of node

Hey there! Is there a method to execute the ng cli command (ng build --prod) without having to download the npm packages repeatedly? The production build process is becoming very sluggish due to this issue. I am curious to know if there is a way to avoid ...

Basic example of jQuery in an ASPX file

I can't figure out why this basic example is not working. In my WebApplication, I have a script: function myAlert() { $("#Button1").click(function () { alert("Hello world!"); }); } In my asp page, I have the following code: < ...

Ways to convert an asynchronous operation to synchronous in JavaScript

Currently in the process of developing an eslint plugin, I have come across a particular issue. My goal is to implement real-time changes to the configuration file by making an HTTP request to retrieve the JSON configuration. When attempting to execute co ...

What is the best way to retrieve props for computed properties using Vue with Typescript?

Seeking help on accessing props data for my computed property. Here is the code snippet: <script lang="ts"> import { defineComponent } from 'vue' export default defineComponent({ props: { color: String, shape: String, ...

Struggling to retrieve information from a connected SQL table within an Express application

I am currently working on an API using express and postgreSQL to showcase restaurant ratings, reviews, and cuisine in specific areas. I have successfully created routes and controllers that display SQL tables in Postman. My challenge arises when attemptin ...

Transfer the contents of one array into the center of another array using JavaScript

I have been looking for answers here, but I can only find solutions for different programming languages. Currently, I am dealing with 2 Uint8 typed arrays. var arr1 = [0,0,0]; var arr2 = [0,1,2,3,4,5,6,7,8,9]; My goal is to replace the contents of arr2 ...

What's the issue with the addClass function not working as expected?

I am currently integrating the website's navbar using PHP, which is why I am unable to use class="active" to highlight the active tab on my navigation bar individually. To solve this issue, I attempted to add the active class to the corresponding tab ...

Using Vue to dynamically upload multiple files simultaneously

Although this question has been asked frequently, most of the answers do not address a key issue - how to upload multiple images while knowing which image belongs to which data. Attempting to bind v-model to input file doesn't work as expected, and ev ...

Is there a way to modify the maximum size limit for a POST request package?

I am encountering an issue while attempting to send an array of bytes using a POST request. In my server-side implementation, I am utilizing Node.js and Express.js. Unfortunately, I am receiving error code 413 or the page becomes unresponsive ('Payloa ...

Error message: Module 'firebase/app' not found in angular framework

I am currently working through a Google codelab, which can be found at this link: Check out the codelab here However, when I attempt to build the project using 'ng build --prod', I encounter the following error: ERROR in node_modules/@angular ...

Having trouble rendering JSON encoded data in a JqPlot Chart within a PHP script

I've spent the past few days scouring through Stack Overflow and various other websites, but I haven't been able to find a solution to my specific issue. Even the book 'Create Web Charts with JqPlot' by Fabio Nelli didn't provide t ...

Exploring the CSS transitions using jQuery to create fade-in fade-out effects

I'm currently developing an app using Cordova and incorporating jQuery effects like fadein and fadeout. However, I've noticed that these effects are quite slow on my android device. To address this, I'm considering converting them to CSS and ...

gulp build task fails to execute properly due to Callback (cb) issue

My gulp build task is not functioning properly even though I have created three tasks for the final build directory: bulid:cleanfolder build:copy build:remove Gulp Build Task /****************************************** bulid task *********** ...