0

When I click the "APPLY" button, I need to get all the checked values of the checkboxes.

I am new to AngularJs. I have a list of checkboxes and according to the checkboxes condition I have to select and deselect the table columns, i.e the checkboxes are the actual columns.

HTML

<li ng-repeat="col in columns">
  <span class="inputH">
    <input type="checkbox" value="col.name" ng-if="col.default === true" checked  ng-model="chk[col.name]" id="{{col.name}}">
    <input type="checkbox" value="col.name" ng-if="col.default === false"  ng-model="chk[col.name]" id="{{col.name}}">
  </span>
  <span class="textH">{{col.name}}</span>
</li>
<li>
  <button class="btn-default" ng-click="onCustomizationApply()">Apply</button>
  <button class="btn-default" ng-click="onCustomization()">Cancel</button>
 </li>

If you notice, onCustomizationApply(), on this I need to get all checkboxes which are checked

2

3 Answers 3

0

try this give common class to all checkbox

<li ng-repeat="col in columns">
<span class="inputH">
<input class="mychkbox"type="checkbox" value="col.name" ng-if="col.default === true" ng-model="chk[col.name]" id="{{col.name}}">
<input class="mychkbox" type="checkbox" value="col.name" ng-if="col.default === false"  ng- model="chk[col.name]" id="{{col.name}}">
</span>
<span class="textH">{{col.name}}</span>
</li>
<li>
<button class="btn-default" ng-click="onCustomizationApply()">Apply</button>
<button class="btn-default" ng-click="onCustomization()">Cancel</button>
</li>

in function onCustomizationApply()

angular.forEach($(".gridCheck"),function(val,index){

if(val.checked){
//here we will get those fields which are checked and we can perform   operation
}
})
Sign up to request clarification or add additional context in comments.

Comments

0

So I modified your code a bit and made a JSFiddle: http://jsfiddle.net/Lvc0u55v/5415/

HTML:

<li ng-repeat="col in columns">
    <span class="inputH">
    <input type="checkbox" ng-value="col.default" value="col.name" ng-model="chk[col.name]" id="{{col.name}}">
  </span>
    <span class="textH">{{col.name}}</span>
  </li>
  <li>
    <button class="btn-default" ng-click="onCustomizationApply()">Apply</button>
    <button class="btn-default" ng-click="onCustomization()">Cancel</button>
  </li>

Angular:

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

function MyCtrl($scope) {
  $scope.columns = [{
    name: 'col1',
    default: true
  }, {
    name: 'col2',
    default: true
  }, {
    name: 'col3',
    default: true
  }];

  $scope.chk = {};

  $scope.onCustomizationApply = function() {
    console.log($scope.chk);
  };
}

2 Comments

why did you removed another checkbox, basically the value of selected checkbox will get saved in a cookie. and on load I am going to read it, depending I will check or uncheck the checkbox
Ah ok, can you add this extra info in the OP?
0

if you change your code (ng-model) from

<input type="checkbox" value="col.name" ng-if="col.default === true" checked  ng-model="chk[col.name]" id="{{col.name}}">

to

<input type="checkbox" value="col.name" ng-if="col.default === true" checked  ng-model="col.colName" id="{{col.name}}">

since ng-model is set to {{col.colName}} when you change this value, value of col in columns also changes. Therefore on click event of button you can iterate through columns to see if value it is checked or not.

 angular.forEach($scope.columns, function (value, key) {
        if (value.colName == true) {
            //checked
        }
        else {
            //not checked
        }
    });

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.