Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete the row in which a ng-click is located?

In the following code, when I delete a customer, I want the TR row to disappear.

What is the best way to do this? Do I need to send the row as a parameter to deleteCustomer somehow? Do I have access to the TR DOM element within AngularJS somehow?

<html ng-app="mainModule">
    <head>
        <script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
        <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
    </head>    
    <body ng-controller="mainController" style="padding: 20px">
        <div class="col-lg-5">
            <table style="width: 500px" class="table-striped table-bordered table table-hover">
                <tr ng-repeat="customer in customers">
                    <td style="width:50px"><button ng-click="deleteCustomer(customer)">Delete</button></td>
                    <td style="text-align:right">{{customer.id}}</td>
                    <td>{{customer.firstName}}</td>
                    <td>{{customer.lastName}}</td>
                </tr>
            </table>
        </div>
        <div class="col-lg-7">
            <div class="panel panel-info">
                <div class="panel-heading">Logger</div>
                <div class="panel-body" style="padding:0">
                    <table class="table table-bordered" style="margin:0">
                        <tr ng-repeat="loggedAction in loggedActions">
                            <td>{{loggedAction.action}}</td>
                            <td>{{loggedAction.description}}</td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
        <script>
        var mainModule = angular.module('mainModule', []);
        function mainController($scope) {

            $scope.loggedActions = [];

            $scope.customers = [
                {id: 1, firstName: 'Joe', lastName: 'Thompson'},
                {id: 2, firstName: 'Hank', lastName: 'Howards'},
                {id: 3, firstName: 'Zoe', lastName: 'Frappe'}
            ];

            $scope.deleteCustomer = function (customer) {
                $scope.$emit('customerDeleted', customer);
            };

            $scope.$on('customerDeleted', function (event, customer) {
                $scope.loggedActions.push({action: 'delete', description: 'Deleted customer ' + customer.firstName + ' ' + customer.lastName});
            });
        }
        </script>
    </body>
</html>
like image 323
Edward Tanguay Avatar asked Jan 07 '15 11:01

Edward Tanguay


People also ask

How to delete the row in Angular?

Here on click of delete button our deleteRow() function gets fire and using splice we remove the selected rows. Conclusion: Directive ng-repeat is used to Bind data to our HTML table. Directive ng-click is applied on the Delete button which fires deleteRow() function. And with splice remove the selected row.

What is the difference between click and Ng-click?

Another significant difference between ng-click and onclick is the execution context. Code inside an onclick attribute executes against the global window object, while an expression inside of ng-click executes against a specific scope object, typically the scope object representing the model for the current controller.

What is the use of NG-click?

The ng-click directive tells AngularJS what to do when an HTML element is clicked.

Can we call two functions on Ng-click?

When an HTML is clicked, the ng-click directive tells the AngularJS script what to do. In this article, we will learn how to get many/multiple functions to the ng-click directive passed, in just one click. The key is to add a semi-colon (;) or a comma (,). All the functions must be separated by a (;) or a (, ).


1 Answers

EDIT:

as pointed out by @K.Toress's comment, it's better to retrieve the index of the deleted customer via indexOf() from within the function, rather than passing $index from the ng-repeat. passing $index will give unexpected results if using a filter or sorting the array.

deleteCustomer function:

$scope.deleteCustomer = function (customer) {
  var index = $scope.customers.indexOf(customer);
  $scope.customers.splice(index, 1);
  $scope.$emit('customerDeleted', customer); 
};

updated plnkr


you can use the $index provided by ng-repeat, and array.splice from within the delete function:

html:

<button ng-click="deleteCustomer($index, customer)">Delete</button>

js:

$scope.deleteCustomer = function ($index, customer) {
  $scope.customers.splice($index, 1);
  $scope.$emit('customerDeleted', customer);
};

plnkr

like image 113
Nitsan Baleli Avatar answered Oct 24 '22 05:10

Nitsan Baleli