How to Delete Multiple Rows from an Angular 4 Table

I have successfully figured out how to remove a single row from a table using splice method. Now, I am looking to extend this functionality to remove multiple rows at once.

html

<tr *ngFor="let member of members; let i = index;">
    <td>
      <input type="checkbox" (change)="selChk(member.id, i)"  
      [checked]="false">
    </td>
</tr>

This will label each row as 0, 1, 2, 3, and so on with every loop iteration.

component

Below is the list of ids from checkboxes that have been checked.

For instance, if I check the first and second rows, it will capture rows 0 and 1. Later, I can click on the remove multiple buttons to use these rows for splicing.

selChk(val:number, ind:number) {

  var i =  this.id.indexOf(val);
   if(i === -1){
     this.id.push(val);
     this.ind.push(index);
   }else{
     this.id.splice(index,1);
   }

}

The following code snippet will execute when the remove multiple button is clicked. It logs the indices of the rows that need to be deleted. However, I am unsure about how to perform multiple splices at once.

 removeSel() {
   console.log(this.ind);
  }

Answer №1

Include the following function in your component's code:


deleteSelectedItems() {
  this.items = this.items.filter((_, index) => this.selectedIndexes.indexOf(index) === -1)
}

Invoke the deleteSelectedItems function when the remove multiple button is clicked: (click)="deleteSelectedItems()"

Reference: MDN Filter

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

Utilizing precise data types for return values in React hooks with Typescript based on argument types

I developed a react hook that resembles the following structure: export const useForm = <T>(values: T) => { const [formData, setFormData] = useState<FormFieldData<T>>({}); useEffect(() => { const fields = {}; for (const ...

Using Formik inside Material-UI's table components

My goal is to design a material UI table where each cell acts as a Formik input. However, I've encountered errors with Material UI when attempting to use a Formik Object within TableBody or TableItem tags. Here's an example image of what I' ...

Difficulty commencing a background operation in a node.js application

I have a setup using node.js/express and React for the client side code. This setup allows users to query any number of players by making fetch requests to my express server, which then sends requests to Riot Games public API. The issue I'm encounteri ...

Having trouble loading static files in Django Angular framework? Specifically, images stored in the assets folder returning a

In my Angular project, I have an assets/image folder within the src directory where all my images are stored. Various components and child components in my app use these images like <img src"../../../assets/image/test.png">. After building my Angula ...

Extension for capturing videos on Chrome or Firefox

I am interested in developing a Chrome or Firefox extension that can capture video from a window or tab. My goal is to record full screen videos, such as those on YouTube, for offline viewing similar to a DVR for online content. Creating an extension see ...

Having trouble executing the project using Gulp

I'm a beginner in front-end development and I am working on an existing project that I'm having trouble running. According to the documentation, I should run the project by executing: $ gulp && gulp serve But I keep getting this error: ...

Is it possible to utilize Angular's dependency injection in place of RequireJS?

As a newcomer to Angular, I am wondering how I can organize my code into separate files without using requirejs or any other framework. After watching a brief introductory video, it seemed possible. Currently, my app looks like this and functions well: v ...

What is the best way to implement client side validation for a related input field using JavaScript in a Rails application?

Struggling to find a solution for adding a validation check for a dropdown list that is dependent on another input selection in a different form. It seems like database validation may be necessary, but I'm unsure of the best approach. Any suggestions ...

Determining the total number of current connections using JavaScript, jQuery, and Selenium WebDriver

I need your assistance as I've encountered a roadblock. In my automated tests using Selenium WebDriver + Java, I rely on the following code snippet to check for active background AJAX connections: private boolean hasNoActiveConnections() { retur ...

The Angular ui-calendar introduces an innovative approach to event addition, providing users with

I need help with adding events to the angular ui calendar. Currently, I am using $scope.events.push() method to add events, but they get reset when changing the month. If anyone has experience with angular ui-calendar and event addition, please provide ...

Pages in Angular are being loaded multiple times when they are loaded with various parameter values

I have encountered an issue with my page setup that involves contracts and chats. Each chat corresponds to a specific contract, and depending on which chat or contract is open, the URL changes accordingly: david/contracts -> david/contracts/12 david/c ...

React Quill editor fails to render properly in React project

In the process of developing an application in Ionic React, I am in need of a rich text editor that can save data on Firestore. Despite initializing the editor as shown below and adding it to the homepage component, it is not rendering correctly although i ...

What is the best way to resize an element such as an image?

When an image is resized using a percentage, it preserves its aspect ratio properly. I am looking for a way to replicate this behavior with a div. My current challenge involves precisely positioning an element relative to an image. The image fills 100% of ...

Ways to adjust the border color when error helper text is displayed on a TextField

I am currently working with React Material UI's TextField element. What I aim to achieve is when the submit button is pressed, the helpertext displayed should be "Please enter the password." It should look like this: However, once the helpertext is ...

Is there a way to display a variety of images on separate divs using *ngFor or another method?

I have created a custom gallery and now I would like to apply titration on the wrapper in order to display a different image on each div. Currently, my code is repeating a single image throughout the entire gallery. HTML <div class="wrapper" ...

Error: Attempting to access 'forEach' property on an undefined variable at line 10 in the script file

I'm completely new to Javascript and I apologize for my messy code. I've been struggling with this error for a while now, both on Replit and Visual Studio, but I can't seem to figure it out. Any help would be greatly appreciated! Every time ...

When attempting to showcase array information in React, there seems to be an issue with the output

After printing console.log(response.data), the following output is displayed in the console. https://i.sstatic.net/LLmDG.png A hook was created as follows: const [result,setResult] = useState([]); The API output was then assigned to the hook with: setRe ...

`Implementing a Reusable RadioButton Component in Angular2`

When trying to reuse my component on the same page, I encountered an issue with defining the value of Radio Buttons based on the specific component I'm working with. I have three shared-components that need to be distinguished. If I modify one compon ...

typescript undefined subscription to observable

After making an http request to fetch some data, I am facing issues in displaying it as intended. The dropdown select for entriesPerPage, and the left and right cursors for switching page in pagination are working fine. However, upon switching a page, I en ...

API response containing JSON data is not being displayed properly in the webdatarocks angular component

I can't seem to figure out how to properly display the JSON formatted data returned by a REST API using Angular. Any suggestions on how to accomplish this? Here's what I've been attempting to do - fetchData() { this.service.fetchData().s ...