我使用下面的鏈接,創建一個樹狀結構: LINK如何在本地(在本機上)保存JSON數據?
這是我的代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Tree Context Menu - jQuery EasyUI Demo</title>
<link rel="stylesheet" type="text/css" href="themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="themes/icon.css">
<link rel="stylesheet" type="text/css" href="demo.css">
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery.easyui.min.js"></script>
</head>
<body>
<h2>Tree Context Menu and Drag Drop Tree Nodes</h2>
<p>Right click on a node to display context menu.</p>
<p>Press mouse down and drag a node to another position.</p>
<div style="margin:20px 0;"></div>
<div class="easyui-panel" style="padding:5px">
<ul id="tt" class="easyui-tree" data-options="
url: 'tree_data1.json',
method: 'get',
animate: true,
dnd:true,
onContextMenu: function(e,node){
e.preventDefault();
$(this).tree('select',node.target);
$('#mm').menu('show',{
left: e.pageX,
top: e.pageY
});
}
">
</ul>
</div>
<div style="padding:5px 0;">
<a href="#" class="easyui-linkbutton" onclick="save()" data-options="iconCls:'icon-save'">Save</a>
</div>
<div id="mm" class="easyui-menu" style="width:120px;">
<div onclick="append()" data-options="iconCls:'icon-add'">Append</div>
<div onclick="removeit()" data-options="iconCls:'icon-remove'">Remove</div>
<div class="menu-sep"></div>
<div onclick="expand()">Expand</div>
<div onclick="collapse()">Collapse</div>
</div>
<script type="text/javascript">
function append(){
var t = $('#tt');
var node = t.tree('getSelected');
t.tree('append', {
parent: (node?node.target:null),
data: [{
text: 'new item1'
},{
text: 'new item2'
}]
});
}
function removeit(){
var node = $('#tt').tree('getSelected');
$('#tt').tree('remove', node.target);
}
function collapse(){
var node = $('#tt').tree('getSelected');
$('#tt').tree('collapse',node.target);
}
function expand(){
var node = $('#tt').tree('getSelected');
$('#tt').tree('expand',node.target);
}
function save(){
var a = document.createElement('a');
a.setAttribute('href','data:text/plain;charset=utf-u,'+encodeURIComponent(JSON.stringify({$('#tt').html()}));
a.setAttribute('download', "data.json");
}
</script>
</body>
</html>
當我運行這一點,沒有什麼是得到保存。
我已經爲此結構的代碼添加了一個保存按鈕。
我希望每當用戶點擊這個保存按鈕,然後他可以將這個樹結構作爲JSON數據生成在他/她的本地機器上。我想要這個,因爲這棵樹是可編輯的。我怎樣才能做到這一點?
我用下面的鏈接是相同的:
我想一個碰巧的ID =「TT」無論變化可能會在JSON形式和存儲我的本地機器上檢索。
一個JSON對象,你想用戶節省實際Ĵ SON文件,還是隻是希望它存儲在瀏覽器中供您稍後使用? – 2015-02-11 21:30:27
@BillCriswell:我希望用戶保存一個實際的JSON文件 – 2015-02-11 21:41:47
下面發佈的Dustin對除了IE之外的大多數瀏覽器都適用。如果你需要IE支持,你必須去服務器端。 Google「內容配置」。不知道你用什麼服務器的明智,但它是所有的想法:http://stackoverflow.com/questions/1465573/forcing-to-download-a-file-using-php – 2015-02-11 22:03:36