简体   繁体   中英

Angular checklist-model checkboxes with reverse action

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 :

在此处输入图片说明

and here is how I want it to work:

在此处输入图片说明

UPDATED: WORKING AS IT SHOULD DEMO

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM