How to collect data for the rows that have been selected in AngularJS?

Ashish Agrawal

I am receiving a list of data from server and has displayed that in table format using ng-repeat along with checkbox in each row. My requirement is to pass the selected rows back to server upon clicking a removeUserData button. Am facing issue to get it done, help would be appreciated.

<table border="2" border-color=black>
       <tr data-ng-repeat="user in users">
            <td><input type="checkbox"></td><td>{{user.id}}</td><td>{{user.country}}</td><td>{{user.name}}</td>             
       </tr>
</table><br>
<button data-ng-click="removeUserData()" data-ng-show="users.length">Remove User</button>
developer033

I'd suggest you to make use of a new property in users, something like removed, then when checkbox is checked it will be true, otherwise false.

See it working:

(function() {
  angular
    .module("app", [])
    .controller('MainCtrl', MainCtrl);

  MainCtrl.$inject = ['$scope'];

  function MainCtrl($scope) {
    $scope.removeUserData = removeUserData;

    $scope.users = [  
       {  
          "id":1,
          "country":"Italy",
          "name":"Pavarotti"
       },
       {  
          "id":2,
          "country":"French",
          "name":"Some user"
       }
    ];

    function removeUserData() {
      $scope.users = $scope.users.filter(function(user) {
        return !user.removed;
      })
    }
  }
})();
<!DOCTYPE html>
<html ng-app="app">

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
</head>

<body ng-controller="MainCtrl">
  <table>
    <tr ng-repeat="user in users">
      <td>
        <input type="checkbox" ng-model="user.removed">
      </td>
      <td ng-bind="user.id"></td>
      <td ng-bind="user.country"></td>
      <td ng-bind="user.name"></td>
    </tr>
  </table>
  <div ng-if="users.length">
    <hr>
    <button ng-click="removeUserData()">Remove User</button>
  </div>

</body>

</html>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I select a data from another column from rows that have been selected?

How to get all the rows which have been not selected in a dataset

How do I both randomly select rows from a data frame and delete each row as it has been selected?

how to see highlighted rows that have been edited

How to know how many class elements have been selected on click?

AngularJS: how to copy selected table rows to clipboard?

How can I get a scrollable div element to scroll with rows that have been set up to be selected via arrow keys when the rows are out of view?

Finding and modifying data in rows that have been filtered in a datatables table

How to know if some data have no been extracted

How can I put a textbox inside a table data which rows have been appended/moved from another table?

a[ ][3] How many rows have been created in this array?

How to only select rows that have not been soft deleted in a SQL Query?

How to return jqgrid data of selected rows

How to import database in Yii2, update rows that have changed, delete rows that have been removed

How to remove a data from a list after it has been selected

How to add rows with the average values of selected rows into a data frame

Apply selector to elements that have already been selected

how to hide rows that dont have data in the row

How to collect rows if they have several conditions in only one other column in r?

angularJS with SignalR data how to group table rows

How to set textView when two items have been selected by using two spinners?

How to read which checkboxes have been selected (HTML form to Google Sheet)

67 dynamic checkboxes, how to disable all check boxes after 4 have been selected?

How do I keep track of cells that have been selected using NSIndexPath?

How to display GUI message indicating which checkbutton(s) and radiobutton have been selected in tkinter in Python?

How can we reset a radio button after some other radio buttons in the same group have been selected

How to trigger a button to appear only if multiple options from different drop-down menus have been selected

How to collect chart data

How to give an error message if no value has been selected after button click in AngularJS