Angular checklist-model checkboxes with reverse action

2.5k views Asked by At

I'm working with checklist-model.js for angular to select from dynamically generated list of objects. It's working fine, but now I need to make it work in reverse so when I uncheck any of checkbox - place it to new array (and when checked back - remove from array). Can any of You give me some ideas or tell me next steps how to deal with it?

html:

<label>
    <input type="checkbox"
        ng-model="check_all_domains"
        ng-click="toggle_select_all()"/> all
</label>
<label ng-repeat="objects in objects_model">
    <input type="checkbox"
        checklist-model="objects_selected"
        checklist-value="objects"
        ng-checked="check_all_domains"/>
    {{objects.name}}
</label>

model:

$scope.objects_model = [
    {id : '1', name: 'name1'},
    {id : '2', name: 'name2'},
    {id : '3', name: 'name3'},
  ];
$scope.objects_selected = [];
$scope.check_all_domains = false;

$scope.toggle_select_all = function() {
    $scope.objects_selected = [];
};

here is screenshot how it's working right now:

enter image description here

and here is how I want it to work:

enter image description here

UPDATED: WORKING AS IT SHOULD DEMO

2

There are 2 answers

2
test1n On BEST ANSWER

Updated my demo in question so have a look at the final result

<label ng-repeat="objects in objects_model" class="test">
    <input type="checkbox" 
          checklist-model="objects_selected" 
          checklist-value="objects" />
    <i ng-class="{checked : check_all_domains, 
                unchecked : !check_all_domains, 
                fakecheck : check_all_domains}"></i>{{objects.name}}
  </label>
0
Rus Paul On

I had a similar issue with checklist-model and I've managed to create a workaround

Although its a pretty nasty solution, it works:

$scope.toggle_select_all = function() {
   $timeout(function() {
       $scope.check_all_domains = $scope.check_all_domains ? false : true;
   });

   if (!$scope.check_all_domains) {
      angular.copy($scope.objects_model, $scope.objects_selected);
   } else {
      angular.copy([], $scope.objects_selected);
   }
};

See this plunkr: http://plnkr.co/edit/CiXO1debaDkKPHPYKNfT?p=preview


I'd highly suggest searching for an alternative, cause later on you'll see that it pays off.

For me this worked: http://jsfiddle.net/cjwprostar/M4vGj/6/ - Chris Waguespack

Source: https://groups.google.com/forum/#!topic/angular/KMS5hXn1OCI