简体   繁体   English

如何在toDo App Javascript中删除已完成的任务

[英]How can I remove completed tasks in toDo App Javascript

I don't have idea how can i deleted all completed tasks from my json file and my app view: 我不知道如何从json文件和应用视图中删除所有已完成的任务:

    json:
{
  "list": [
    {
      "name": "Cleaning",
      "desc": "by me",
      "date": "11-3-2018 13:38",
      "active": "false",
      "id": 1
    },
    {
      "name": "Wash the dishes",
      "desc": "by me",
      "date": "11-3-2018 23:11",
      "active": "true",
      "id": 2
    },
    {
      "name": "Training",
      "desc": "go with bro",
      "date": "15-1-2016 23:41",
      "active": "false",
      "id": 3
    }
  ]
}

I would like to deleted all tasks - active: false by one click button. 我想删除所有任务-活动:一键单击否。 I have to use XMLHttpRequest. 我必须使用XMLHttpRequest。

You can loop through your JSON variable then have a condition on checking the active key. 您可以遍历JSON变量,然后在检查活动密钥方面有条件。 If its true then remove it. 如果为true,则将其删除。 You can use splice to remove elements from an array. 您可以使用splice从数组中删除元素。 Then send it to your server side or etc. 然后将其发送到您的服务器端或其他。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

You will need to go server side to change the content of files, which I think you want to do. 您将需要在服务器端更改文件的内容,我认为您想这样做。 Using JS and PHP you would get something like: 使用JS和PHP,您将获得类似以下内容的信息:

For the JS: 对于JS:

$("#yourButtonId").on('click', function(){

  //Get the JSON file 
  var request = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
      var jsonObj = JSON.parse(this.responseText);

      //Go over all tasks and remove if active is set to false
      for (var i = 0; i < jsonObj.length; i++) {
        if(jsonObj.list[i].active == "false"){
          delete jsonObj.list[i];
        };
      }

      //Send the updated json file as string to PHP
      var xmlhttp = new XMLHttpRequest();    
      xmlhttp.open("GET", "yourphpfile.php");
      xmlhttp.send(JSON.stringify(jsonObj));
    }
  }
  request.open("GET", "urlToJson.json", false);
  request.setRequestHeader("Content-type", "application/json")
  request.send(null);

});

For the PHP: 对于PHP:

//Get your updated json string
$theJson = $GET['jsonObj'];

//Write the updated json string to the file
$myfile = fopen("urlToJson.json", "w") or die("Unable to open file!");
fwrite($myfile, json_encode($theJson));
fclose($myfile);

Try this : 尝试这个 :

 var jsonObj = { "list": [{ "name": "Cleaning", "desc": "by me", "date": "11-3-2018 13:38", "active": "false", "id": 1 }, { "name": "Wash the dishes", "desc": "by me", "date": "11-3-2018 23:11", "active": "true", "id": 2 }, { "name": "Training", "desc": "go with bro", "date": "15-1-2016 23:41", "active": "false", "id": 3 } ] }; function removeCompletedTask() { var resJson = jsonObj.list.filter(item => item.active == "true"); console.log(resJson); } 
 <input type="button" value="Remove" onClick="removeCompletedTask()"/> 

  1. I use only json server (without php file) 我仅使用json服务器(没有php文件)
  2. If I want to delete single task I do this code 如果要删除单个任务,请执行以下代码

    function deleteTask(task) { var xhr = new XMLHttpRequest(); xhr.open("DELETE", url + "/" + task.dataset.id, true); xhr.onload = function() { var json = JSON.parse(xhr.responseText) if (xhr.readyState == 4 && xhr.status == 200) { task.parentNode.parentNode.removeChild(task.parentNode); } } xhr.send(null); }

    1. I did checkbox to change active my tasks and i would like to delete all of them i try something like this: 我做了复选框以更改活动的任务,并且想删除所有任务,我尝试执行以下操作:

    function clearTasksCompleted(task) { var taskCompleted = task.parentElement.parentElement.querySelectorAll('li'); var completed = [];

for(let i =0; i<taskCompleted.length; i++){

  if(taskCompleted[i].querySelector('.checkbox').checked)
  {
    completed.push(taskCompleted[i]);
  }

}

for (let i=0; i<completed.length; i++) {

  var xhr = new XMLHttpRequest();
  xhr.open("DELETE", url + "/" + completed[i].dataset.id, true);

xhr.onload = function() {

    if (xhr.readyState == 4 && xhr.status == 200) {
      var json = JSON.parse(xhr.responseText);
     completed[i].parentNode.removeChild(completed[i])
    }
  }
  xhr.send(null);}}

` `

and this deleted tasks from json but dont render my page automatically, only after preload i don't see completed tasks 并且此操作从json中删除了任务,但仅在预加载后才自动呈现我的页面,我看不到完成的任务

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

相关问题 如何将按钮添加到待办事项列表以将任务标记为已完成 Javascript? - How can I add a button to a todo-list to mark tasks as done Javascript? 如何使用 Vue.js 保存待办事项 - How can I save todo tasks using Vue.js 显示JS ToDo列表中有多少完成的任务与多少个任务 - Showing how many completed tasks VS how many tasks there are in a JS ToDo list 如何使用LocalStorage从待办事项列表中删除项目 - How can i remove items from Todo List with LocalStorage 如何在函数调用中清除所有已完成的任务? - How can I get all completed tasks to clear on function invocation? 如何在 React 中使用过滤器方法制作 Todo 应用程序时删除 Todo 任务? - How can I Delete a Todo Task while Making a Todo app using filter method in React? 如何在待办事项应用程序中使用本地存储(javascript) - how use localstorage in todo app (javascript) 我可以存储在 LOCAL STORAGE 但刷新页面后列表仍然消失了? 带有 Javascript 的基本待办事项列表应用程序 - I can store in LOCAL STORAGE but the lists are still gone after refreshing the page? basic Todo list app with Javascript 如何在待办事项列表中的每个 LI 上显示一个删除按钮? - How can I make appear a remove button on every LI in a Todo List? 我如何获得 Todo 的 ID - How can i get the Id of the Todo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM