0

I have a table in my HTML code in which the rows are generated by AngularJS ng-repeat. Each row contains a checkbox in the first column and other data in the corresponding columns. I am trying to mark all checkboxes as selected when this button which is present in another div is clicked.

<tbody>
  <tr ng-repeat="row in myObject track by $index" ng-class-even="'alternate-row'">
    <td name="checkbox">
      <input type="checkbox" ng-model="row.checked" ng-style="row.checkboxStyle" ng-click="setSelectedRow(row)" />
    </td>
    <td>{{ row.data1 }}</td>
    <td>{{ row.data2 }}</td>
  </tr>
</tbody>

The button is as follows:

<div class="btn-group">
	<button type="button" id="selectAll" class="btn btn-default" title="Select All" ng-click="selectAllGrid()">Select All</button>
</div>

A method is called on the click of the checkbox that adds that particular row to an array. How will that method be called if I mark the checkbox as checked on click of the button.

2 Answers 2

2

use the directive ng-checked

<tbody>
  <tr ng-repeat="row in myObject track by $index" ng-class-even="'alternate-row'">
    <td name="checkbox">
      <input type="checkbox" ng-checked="checkAll" ng-model="row.checked" ng-style="row.checkboxStyle" ng-click="setSelectedRow(row)" />
    </td>
    <td>{{ row.data1 }}</td>
    <td>{{ row.data2 }}</td>
  </tr>
</tbody>

and in your button function

selectAllGrid(){
 $scope.checkAll=true;
}
Sign up to request clarification or add additional context in comments.

1 Comment

it worked. Thanks mate. But the action that was performed when I clicked the checkbox is not happening.. Also after I click the checkbox manually the button stops working and doesn't select any further.
2

You can do like this:

<table>
    <tbody>
      <tr ng-repeat="row in myObject track by $index" ng-class-even="'alternate-row'">
        <td name="checkbox">
          <input type="checkbox" ng-model="row.checked" ng-style="row.checkboxStyle" />{{row.checked}}
        </td>
        <td>{{ row.data }}</td>
      </tr>
    </tbody>
  </table>


  <div class="btn-group">
    <button type="button" id="selectAll" class="btn btn-default" title="Select All" ng-click="selectAllGrid()">Select All</button>
  </div>

and in your controller:

app.controller('MainCtrl', function($scope) {
  $scope.allchecked  = false;

  $scope.selectAllGrid = function(){
    console.log($scope.allchecked);
    $scope.allchecked = !$scope.allchecked
   for(var i =0; i<$scope.myObject.length; i++) {
     $scope.myObject[i].checked = $scope.allchecked ;
   }
  }

  $scope.myObject = [
      {"data": "sdfsdf"},
      {"data": "asdfassafs"},
      {"data": "asesfrsaedfsadfsadsd"}
    ]
});

Working plunker here

4 Comments

Thanks. Its a great answer. But how to call the function/event that was called when I physically marked the checkbox as checked.
Don't understand. Can you explain in detail.
When I manually click the checkbox to check/uncheck it, an event is triggered and I call a method in JS for that event. I populate an array with the values of that row where that checkbox was present. Now when I check/uncheck it with a button, that method is not being fired on checkbox check/uncheck.
Ok. I updated the plunker. Once see that. If I miss anything tell me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.