0

/**
 * @Summary: checkAllConnectedUser function, to create album
 * @param: index, productObj
 * @return: callback(response)
 * @Description:
 */
$scope.shardBuyerKeyIdArray = [];
$scope.countBuyer = 0;
$scope.checkAllSharedBuyer = function(isChecked) {
  if (isChecked) {
    if ($scope.selectAll) {
      $scope.selectAll = false;
    } else {
      $scope.selectAll = true;
    }
    angular.forEach($scope.selectedSharedBuyerObjectList, function(selectedBuyer) {
      selectedBuyer.select = $scope.selectAll;
      //IF ID WILL BE EXIST IN THE ARRAY NOT PSUH THE KEYID
      if ($scope.shardBuyerKeyIdArray.indexOf(selectedBuyer.userTypeDto.keyId) == -1) {
        $scope.shardBuyerKeyIdArray.push(selectedBuyer.userTypeDto.keyId);
        $scope.countBuyer++;
      }
    });
  } else {
    $scope.selectAll = false;
    //USED FOR UNCHECK ALL THE DATA ONE- BY-ONE
    angular.forEach($scope.selectedSharedBuyerObjectList, function(selectedBuyer) {
      selectedBuyer.select = $scope.selectAll;
      var index = $scope.shardBuyerKeyIdArray.indexOf(selectedBuyer.userTypeDto.keyId);
      $scope.shardBuyerKeyIdArray.splice(index, 1);
      $scope.countBuyer--;
    });
  }
}
<div class="checkbox w3-margin" ng-if="selectedSharedBuyerObjectList.length > 0">
  <span class="w3-right" ng-if="countBuyer">
    <h5>You are selecting {{countBuyer}} buyers!</h5>
  </span>
  <label>
    <input type="checkbox" ng-model="selectAll"  ng-click="checkAllSharedBuyer(selectAll)"/>Check All
  </label>
</div>

<div id="sharedRow" class="checkbox" ng-repeat="selectedBuyer in cmnBuyer = (selectedSharedBuyerObjectList | filter : userSearchInProduct
  | filter : filterUser)"> 
  <label>
    <input type="checkbox" ng-model="selectedBuyer.select" 
      ng-change="selectedSharedBuyer($index, selectedBuyer.select, selectedBuyer.userTypeDto.keyId)"/>
      {{selectedBuyer.personName}} 
  </label>
</div>

I have two list in which i have to count the select all checkbox length as well as single checkbox count my problem if the user un-check the ALL checkbox Checkbox count will be return -- what's the problem in my code?

3 Answers 3

1

$(function(){
var count = 0;
$('#sharedRow ').find('input[type=checkbox]').on('change',function(){
   $('#msg').text('You are selecting '+$('#sharedRow ').find('input[type=checkbox]:checked').length+' buyers!')
})
$('#chkAll').on('change', function () {
    if ($(this).is(':checked')) {
        $('#sharedRow ').find('input[type=checkbox]').prop('checked', true);
        $('#msg').text('You are selecting '+$('#sharedRow ').find('input[type=checkbox]:checked').length+' buyers!')
    }
    else {
        $('#sharedRow ').find('input[type=checkbox]').prop('checked', false);
        $('#msg').text('You are selecting '+$('#sharedRow ').find('input[type=checkbox]:checked').length+' buyers!')
    }
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="checkbox w3-margin">
    <span class="w3-right">
        <h5 id="msg" >You are selecting 0 buyers!</h5>
    </span>
    <label>
        <input id="chkAll" type="checkbox" />Check All
    </label>
</div>

<div id="sharedRow" class="checkbox">
    <label>
        <input type="checkbox" value="1 Buyers" />1 Buyers
    </label>
    <label>
        <input type="checkbox" value="2 Buyers" />2 Buyers
    </label>
    <label>
        <input type="checkbox" value="3 Buyers" />3 Buyers
    </label>
    <label>
        <input type="checkbox" value="4 Buyers" />4 Buyers
    </label>
    <label>
        <input type="checkbox" value="5 Buyers" />5 Buyers
    </label>
    <label>
        <input type="checkbox" value="6 Buyers" />6 Buyers
    </label>
    <label>
        <input type="checkbox" value="7 Buyers" />7 Buyers
    </label>
    <label>
        <input type="checkbox" value="8 Buyers" />8 Buyers
    </label>
</div>

try this one. is it ok? if not then tell me what's wrong.

Sign up to request clarification or add additional context in comments.

Comments

0

if you have a group of checkbox then you can find all selected checkbox.

$('div').find('input[type=checkbox]:checked').length;

3 Comments

hi, I'm resolving this issue in jquery. if you don't mine. can I do?
i have tried this code and its work's for me sir thank you for your great response but sir how to implmented this functionality with AngularJS?
If you Indian read in Hinglish Bhai ab aap socho or logic build kro tvi hoga wrna kvi ni hoga
0

If you only need the number

var count = $scope.selectedSharedBuyerObjectList.reduce(function(sum, item) {
  return (item.select) ? sum + 1 : sum;
}, 0);

If you need the filtered array

var selected = $scope.selectedSharedBuyerObjectList.filter(function(item) {
  return item.select;
});

var count = selected.length;

Or do it using plain old loop

var count = 0;

for (i = 0; i < $scope.selectedSharedBuyerObjectList.length; i++) {
  if ($scope.selectedSharedBuyerObjectList.select) count++;
}

Comments

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.