Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am trying to achieve two things:

  1. Bind an array to a list of checkboxes (just a string array), and

  2. Limit the number of selections the user can make to a number between 1 and the number of available items less 1.

I can get (2) to work until the user clicks the last item, at which point it loses track and the items remain selected.

The interactive code is up here: http://codepen.io/adamcodegarden/pen/bdbQqe (forked from a similar example)

My HTML/Angular code:

<p ng-repeat="item in allOptions" class="item" id="{{item}}">
      {{item}} <input type="checkbox" ng-change="sync(bool, item)" ng-model="bool" ng-checked="isChecked(item)"> Click this to sync this item with the target array.  {{item}} Selected: {{bool}}

and the JS:

var myApp = angular.module("myApp", []);

var maxItems = 1;

myApp.controller('myController', function($scope) {

  $scope.isChecked = function(item){
      var match = false;
      for(var i=0 ; i < $scope.data.length; i++) {
        if($scope.data[i] === item) {
          match = true;
        }
      }
      return match;
  };

  $scope.allOptions = [
    'one', 'two', 'three', 'four'
  ];

  $scope.data = [
  ];

  $scope.sync = function(bool, item){
    if (bool) {
      // add item
      $scope.data.push(item);
      // if we have gone over maxItems:
      if ($scope.data.length > maxItems) {
        //remove oldest item
        $scope.data.splice(0,1);
      }
    } else {
      // remove item
      for (var i=0 ; i < $scope.data.length; i++) {
        if ($scope.data[i] === item){
          $scope.data.splice(i,1);
        }
      }      
    }
  };

});
share|improve this question
    
I think the codepen is not working correctly. I selected all 4 of them , and the Final Data Array shows only 1 item. – ABOS Apr 20 '15 at 15:43
    
That is the desired behaviour - it is limiting the selected array to 1 item, as defined by var maxItems = 1 – Adamski Apr 20 '15 at 16:03
    
I see. So you ARE working on how to sync selection with final data array. – ABOS Apr 20 '15 at 17:32
up vote 2 down vote accepted

I like plunker more than codepen. So I created this plunker

http://plnkr.co/edit/l8gxQHXBQdFeKIuwf3f0?p=preview

The main idea is that I format the original array as:

$scope.allOptions = [
   {key: 'one'}, {key: 'two'}, {key: 'three'}, {key:'four'}
];

And slight change to the sync logic as well:

$scope.sync = function(bool, item){
if (bool) {
  // add item
  $scope.data.push(item);
  // if we have gone over maxItems:
  if ($scope.data.length > maxItems) {
    //remove first item
    $scope.data[0].checked = false;
    $scope.data.splice(0,1);
  }
} else {
  // remove item
  for (var i=0 ; i < $scope.data.length; i++) {
    if ($scope.data[i] === item) {
      $scope.data.splice(i,1);
    }
  }      
}

};

Also change html portion:

<p ng-repeat="item in allOptions" class="item" id="{{item.key}}">
  {{item.key}} <input type="checkbox" ng-change="sync(item.checked, item)"    ng-model="item.checked"> Click this to sync this item with the target array.  {{item.key}} Selected: {{bool}} 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.