0

I'm trying to create a button that would delete all complete items in a to do list --- after trying several different ways to accomplish this, I figure its probably easiest to just send checked items to one array, and unchecked items to another array, so I can make a button that just clears the checked items array... but nothing I am doing is working... thoughts?

html:

<input type="text" placeholder="To do..." ng-model="vm.myTask">
<button type="button" ng-click="vm.submitTask()">Submit</button>
<br>

<div ng-repeat="task in vm.myTasks" ng-class="'checkedbackground': task.checked">
    <ul>
        <li>
            <input type="checkbox" value="{{task.name}}" ng-model="task.checked">{{task.name}}
            <button type="button" ng-click="vm.myTasks.splice($index, 1)">Delete</button>
        </li>
    </ul>
</div>

<button type="button" ng-click="vm.clearAll()"> Clear Complete</button>

homeController.js

var myTasks = [];
var completeTasks [];

class HomeController {



//submit task from input value
submitTask(){
//push new task into array
  myTasks.push({name: this.myTask, checked: false});

}
constructor(myTask, checkedTask){
this.myTasks = myTasks;

}

clearAll(){



}

}



angular.module("myapp").controller("HomeController", HomeController);

I erased what I tried but some of what I tried involved this:

constructor(myTask, checkedTask){
    this.myTasks = myTasks;
    completeTasks.push({name: this.checkedTask, checked: true});
    }

that broke the whole thing though.

1
  • Can you read your question one more time? Because am unable to understand it. :( Commented Jun 29, 2017 at 9:13

1 Answer 1

1

Considering your original idea and the fact that you tagged this question with javascript, what you can do is to create a deleter function, which goes through your original array (no need to create a separate one) , and then checks if the task has been completed (each list item can be a object with a property named 'isDone' ) , and if done, you can remove that item from array using splice()

Another way of doing this is

function clr() {
    var newlist = TaskList.filter(function (item,i,TaskArr) {
        if(item.check)
            return false;
        else return true;
    });
    TaskList = newlist;

}

This uses the filter() function to check if the item is checked (completed) and returns a new array which contains only uncompleted items.

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

5 Comments

i'm confused, you are talking about an if statement, then your if statement disappeared and the making a new array part...
My code was edited to make it smaller by Pankaj Parkar, see the answer history and you will find my code, maybe you'll understand that better.
@AbhishekRanjan if you want original code, you can revert to older revision. just click on this link, I just had a thought about optimizing code
Thank you @PankajParkar , being new to StackOverflow, I didn't know that. ;
@karmadreamwalker you can view the original code now.

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.