1

I have to use single checkbox for my code

My doc.ejs

<tr ng-repeat="folder_l in folderlist | orderBy:created_date" >
   <td>
      <div class="permit">
         <input class="chkbtn move_check" type="checkbox" id="movecheck{{folder_l._id}}" name="movecheck" value="{{folder_l._id}}" ng-model="checkfolder" ng-click="updateSelection($index, folderlist)" />
         <label class="chklabel" for="movecheck{{folder_l._id}}"></label>{{checkfolder}}
      </div>
   </td>
</tr>

app.js:

$scope.updateSelection = function(position, entities) {
  angular.forEach(entities, function(subscription, index) {
         console.log('position')

     console.log(parseInt(position))
         console.log('index')

    console.log(parseInt(index))

          $scope.checkfolder;

        if (parseInt(position) == parseInt(index)) {
      console.log("Equal")
     $scope.checkfolder = true;
    }
    else{
      $scope.checkfolder = false;
    }
  });
}

I searched so many links and i have tried, but nothing is working for my code, even i have tried jquery click,but click function has not getting called.

UPDATE My JSON

{
    "_id" : *****,
    "__v" : 0,
    "folder_name" : "Music",
    "status" : "Y",
    "created_date" : ISODate("2017-12-29T10:50:31.171Z"),
    "updated_date" : ISODate("2017-12-29T10:50:31.171Z")
}
5
  • use ng-change="updateSelection($index, folderlist)" instead of ng-click="updateSelection($index, folderlist)" Commented Feb 6, 2018 at 7:03
  • @AngelPolitis i have to select only one checkbox , all checkbox should be unchecked except the clicked one Commented Feb 6, 2018 at 7:05
  • could you please share the Json??? Commented Feb 6, 2018 at 7:07
  • @VenkaTeshuser5397700 updated my question with JSON Commented Feb 6, 2018 at 7:11
  • @ManvendraPriyadarshi Thanks, I tried ng-change too, but its act like multi check only Commented Feb 6, 2018 at 7:13

2 Answers 2

1

HTML :

<tr ng-repeat="folder_l in folderlist | orderBy:created_date" >
   <td>
      <div class="permit">
         <input class="chkbtn move_check" type="checkbox" id="movecheck{{folder_l._id}}" name="movecheck" value="{{folder_l._id}}" ng-model="folder_l.checkfolder" ng-click="updateSelection($index, folderlist)" ng-checked="{{folder_l.checkfolder}}" />
         <label class="chklabel" for="movecheck{{folder_l._id}}"></label>{{folder_l.checkfolder}}
      </div>
   </td>
</tr>

JS

$scope.updateSelection = function(position, entities) {
    angular.forEach(entities, function(subscription, index) {
        if (parseInt(position) == parseInt(index)) {
          subscription.checkfolder = true;
        } else {
            subscription.checkfolder = false;
        }
    });
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your response, Its working but I could not able to unchecked the selected one
change this line "subscription.checkfolder = true;" to " subscription.checkfolder != subscription.checkfolder;"
Thank you so much..its working perfectly, after changing this line.. could you please explain me , why its working after changing
by default we only changed to true. now if the value is true changed to false and if the value is false changed to true.
Thank u so much :)
0

for this you need to create virtual model. if you see i have created isSelected

<tr ng-repeat="folder_l in folderlist | orderBy:created_date" >
   <td>
     <div class="permit">
     <input class="chkbtn move_check" type="checkbox" 
       id="movecheck{{folder_l._id}}" name="movecheck" value="
         {{folder_l._id}}" ng-model="folder_l.isSelected"/>
     <label class="chklabel" for="movecheck{{folder_l._id}}"></label>
     {{folder_l.isSelected}}
      </div>
   </td>
</tr>

by doing only this you will able to select only one checkbox at a time. and for submitting you can traverse through the array.

angular.forEach(entities, function(subscription, index) {
       if(subscription.isSelected)
         console.log('position')
})

1 Comment

Thank u for your reply, i will try this.

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.