Using Angular 4 to delete selected rows based on user input in typescript

I am facing a challenge with a table that contains rows and checkboxes. There is one main checkbox in the header along with multiple checkboxes for each row. I am now searching for a function that can delete rows from the table when a delete button is clicked. The challenge is that it should delete all rows if the top checkbox is selected, which selects all rows. Additionally, it should be able to delete a single row when a specific row's checkbox is clicked. Below, I have provided the code snippet and a plunkr example from my application to give a clearer idea of row selection.

https://plnkr.co/edit/7l4AltdWfLGdLaXgF4vl?p=info

@Component({
      select
or: 'my-app',
      template: `
        <div>
          <h2>Hello Angular2</h2>
          <table class="table table-bordered table-condensed table-striped table-hover">
            <thead>
                <tr>
                    <th></th>
                    <th>Size</th>
                    <th>Diameter</th>
                    <th class="text-center">
                        <input type="checkbox" name="all" [checked]="isAllChecked()" (change)="checkAll($event)"/>
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr *ngFor="let size of sizes ; let i = index">
                    <td class="text-right">{{i + 1}}</td>
                    <td class="text-right">{{size.size}}</td>
                    <td>{{size.diameter}}</td>
                    <td class="text-center">
                        <input type="checkbox" name="sizecb[]" value="{{size.id}}" [(ngModel)]="size.state"/>
                    </td>
                </tr>
            </tbody>
          </table>
        </div>
      `,
    })
    export class App {
      sizes: any[] = [
        { 'size': '0', 'diameter': '16000 km' },
        { 'size': '1', 'diameter': '32000 km' },
{ 'size': '1', 'diameter': '32000 km' },
{ 'size': '1', 'diameter': '32000 km' },
{ 'size': '1', 'diameter': '32000 km' },
{ 'size': '1', 'diameter': '32000 km' },
{ 'size': '1', 'diameter': '32000 km' },
{ 'size': '1', 'diameter': '32000 km' }
      ];

      checkAll(ev) {
        this.sizes.forEach(x => x.state = ev.target.checked)
      }

      isAllChecked() {
        return this.sizes.every(_ => _.state);
      }
    }

    @NgModule({
      imports: [ BrowserModule, FormsModule ],
      declarations: [ App ],
      bootstrap: [ App ]
    })
    export class AppModule {}

Answer №1

Could you please verify the content in this link

verifyContent(size) {
    size.status = !size.status;
}
  
removeContent() {
    this.contents.forEach(x => {
      if(x.status) { alert(x.content); }
    });
}
<td class="text-center">
  <input type="checkbox" name="contentcb[]" value="{{content.id}}"
  [(ngModel)]="content.state" (click)="verifyContent(content)"/>
</td>
...
<button (click)="deleteContent()">Delete</button>
...

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

Display elements exclusively when the class XY is in an active state using JavaScript

Hello everyone, I'm new to this platform and excited to share my first post. Currently, I find myself facing a major challenge as I navigate through a bootcamp program. I have been working on a website with different sections that require specific fu ...

Creating raw JavaScript from npm package:How to convert npm modules into plain

Is there a way to get a pre-compiled plain JavaScript file from the repository https://github.com/airbrake/airbrake-js? I'm looking for a solution without using bower, npm or any other tools - just a single JavaScript file. By the way: The file "http ...

What is the functionality of the getBlock('meta') method within DocPad?

While transitioning a website from a different site generator to DocPad, I am currently exploring the capabilities of the getBlock('meta') feature. Understanding how to utilize getBlock('scripts') and getBlock('styles') was re ...

How can I align rectangles with different heights to display side by side using Javascript?

Currently, I am designing a press page for a website where the headlines/articles are displayed in rectangles. To achieve this layout, I am using the following CSS: .press-blocks{ column-count: 4; column-gap: 2em; padding-left: 10%; padding ...

JavaScript Promise Handling: using fetch method to retrieve and extract the PromiseValue

I am currently struggling to access the [[PromiseValue]] of a Promise. However, my function is returning a Promise instead and what I really want myFunction to return is the value stored in [[PromiseValue]] of the promised returned. The current situation ...

"Ionic Calendar 2 - The ultimate tool for organizing your

I am using a calendar in my Ionic app that retrieves events from a database through an API. var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://portalemme2.com.br/SaoJoseAPI/agenda', true); this.http.get('http://portalemme2.c ...

Adjust the ng-show attribute in a different controller

I am attempting to toggle the visibility of a div using ng-show. Specifically, I have a navbar that should only be displayed in certain views. I have one controller managing the behavior of this div, and another controller where I want to manipulate the v ...

JavaScript form submission failing to transmit updated data

I have been working on a JavaScript function that changes the hidden value of a form based on which button is clicked, and then sends it via post to a processing page. Even though I have confirmed that the value is being changed correctly, when the post i ...

problem encountered while attempting to transmit data to multer in React

I was attempting to upload an image to the backend using Multer. I have reviewed the backend code multiple times and it appears to be correct. Could there be an issue with my front-end code? Here is a POST code snippet: const response = await fetch(' ...

React Typescript Mui causing `onBlur` event to trigger with every change occurring

I'm currently developing a front-end application using Typescript and React with MUI. The form code I have written is as follows: <TextField label="Password" value={this.state.password} placeholder="Choose a password" type="password" onC ...

I am working with an array called officeLocations, and I am looking to showcase it in an HTML format using the isOpened property in Angular

In my officeLocation.ts file, I have an array called officeLocationsArray structured like this: export class OfficeLocation { officeLocationName:string; Isopened:boolean; } export const officeLocationArray : OfficeLocation[] = [ {officeLocatio ...

Trouble getting search filter to function properly on an array of objects in a Vue JavaScript application

Can someone help me with integrating this code into my project? Here is the code I am referencing: http://jsfiddle.net/dkmmhf5y/701/ This snippet shows my JavaScript file, please disregard the 'test' data var app = new Vue ( { el: &apos ...

Hiding Modal Box Upon User Login: A Step-by-Step Guide

After a user clicks the login button in my navigation, a modal box pops up. However, once the user logs in, the modal box does not disappear. How can I hide or remove the modal box when users click on the login button? This code snippet is from Home.vue: ...

I am eager to create a Material-UI textfield using an array

My current task involves utilizing TextField based on an Array, but I am facing an issue with dynamically changing all the values. I understand that I can employ ternary conditions to accomplish this task effectively. const TextField = () => { r ...

What is the process for spawning a terminal instance within a NodeJS child process?

I'm in the process of setting up a discord channel to serve as an SSH terminal. This involves using a NodeJS server to establish the connection. A custom command will then be used to spawn a new terminal instance that can function as a shell. However ...

What is the best method for storing objects in Firebase in order to easily rearrange them at a later time?

My list looks something like this: {"ids": [ { "id1": { "name": "name1", "lastname": "lastname1" } }, { "id2": { "name": "name2", "lastname": "lastname2" } }, { "id3": { "name": "name3", "l ...

Swap out The div element with the html() method

I'm encountering an issue when trying to swap out a div element with ajax. My goal is to create a start/stop button functionality. The page displays a list of card elements, each with its own button, which are stored in separate html files. work_orde ...

Using Styled components and react-router-dom v6 to emphasize the active navigation link upon clicking

I'm just starting out with react and I am trying to figure out how to keep my hover style active while the current page is open. I think I need to set a condition but I am not sure how. const StyledLi = styled.li` `list-style: none; displa ...

NGXS State Mutation Event

In my NGXS store, I have an object called obj = { name: 'so-and-so', age: 42, nationality: 'country-name', address: { ... } }. It is essential that whenever any of the properties, including nested ones like 'address', are upda ...

Navigating through alterations in intricate data utilizing the BehaviorSubject concept

If I have a Service that offers a BehaviorSubject to any component needing the most up-to-date version of certain data, how can these components differentiate what changed in the data when they receive notifications? export class DataService { pri ...