这可能听起来像一个业余问题,但我坚持这一点。我正在使用jstree,目前我正在将jstree的数据加载为JSON。我想将JSON转换为变量。我怎么做?如何使用变量在jstree中获取数据
这是我的代码:
var treedata = ['{ "id" : "ajson1", "parent" : "#", "text" : "Customer" }',
'{ "id" : "ajson2", "parent" : "ajson1", "text" : "Order number" }',
'{ "id" : "ajson3", "parent" : "ajson1", "text" : "Date" }',
'{ "id" : "ajson4", "parent" : "#", "text" : "Company Name" }',
'{ "id" : "ajson5", "parent" : "#", "text" : "Contact Name" }',
'{ "id" : "ajson6", "parent" : "#", "text" : "Name1" }',
'{ "id" : "ajson7", "parent" : "#", "text" : "Product number1" }'];
$(function() {
$('#jstree').jstree({
"checkbox" : {
"keep_selected_style" : false
},
"core" : {
// so that create works
//ACITREE
"check_callback" : true,
'data' : treedata
},
"types" : {
"default" : {
"icon" : "none"
}},
"plugins" : [ "checkbox","dnd","sort","types",,"crrm"]
});
$('#jstree').on("changed.jstree", function (e, data) {
console.log(data.selected);
//document.getElementById("test").innerHTML+=data.selected.text;
});
// 8 interact with the tree - either way is OK
$('button').on('click', function() {
$('#jstree').jstree(true).select_node('child_node_1');
$('#jstree').jstree('select_node', 'child_node_1');
$.jstree.reference('#jstree').select_node('child_node_1');
});
});
请帮助。提前致谢。
你的意思是你想要用包含JSON数组的变量替换该JSON数组? – 2014-09-02 06:19:08
是的!以便我可以动态更改JSON中的值。 – 2014-09-02 06:21:09
好的,这太简单了,作为答案发布。剪掉你的JSON块,用“myvar”取代它,然后在你的JSTree块定义一个变量之前......'var myvar = [YOURJSONHERE];' – 2014-09-02 06:22:07