[英]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()"/>
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); }
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.