2017-09-16 45 views
0

我有一个资源列表,如下面的html内容加载从resources_data.json数据我从同一个服务器从外部加载到我的应用程序。删除一个外部.json对象并再次保存

<div class="callout"> 
<div class="large-8 medium-8 small-8 columns left">Demo</div> 
<div class="large-2 medium-2 small-2 columns right"> 
    <a href="#" class="deleteResource" data-id="1" data-name="Demo"> 
<i class="fa fa-close red"></i> DELETE</a> 
</div> 
</div> 

我希望能够通过“ID”来删除该列表中的项目不必匹配“资源名称”所以每次删除我将主要删除整个对象我会不喜欢将文件保存回服务器以节省响应时间,但我不确定是否可以删除外部文件的字符串而不重新保存。

MY JSON:

"calendarResources": [ 
    { 
     "id": 1, 
     "resourceName": "Demo" 
    }, 
    { 
     "id": "4", 
     "resourceName": "New Test Resource" 
    }, 
    { 
     "id": "2", 
     "resourceName": "test" 
    }, 
    { 
     "id": "5", 
     "resourceName": "another test" 
    }, 
    { 
     "id": "6", 
     "resourceName": "new test data" 
    }, 
    { 
     "id": "7", 
     "resourceName": "better one" 
    }, 
    { 
     "id": "8", 
     "resourceName": "the fucking one!" 
    }, 
    { 
     "id": "12", 
     "resourceName": "Dune one" 
    }, 
    { 
     "id": "13", 
     "resourceName": "res test" 
    } 
    ] 

我试图使用删除功能删除字符串,并把数据上传回再次JSON,但事情错在这里,没有什么工作,我也得到了相同的数据了。

MY JS:

function deletingResource() { 
     $("body").on("click", ".deleteResource", function (e) { 
      e.preventDefault(); 

      var resourceId = $(this).data("id"); 
      var resourceName = $(this).data("name"); 

      $.getJSON(appDirLocation + "_data/resources_data.json", function (jsonData) { 

       console.log(jsonData); 

       var resourceDeletion = ({ 
        "id": resourceId, 
        "resourceName": resourceName 
       }); 

       delete jsonData.calendarResources[resourceDeletion]; // delete data string 


       var newJsonOutput = JSON.stringify(jsonData); //stringify new data to save 

       var jsonFile = new BCAPI.Models.FileSystem.File(appDirLocation + "_data/resources_data.json"); 

       jsonFile.upload(newJsonOutput).done(function() { 
        $("#resourcesList").html(""); 
        console.log("RESOURCE DATA DELETED"); 
        renderResources(); 
       }).error(function (jqXHR) { 
        console.log("RESOURCES JSON FAILED DELETE: " + jqXHR.responseText); 
       }); // END OF JSON CREATING 

       console.log(newJsonOutput); 

      }).done(function() { 
       $(this).closest(".resourcesData").hide(); 
      }).fail(function (jqXHR) { 
       console.log("Request failed." + "Error code: " + jqXHR.status + "Error text: " + jqXHR.statusText + "Response text: " + jqXHR.responseText); 
      }); 

     }); 

    } // DELETING RESOURCES 
    deletingResource(); 

感谢提前的帮助......

回答

0

基于一个回答问题发布here还引用Array.prototype.filter()函数我能用恩典解决这个问题!

我推荐给任何人看使用JavaScript来检查的JavaScript库的Array属性使用JSON文件工作在固定MDN WEB DOCS

代码:

jsonData.calendarResources = jsonData.calendarResources.filter(item => item.id != resourceId); // WORKING ###### 

var newJsonOutput = JSON.stringify(jsonData); 
var jsonFile = new BCAPI.Models.FileSystem.File(appDirLocation + "_data/resources_data.json"); 
相关问题