J
J
jack3d2015-05-27 10:44:04
Angular
jack3d, 2015-05-27 10:44:04

How to get an array of checkboxed elements?

We mark all posts with checkboxes:

<input ng-click="selectAllPosts()" class="select-all-posts" type="checkbox"><label class="select-all-posts">{{ selectedText }}</label>

Retrieve all posts:
<div class="flagged-post" data-ng-repeat="post in posts" >
                    <input ng-checked="post.checked" ng-model="post.checked" type="checkbox" class="select-post">
<div>Что-то</div>
</div>

$scope.selectedText = 'Select All Posts';
          $scope.isAll = false;
          $scope.selectAllPosts = function() {

            if($scope.isAll === false) {
              angular.forEach($scope.posts, function(post){
                post.checked = true;

              });
              $scope.isAll = true;
              $scope.selectedText = 'Deselect All Posts';
            } else {
              angular.forEach($scope.posts, function(post){
                post.checked = false;

              });
              $scope.isAll = false;
              $scope.selectedText = 'Select All Posts';
            }
          };


          $scope.getFullName = function( post ) {
            post.checked = false;
          };

          $scope.selectedPostsFilter = function () {
            return $filter('filter')($scope.posts, {checked: true});
          };

How to get an array of selected objects in order to go further forEach'om?
$scope.removeSelectedPosts = function () {
            angular.forEach(selectedObj, function(post) {
              //что-то делаем
                  });
          };

You need to output something like $scope.posts WITH {checked: true}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gavrilov, 2015-05-27
@FireVolkhov

What is it?

$scope.selectedPostsFilter = function () {
    return $filter('filter')($scope.posts, {checked: true});
};

A
Alexander Tartmin, 2015-05-27
@baskerville42

This module will help you to work with checkboxes vitalets.github.io/checklist-model . With it, it will be easier then to perform actions on all selected elements.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question