What is the best way to modify specific data retrieved from an API using Angular?

After successfully listing some data from an API using ngFor, I am facing an issue with editing the data. Whenever I click the edit button, it edits the entire data instead of just the specific row. Below is the code snippet for reference:

HTML

<table class="table table-striped" style="width: 98%; margin: 10px auto;">
                <thead>
                    <tr>
                        <th><strong>Name</strong></th>
                        <th><strong>Consent Type</strong></th>
                        <th><strong>Updated At</strong></th>
                        <th><strong>Status</strong></th>
                        <th><strong>Content</strong></th>
                        <th></th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <tr *ngFor="let consent of SystemConsent">
                        <th *ngIf="!editorStatus">{{consent.fileName}}</th>
                        <th *ngIf="editorStatus"><input type="text" value="{{consent.fileName}}" class="form-control"></th>

                        <th *ngIf="!editorStatus">{{consent.type}}</th>
                        <th *ngIf="editorStatus"><input type="text" value="{{consent.type}}" class="form-control"></th>

                        <th>{{consent.updatedAt}}</th>

                        <th *ngIf="!editorStatus">{{consent.status}}</th>
                        <th *ngIf="editorStatus"><input type="text" value="{{consent.status}}" class="form-control"></th>

                        <th *ngIf="!editorStatus" [innerHTML]="consent.content"></th>
                        <th *ngIf="editorStatus">
                            <ckeditor name="htmlEditor" [config]="config" [editor]="Editor" [(ngModel)]="consent.content" skin="moono-lisa" language="en">
                            </ckeditor>
                        </th>

                        <th><button class="btn trans-btn list-head-btn ng-star-inserted btn-gradient" (click)="changeEditor()">Edit</button></th>
                        <th><button [disabled]="!editorStatus" class="btn trans-btn list-head-btn ng-star-inserted btn-gradient" (click)="getEditorValue()">Save</button></th>
                    </tr>
                </tbody>
            </table>

Typescript

getAdminSystemPreferences() {
    this.adminDashboardService.getSystemPreferences().then(resp => {
      this.SystemConsent = resp['data'].consent;
    });
}

changeEditor() {
    this.editorStatus = true;
}
  
getEditorValue() {
    this.editorStatus = false;
}

Screenshot https://i.sstatic.net/msde4.png

https://i.sstatic.net/5fv8O.png

Please provide assistance in resolving this issue.

Answer №1

Make sure to establish a unique cell identifier (such as id or name) in your data.

Create a public property to hold the selected editing cell id

public selectedEditCellId; // can be a number, string, UUID, etc

When entering edit mode, pass the cell id

changeEditor(cellId) {
    this.selectedEditCellId = cellId;
    this.editorStatus = true;
}

Pass the cell ID when clicked

<td *ngIf="!editorStatus" (click)="changeEditor(CELL_ID)">{{consent.status}}
</td> 
<td *ngIf="editorStatus && consent.id === selectedEditCellId"> 
  <input type="text" value="{{consent.status}}" class="form-control"> 
  <button (click)="changeEditor(undefined)">close editor</button>
</td>

After that, update the *ngIf condition to validate both the editorStatus property and the given identifier

*ngIf="editorStatus && consent.id === selectedEditCellId"

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: Unable to assign property '_id' to a string

I'm currently working on a basic REST API that includes an endpoint for posting data to be saved in MongoDB from an external API. Here's the code I've written so far: app.post('/data', (req, res) => { let url = 'https ...

Changing the height of one Div based on another Div's height

I currently have a display of four divs positioned side by side. I am seeking a solution to ensure that the height of each div remains consistent, and should adjust accordingly if one of them increases in size due to added text content. For reference, ple ...

Is it possible to establish a connection between React and a MySQL Database?

I've been encountering issues with connecting to a remote database in React. Despite my efforts, I haven't been successful in establishing the connection. I have tried various solutions without any luck. The goal is simple - I just want to connec ...

How to activate a button only if the specified conditions are met using VueJS 3

I'm currently facing challenges while working on a form to enable a button when certain conditions are met. The form I created includes fields for name, phone number, options, and a message. Once all requirements are filled, I aim to re-enable the di ...

Electron window widens but does not shrink in size

Currently, I am utilizing electron to create an application where I am using ipc messages to expand and retract the width of the app. The frontend is able to trigger these ipc messages successfully, but for some reason, it refuses to go back to a width of ...

Error: The function SomeFunction is not recognized as a valid function (Mongoose library)

Help needed! I'm encountering an error stating "TypeError: User.getUserByUsername is not a function at Strategy._verify (.../routes/users.js:65:10) var User = require('../models/user'); passport.use(new LocalStrategy( function(username, ...

Validating forms using TypeScript in a Vue.js application with the Vuetify

Currently, I am attempting to utilize Vue.js in conjunction with TypeScript. My goal is to create a basic form with some validation but I keep encountering errors within Visual Studio Code. The initial errors stem from my validate function: validate(): v ...

Numerous asynchronous requests running simultaneously

After successfully querying a database using js/ajax for a single drop-down, I am now looking to enhance my solution by adding multiple identical drop-downs. The goal is to retrieve the same information when an option is selected without allowing duplicate ...

Using 'require' within a nested directive that relies on the parent directive in AngularJS

Implementing a sub directive to be utilized in multiple directives is my current challenge. These parent directives share a common controller that has useful methods for updating scope variables within these directives: (potentially changing controllers ...

Unable to interact with the page while executing printing functionality in

component: <div class="container" id="customComponent1"> New Content </div> <div class="container" id="customComponent2"> different content </div> ...

Tips for Angular 14: How to clearly define a form control as not being undefined

Consider a scenario with a form setup like this: searchForm = new FormGroup({ SearchBox = new FormControl<string>('', {nonNullable: true}); )} Now, when attempting to extract the value from the form field using this code snippet: thi ...

Error encountered in jQuery call during Page_Load

Here is the code I am using to register a javascript function on Page_Load (I also tried it on Page_Init). This javascript function switches two panels from hidden to shown based on a parameter when the page loads: protected void Page_Load(object sen ...

JSON.Parse function does not return a valid value

I'm currently developing a polling system. Once a user submits their answer choice, I expect to receive JSON data containing all the answers for display purposes. Upon submitting the AJAX form, the JSON response is as follows: [{"answer_1":0,"answer ...

Avoid pressing on y mat-button-toogle

In my component, I have a button-toggle like this: <mat-button-toggle-group appearance="legacy"> <mat-button-toggle *ngFor="let session of sessionsArray!"> <fa-icon icon="calendar-alt"></fa-icon> ...

Breaking auto-update functionality when using the 'dd-slick' jQuery plugin to style chained drop-downs

Utilizing the jquery.ddslick.min.js plugin from jQuery for creating customized drop-down menus with image options. Additionally, incorporating the jquery.chained.min.js script to automatically update the content in the second select box based on the select ...

Message from Discord: Unable to access the property 'MessageEmbed' because it is undefined

Attempting to create a simple welcome message embed. Here is my main.js file without the login token: const Discord = require('discord.js'); const client = new Discord.Client(); const MessageEmbed = new Discord.MessageEmbed(); const prefix = &ap ...

Remove any list items that do not possess a particular class

My ul list contains several lis, and I need to remove all li elements that do not have children with the class noremove. This is the original HTML: <ul> <li>Item 1</li> <li>Item 2 <ul> <li>I ...

Efficiently running multiple PHP pages with just one simple click

Below is the code fragment that I currently have: <html> <script type="text/javascript"> function Run() {var a=["ONE" ,"TWO" ,"THREE", "FOUR", "FIVE"]; window.location.href = "index.php?w1=" +a;} </script> <body> <input type="b ...

There was a problem finding the correct address indicated by the marker

I am working on an Android app using PhoneGap, and I need to display a marker on a Google map at a specific latitude and longitude. When the marker is clicked, I want to show an info window displaying the address associated with that location. However, t ...

AngularJS: The power of dynamic HTTP POST parameter names

Utilizing an API to update profile information allows for the addition of a nickname, email, phone number, or password in the request parameters, which will then be updated in the database. When updating a specific field, such as Nickname: { "nickname": ...