Appending or removing a row in the P-Table will result in updates to the JSON

My task involves implementing CRUD (Create, Read, Update, Delete) functionality for my table. While Create, Read, and Update are working fine with the JSON file, I am struggling to figure out how to delete a specific row in the table without using JQuery or AngularJS but only Angular 6. Can anyone provide guidance on this issue?

I need to replicate this process for 4 more arrays similar to this one. If someone can explain the deletion process with one array, I believe I will be able to handle all of them. My primary concern is regarding the delete operation using splice, and if you also have insights on adding a new empty line/object that can be altered within the p-table using editable columns, it would be greatly appreciated.

JSON:

Partijen = [
  {
    "Adressen": [{
        "iAdresID" : "118",
        "iID" : "74",
        "sType" : "Partij",
        "sSoort" : "BezoekAdres",
        "iWijkCode" : "3356",
        "sSraatCode" : "ME",
        "iHuisNr" : "4",
        "sToev" : "",
        "sStraat" : "Boerenschouw",
        "sPlaats" : "PAPENDRECHT",
        "sGemeente" : "PAPENDRECHT",
        "rLatitudeX" : "51,83249",
        "rlongitudeY" : "4,71396",
        "sAdres" : "Boerenschouw 4, 3356ME PAPENDRECHT"
    },
    {
        "iAdresID" : "119",
        "iID" : "74",
        "sType" : "Partij",
        "sSoort" : "PostAdres",
        "iWijkCode" : "3356",
        "sSraatCode" : "ME",
        "iHuisNr" : "4",
        "sToev" : "",
        "sStraat" : "Boerenschouw",
        "sPlaats" : "PAPENDRECHT",
        "sGemeente" : "PAPENDRECHT",
        "rLatitudeX" : "51,83249",
        "rlongitudeY" : "4,713613",
        "sAdres" : "Boerenschouw 4, 3356ME PAPENDRECHT"
        }
      ]
    }
  ]

Part of P-table

<p-tabPanel header="Adressen" leftIcon="pi pi-home">
            <p-table [value]="Partijen" [(selection)]="Adressen">
                <ng-template pTemplate="header">
                    <tr>
                        <th>Soort</th>
                        <th>Straatnaam</th>
                        <th>Huisnummer</th>
                        <th>Wijkcode</th>
                        <th>Straatcode</th>
                        <th>Plaats</th>
                        <th>Verwijderen</th>
                    </tr>
                </ng-template>
                <ng-template pTemplate="body" let-Partij>
                    <tr>
                    <td pEditableColumn>
                        <p-cellEditor>
                            <ng-template pTemplate="input">
                                <input type="text" [(ngModel)]="Partij.Adressen[0].sSoort">
                            </ng-template>
                            <ng-template pTemplate="output">
                                {{Partij.Adressen[0].sSoort}}
                            </ng-template>
                        </p-cellEditor>
                    </td>
                    <td pEditableColumn>
                            <p-cellEditor>
                                <ng-template pTemplate="input">
                                    <input type="text" [(ngModel)]="Partij.Adressen[0].sStraat">
                                </ng-template>
                                <ng-template pTemplate="output">
                                    {{Partij.Adressen[0].sStraat}}
                                </ng-template>
                            </p-cellEditor>
                        </td>
                        <td pEditableColumn>
                            <p-cellEditor>
                                <ng-template pTemplate="input">
                                    <input type="text" [(ngModel)]="Partij.Adressen[0].iHuisNr">
                                </ng-template>
                                <ng-template pTemplate="output">
                                    {{Partij.Adressen[0].iHuisNr}}
                                </ng-template>
                            </p-cellEditor>
                        </td>
                        <td pEditableColumn>
                            <p-cellEditor>
                                <ng-template pTemplate="input">
                                    <input type="text" [(ngModel)]="Partij.Adressen[0].iWijkCode">
                                </ng-template>
                                <ng-template pTemplate="output">
                                    {{Partij.Adressen[0].iWijkCode}}
                                </ng-template>
                            </p-cellEditor>
                        </td>
                        <td pEditableColumn>
                            <p-cellEditor>
                                <ng-template pTemplate="input">
                                    <input type="text" [(ngModel)]="Partij.Adressen[0].sSraatCode">
                                </ng-template>
                                <ng-template pTemplate="output">
                                    {{Partij.Adressen[0].sSraatCode}}
                                </ng-template>
                            </p-cellEditor>
                        </td>
                        <td pEditableColumn>
                            <p-cellEditor>
                                <ng-template pTemplate="input">
                                    <input type="text" [(ngModel)]="Partij.Adressen[0].sPlaats">
                                </ng-template>
                                <ng-template pTemplate="output">
                                    {{Partij.Adressen[0].sPlaats}}
                                </ng-template>
                            </p-cellEditor>
                        </td>
                        <td>
                            <button pButton type="button" label="Verwijder" class="buttonCSS" (click)="on_Verwijderen()"></button>
                        </td>
                    </tr>
                </ng-template>
            </p-table>
        </p-tabPanel>
</p-tabView>

Answer №1

If you want to remove a row, you can utilize the delete method by passing in the row data.

<p-column> 
    <ng-template pTemplate="body" let-row="rowData">
         <button pButton type="button" label="Delete" class="buttonCSS" (click)="on_Delete(row)"></button>
    </ng-template>
</p-column>

Then, within the on_Delete function, target the specific row in the array based on its unique id for removal:

on_Delete(row: any){
            this.Partijen.Adressen.splice(this.Partijen.Adressen.findIndex((r) => (r.iAdresID === row.iAdresID)), 1);
    }

If you already have the row data stored in a variable like Partij, you can simplify the button's click event:

<button pButton type="button" label="Delete" class="buttonCSS" (click)="on_Delete(Partij)"></button>

It seems there may be confusion with your table setup as you're directly accessing the Adressen array by index, which might not be the ideal approach.

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

Failure to receive an array as the output after invoking a web service using AJAX-JSON

I am working on retrieving data from my web service in array format so that I can loop through and fetch all the necessary information. Here's what I have done so far: When I return the result in my web service, I use return json_encode($newFiles); ...

Testing a component in Angular 2 that utilizes the router-outlet functionality

I recently set up an angular 2 project using angular-cli. As part of the setup, I created a separate AppRoutingModule that exports RouterModule and added it to the imports array in AppModule. Additionally, I have the appComponent which was generated by an ...

Can all intervals set within NGZone be cleared?

Within my Angular2 component, I have a custom 3rd party JQuery plugin that is initialized in the OnInit event. Unfortunately, this 3rd party library uses setIntervals extensively. This poses a problem when navigating away from the view as the intervals rem ...

Updating a nested tag within a JSON column in Postgresql

Here is a JSON field that I am working with: { "Id": "64848e27-c25d-4f15-99db-b476d868b575", "Associations_": [ "RatingBlockPinDatum" ], "RatingScenarioId": "00572f95-9b81-4f7e-a359-3df06b093d4d", "RatingBlockPinDatum": [ ...

Using @HostBinding based on the @Input() condition

I'm attempting to link the CSS class foo to my parent component by utilizing @HostBinding based on a condition I evaluate against a dynamic variable. However, I am struggling to get it to function as expected. Here are the different approaches I hav ...

Sending JSON data from an iOS app to a Flask backend

Within my Flask Python web application, I store certain parameters in SessionStorage to later send back to Flask and save this data as a text file. Interestingly, the process functions perfectly on PCs and Android devices but encounters issues on iOS devi ...

Steps for converting a JSON response into a .json file.Steps to transform a

I am looking to create a .json file within my local project directory. My goal is to store the response from a fetch API call, which is an array of objects, into a .json file. Here is the code snippet I am working with: ts : getRecords(){ this.serv ...

Provider in Angular2 that relies on multiple other Providers

Within my Angular2 application, I have various modules that offer Services and Configuration. Now, I want to integrate the @ngrx/store, which gathers reducers from all my modules. Below is the bootstrap code snippet: import {OpaqueToken} from 'angu ...

Issue encountered while integrating the angular-fontawesome library in StackBlitz

After trying to add a library, an error keeps popping up on stackblitz. The error message in ~/src/main.ts states that ngcc failed to run on @fortawesome/[email protected]. Link to the Stackblitz project Is anyone else experiencing this issue? ...

"Exploring the differences between normalization structures and observable entities in ngrx

I'm currently grappling with the concept of "entity arrays" in my ngrx Store. Let's say I have a collection of PlanDTO retrieved from my api server. Based on the research I've done, it seems necessary to set up a kind of "table" to store th ...

What is the most effective way to retrieve the value of a child component in Angular 2 and pass it to the parent component?

I am working on a project with a child component (calendar) and a parent component (form). I need to select a value in the calendar and then access that value in the form component. What is the best way to achieve this? Child Component.ts: import { ...

``There seems to be a problem with the ngb time picker when using the up and

Currently, I am utilizing Bootstrap 4 and NG Bootstrap time picker for a project in Angular 10. Despite correctly adding all the code, I have encountered an issue where the up and down arrows on the time picker are not functioning as expected. Below is a s ...

Step-by-step guide on generating a JSON object list in Python 2.7

Currently, I am in the process of converting an XML file to a JSON file. To accomplish this task, I begin by opening the XML file, utilizing the xmltodict module, and then employing the .get method to navigate the tree structure up to the desired level. Th ...

Making if-else statements easier

Greetings! I have a JSON data that looks like this: { "details": { "data1": { "monthToDate":1000, "firstLastMonth":"December", "firstLa ...

Creating a floating window in pyqt5 upon pressing the user button - what steps to follow?

Exploring the world of pyqt5, I recently created a window using the following code: class Ui_menu(QtWidgets.QMainWindow): def __init__(self): super(Ui_menu, self).__init__() # Call the inherited classes __init__ method uic.loadUi(' ...

What is the importance of specifying a language version when parsing a TypeScript source file?

Currently, we are utilizing the public API to analyze TypeScript files in this manner: ts.createSourceFile( file.name, file.textContent, languageVersion, /*setParentNodes*/ true); Something that has caught our attention is the significanc ...

Develop a Typescript interface containing all the necessary properties for a specific JSX component

My goal is to create a ControlledInputComponent that consists of a label and an input inside a div. I aim to make it highly customizable as I plan on using it in various ways. At the moment, I have included all the necessary elements, but I would like to e ...

Exploring ways to incorporate conditional imports without the need for personalized webpack settings!

Our project is designed to be compatible with both Cordova and Electron, using a single codebase. This means it must import Node APIs and modules specific to Node for Electron, while ignoring them in Cordova. Previously, we relied on a custom webpack con ...

An error is thrown when attempting to use npm install, stating "integrity checksum failed. The expected sha1 checksum was sha1-6G...=, but the actual checksum was sha512

I have been browsing through various posts on different platforms trying to solve my issue, but unfortunately, I haven't had any luck. Despite having no prior experience with Angular, I was tasked with installing npm and running an unfamiliar Angular ...

Error encountered when trying to execute LaunchRequest for Alexa skill: The skill's response failed to properly execute

I am facing an issue with launching my Alexa skill called "Bigger Brain". Whenever I try to open it by saying "Open Bigger Brain" or "Launch Bigger Brain", Alexa responds with "There was a problem with the requested skill's response". I have tried tro ...