Reputation: 113
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.
Upvotes: 0
Views: 315
Reputation: 497
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.
Upvotes: 1