Delete specific rows by clicking a button in AngularJS

I have a table with checkboxes for each row and I am trying remove the selected rows when a button is clicked. The selected row indexes are stored in an array using ng-change, but I cannot figure out how to delete them all at once with a single button click.

Here is the Fiddle

HTML

<div ng-app="approvalApp">
<div ng-controller="SimpleApprovalController" >
<table style="width:90%" border="5"  >
<tr>   
    <th><input type="checkbox" ng-model="CheckAllData" ng-change="selectAll()" /></th>
    <th>Date</th>
   <th>AssociateID</th>   
<th>Check-In</th>
<th>Checkout</th> 
</tr>

<tr data-ng-repeat="approval in approvalitems">     
    <td><input type="checkbox" value="{{approval.ReqId}}" data-ng-model="approval.selected" data-ng-change="SelectDeselect($index)"/></td>
    <td>{{approval.Date}}</td>
    <td>{{approval.AssociateID}}</td>
    <td>{{approval.CheckIn}}</td>
    <td>{{approval.Checkout}}</td>

</tr>
</table>
<input type="button" value="Approve" data-ng-model="ApproveIndex" data-ng-click="ApproveRequest()" />

Script

    $scope.SelectDeselect=function(index)
    {
        $scope.getIndexvalues = [];
        angular.forEach($scope.approvalitems, function (approval,index) {               
            if (!!approval.selected) {
               $scope.getIndexvalues.push(index);
                $scope.CheckAllData = false;                 
            }              
        });

        console.log($scope.getIndexvalues);
    };

$scope.ApproveRequest = function () {           
        $scope.selectedIdsArray = [{}];          
        angular.forEach($scope.approvalitems, function (item) {                
            if (!!item.selected) { 
                $scope.selectedIdsArray.push({ Reqid: item.ReqId, Status: "Approved" });
                $scope.CheckAllData = false; 
            }
        });           

    };
};

Explore how to use getIndexvalues in approverequest function or if there is a better way to achieve this using other Angular directives as I am new to AngularJS.

Answer №1

Here is a fiddle link: http://jsfiddle.net/jpk547zp/1/

$scope.ApproveRequest = function() {
    $scope.selectedIdsArray = [{}];
    $scope.approvalitemsNew = [];
    angular.forEach($scope.approvalitems, function(item) {
        if (!!item.selected) {
            $scope.selectedIdsArray.push({ Reqid: item.Date, Status: "Approved" });
            $scope.CheckAllData = false;
            item.hideThis = true;
            console.log($scope.selectedIdsArray);
        } else {
            $scope.approvalitemsNew.push(item);
        }
    });
    $scope.approvalitems = $scope.approvalitemsNew;
    $scope.getIndexvalues = [];
};

I hope this code snippet proves useful to you.

Answer №2

Here is a straightforward method:

$scope.ApproveRequest = function () {           
  $scope.approvalitems = $scope.approvalitems.filter(function(item){
      return !item.selected;
  });
};

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

How can I make my Background change on click using hexadecimal color codes?

I am struggling with changing the background color of an html document using a button click. While colors like "yellow, "blue", and "red" work perfectly, I encounter an issue when trying to use hexadecimal colors such as "#000000". The if-condition doesn ...

Error message: When attempting to access firebase data in our app, an uncaught TypeError is thrown, stating that it is unable to read properties of undefined while trying to access '

While using Firebase 9 and Vue3, I encountered an error when trying to access data from a document. Even though the data was correctly logged in the console, it was showing up as undefined. Additionally, I made sure that the data was loaded before attempti ...

Passing Data to an AngularJS Directive

Hello fellow developers, I'm currently diving into the world of AngularJS and embarking on my journey of creating my very first directive. I've managed to make some progress but I've hit a roadblock when it comes to passing variables to my d ...

Unable to synchronize Rijdnael encryption across C# and Javascript/Node platforms

I have encountered an issue while trying to convert a Rijndael encryption function from C# to Node. Despite using the same Key, IV, Mode, and Block Size, I am unable to achieve matching results. What could be causing this discrepancy? C# MRE: using System ...

Learn how to automatically populate input fields based on the values entered in previous fields. These dynamic fields are generated based on the user

In the following code snippet, fields are dynamically created using jQuery... For more visual explanation, refer to this image: The goal is to calculate the grand total based on previous inputs and display the result when an onclick() event occurs. < ...

Troubleshooting problem with Shopify mailto tag

I am facing an issue with external links in my Shopify store. My app injects a script to display a bubble with an anchor tag that redirects users to a specified link. However, Shopify is altering the anchor tag to a different link, resulting in a 404 erro ...

In search of advice on the best web-based database management technology

I'm looking to create a prototype for a web-based database manager, similar to the desktop version in the image below, with specific features in mind. Initially, the schema will be provided through a flat file. Considering HTML5 as an option, I am a ...

When using i18next interpolation in React components, the displayed output may show as [object Object]

In my React application, I am utilizing the next-i18next package. I want to include a React component within my interpolation: import React from 'react'; import { useTranslation } from 'next-i18next'; import { serverSideTranslations } f ...

The issue with Angular 1.6 not displaying the scope value in the template

Hey there! I'm currently working on index.html Here's the code snippet from before: <body ng-app="MainController"> <div class="page page-base {{ pageClass }}" ng-view> </div> </div> Then, I made changes by ass ...

Enclose the content in a div element and then append it using the appendTo

Attempted to enclose appendcontent within a image div, but received [object Object] as the output. $("<div class=image>" + appendcontent + "</div>").appendTo($('.outside')); Is there a way to insert $(appendcontent) inside $("<d ...

The Angular $http is triggering an endless loop, resulting in the error message: "Error: 10 $digest() iterations reached and now aborting

I am facing a challenge with Angular $http promise. It appears that the $http promise is causing an infinite loop with the error message "error : 10 $digest() iterations reached. Aborting!" I would appreciate any advice on how to resolve this issue. Just ...

Is it considered good or bad practice to use plain JavaScript objects in an AngularJS application?

Imagine needing a custom object that doesn't rely on AngularJS (such as a specific collection with unique functionalities). You could create it independently of AngularJS and simply use it in services/controllers. Alternatively, you could design it a ...

Modify the hue of the iron-icon upon being tapped

There's a simple example I have where my goal is to modify the color of an iron-icon when it's tapped. To achieve this, I'm utilizing iron-selector for tapping: <template> <style> :host { display: block; padding: 10 ...

What is the process for traversing through a multi-level nested JSON array?

I have a JSON array with a unique structure: [{"a":"b","child":[{"a":"c","child":["a":"d","child":[]]}] This array can have any number of levels and the levels are d ...

AngularJS - ng-switch directive causing issues with nested directive

I have a directive that is nested within the ng-switch element like this: <div ng-switch on="myModel"> <div ng-switch-when="foo"> <zippy></zippy> </div> //Other ng-switch-when directives </div> The ...

A comprehensive guide on harnessing the power of server-sent events in express.js

After incorporating the express.js framework, I configured my REST server. Now, I am interested in integrating server-sent events (sse) into this server. However, upon implementing the sse package from npmjs.com, an error occurs. It seems that the error is ...

Why does WebdriverIO send a POST request for an unused webelement?

Exploring the capabilities of webdriverIO has been quite enjoyable! There are many features of this framework that I already appreciate. In my investigation, I wanted to see how WebdriverIO handles non-existing elements and lazy element loading in this te ...

Sending a large CSV file from a Flask backend to an AngularJS frontend for downloading

I am currently working on a Flask application that executes an API query to retrieve data from a database. This data is returned in the form of a large JSON file, typically around 20-50MB in size. My plan is to convert this JSON response into CSV format ...

Tips for seamlessly incorporating a fresh ionic theme into your current ionic application

I am currently developing a project with Ionic v1 and Angular 1, using the basic Ionic UI. While browsing through the Ionic marketplace, I found several interesting themes. Can someone guide me on how to implement a new Ionic theme into my existing appli ...

Exploring the world of JavaScript by dynamically retrieving all class functions

Is there a way to retrieve an array of all functions from a given class, including functions inherited from parent classes? For instance: class Foo extends Bar { funcA() {} } class Bar { funcB() {} } const instanceFoo = new Foo(); getClass ...