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

What is the best way to specify when form inputs are required in AngularJS?

Imagine developing a contact management application (for demonstration purposes) utilizing AngularJS. We have crafted a contact form that includes fields for email and phone number. We aim to make one of the two mandatory, but not both: The email field sh ...

Creating a URL using Form Fields with Javascript or jQuery - Reg

Creating a Custom URL with Form Fields using JavaScript or jQuery I am looking to generate an external link by incorporating a form with a dynamic variable as shown below: (Where 2500 can be customized based on user input) The final URL will be display ...

Tips for sending the setState function to a different function and utilizing it to identify values in a material-ui select and manage the "value is undefined" issue

I am currently utilizing a Material UI select component that is populated with data from an array containing values and options. Within this array, there exists a nested object property named "setFilter". The setFilter property holds the value of setState ...

How to interrupt a JQuery animation and restart it from the middle?

Currently, I am tackling my first JQuery project and facing a challenge. As the user mouseleaves the .container, the animation does not reset but continues as if they are still within it. My goal is to have the animation revert in reverse if the user decid ...

This asynchronous function will provide a response of "undefined"

Having a challenge with an async function that should return a geocode value: async function getLocationCoordinates(place){ //var str; return googleMapsClient.geocode({ address: place }).asPromise() .then((response) => { response.json.results[0].ge ...

Merge arrays with identical names within the same object into one cohesive object containing all elements

I just started using Vue and I'm not entirely sure if it's possible to achieve what I have in mind. Here is the structure I have: { "items":[ { "total":1287, "currency":"USD", "name":"string", "itemID":"", "pro ...

Enhancing presentation with a multitude of pictures

I am currently in the process of developing a slideshow feature that includes an extensive collection of images for users to navigate through using 'next' and 'previous' buttons. At the moment, each time a user clicks on the navigation ...

What sets apart "config" from "defaults" in Sencha Touch?

As a newcomer to Sencha Touch, I have one simple question that's been on my mind... Can someone explain the distinction between "config" and "defaults" in Sencha Touch? ...

Creating a text design that spans two lines using Scalable Vector Graphics (SVG

I am working with an SVG that displays strings pulled from an Array... {"label":"Here is an example of the string...", "value":4}, The text above is shown in an SVG element as... <text>Here is an example of the string...<text> I would like ...

Steps for adjusting the status of an interface key to required or optional in a dynamic manner

Imagine a scenario where there is a predefined type: interface Test { foo: number; bar?: { name: string; }; } const obj: Test; // The property 'bar' in the object 'obj' is currently optional Now consider a situatio ...

Determine the position and quantity of elements in jQuery by comparing their IDs with the current page or element

Looking to retrieve the n (zero based) position of an element by matching the page and element ID... Let's use an example (Assume the current page ID is 488); <ul id="work-grid"> <li id="item-486" class="work-item"><!--/content--& ...

Dealing with an unexpected token error in JSON? Learn how to properly handle removing special characters like quotation marks and line breaks to

After receiving a JSON response from the server during page load, I successfully populate it on the page using Handlebars.js. However, I am facing difficulties in storing this JSON object in a JavaScript object. Here is what I tried: var jsObject = "{{o ...

Weapons of Mass Destruction - receive markdown content

My application is utilizing a markdown editor from Google Code. $(document).ready(function () { var converter = Markdown.getSanitizingConverter(); var editor = new Markdown.Editor(converter); editor.run(); }); <div class="wmd-panel"> ...

Collaborating on two AngularJS directives with a third party

Update Here is the link to my plunk These are the two lines in the directive 'myPane' that are causing issues. require: '^myMainTabs', //Comment out this line and uncomment below to cause problem. //require: ['^myMainTabs', ...

Certain mobile devices experiencing issues with AngularJS filters

Currently, I am attempting to filter an AngularJS array by utilizing custom filters within a controller. The filters are functioning correctly on certain mobile devices, yet they are not working on others. Here is the code snippet that I am using: var a ...

Sharing data between controllers within an MVC architecture in JavaScript

I'm currently working on an app using Express and Node. Within my routes, I have '/new-poll' and '/poll-create' //The route poll-create allows users to create a new poll app.route('/poll-create') .get(functi ...

Exploring the Node Promise Chain: Utilizing Local Functions and Managing Multiple Value Passing in a Code Review

Upon reviewing the code provided, several questions have arisen: #1 I am curious about the best way to make the values returned by the bluebird.all functions accessible in subsequent functions. Is using the this-context a viable option without declaring t ...

Utilizing React Router to dynamically render components based on JSON object data

My current challenge involves rendering React components based on the component names in a JSON file I've created. Specifically, I want to render the TestSection component within the context of my Route component. To achieve this, I am utilizing the ...

Unable to pass a variable through ajax to another PHP file

I am currently facing an issue with passing a variable from one PHP file to another using ajax. In my 'index.php' file, there is a button that, when clicked, should pass the button's id to another PHP file named 'show_schedule.php' ...

Is there a way to ensure that GIFs in jQuery Mobile always start from the beginning?

My cross-platform mobile app built with jQuery Mobile is a small quiz application. After each correct or wrong answer, I display a 3-second GIF. Currently, I have set up the code to show the GIF for 3 seconds before moving on to the next page: else if ($. ...