2

I am using ionic framework and angularjs for one app. I am using checkboxes inside ng-repeat. The issue is, if I check the checkbox, it is getting added to the array. If I uncheck it is not getting removed from that array.

The html code is

<div class="action-checkbox" ng-repeat="task in alltasks">
  <h3>{{task.taskName}}</h3>
  <ul>
    <li ng-repeat="subtask in task.subTasks" ng-click="addList(task,subtask)">
      <input id="{{task._id}}_{{subtask._id}}" name="{{task._id}}_{{subtask._id}}" type="checkbox" value="{{subtask.subTaskName}}" ng-checked="subtask.checked" ng-model="slectedTasks" class="hide"/>
      <label for="{{task._id}}_{{subtask._id}}" > 
        {{subtask.subTaskName}}
      </label>
    </li>
  </ul>
</div>

My controller code is

$scope.addList = function(task,subtask){
       subtask.checked= !(subtask.checked);
       var data = {
       "task_id": task._id,
       "subTaskName": subtask.subTaskName,
    };     
        if(subtask.checked){
         selectedMap.push(data);
        }
    }

can anyone help me to resolve this..

2
  • where did you put your delete code ? Commented Dec 21, 2015 at 8:02
  • That's what I am trying to do Anik. If I give selectedMap.remove(data), Entire array is getting removed Commented Dec 21, 2015 at 8:14

2 Answers 2

1

Add code to remove element also.

if(subtask.checked == false){
             for(var i = selectedMap.length - 1; i >= 0; i--) {
                  if(selectedMap[i] === data) {
                        selectedMap.splice(i, 1);
                      }
                  }
            }
Sign up to request clarification or add additional context in comments.

Comments

0

Finally I got the answer for this. If I use "selectedMap[i] === data" I am not able to splice the array because it is not able compare the objects properly. The code is

if(subtask.checked){
      //selectedMap[task._id,subtask.subTaskName] = data;
     selectedMap.push(data);
     duparray = selectedMap;
     console.log(selectedMap);
    }
    if(!subtask.checked){
      console.log("not checked");
      var sellength = selectedMap.length;
      duparray = selectedMap;
      //console.log(duparray);
             for(var i = sellength - 1; i >= 0; i--) {
                  if(selectedMap[i].subTaskName == data.subTaskName) {
                      console.log(duparray);
                        duparray.splice(i, 1);
                        console.log(duparray);
                      }
                  }
            }

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.