简体   繁体   English

如何将选中的项目与未选中的项目添加到另一个数组? angularjs

[英]how to add checked items to a different array from unchecked items? angularjs

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: 的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 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. 但这破坏了整个事情。

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() 考虑到您的原始想法和使用javascript标记此问题的事实,您可以创建一个deleteer函数,该函数遍历原始数组(无需创建单独的数组),然后检查任务是否已完成。完成(每个列表项可以是具有名为'isDone'的属性的对象),如果完成,则可以使用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. 这使用filter()函数检查项目是否已检查(完成),并返回仅包含未完成项目的新数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM