Saving tree in alternative JSON format

837 views
Skip to first unread message

Slim Slam

unread,
Mar 19, 2014, 11:05:46 AM3/19/14
to jst...@googlegroups.com
I want save a JStree tree in alternative JSON format.  This code I came up with does
save the tree, but it's in the regular JStree JSON format, not the alternative JSON format (using JStree beta 10). 
Anyone see what's wrong here?

function saveIt(PostUrl) {
           var objtree = $("#jstree_sch").jstree(true).get_json('#', { 'flat' : true });
           var fulltree = JSON.stringify(objtree);

                $.ajax({
                  url: PostUrl,
                  data: fulltree,
                  type: 'POST',
                  processData: false,
                  dataType: "text",
                  contentType: "text/plain; charset=utf-8",

                  success: function(results) {
             alert("You saved this tree.");
             window.location.href = "/main";
          }
});

=========

J

Ivan Bozhanov

unread,
Mar 19, 2014, 11:23:58 AM3/19/14
to jst...@googlegroups.com
Are you sure you have beta10? As I am testing locally and I have no problem using:
$('#tree').jstree(true).get_json('#', { flat : true})

I get an array with nodes that have id & parent properties.

Best regards,
Ivan

Slim Slam

unread,
Mar 19, 2014, 11:52:43 AM3/19/14
to jst...@googlegroups.com
Hi Ivan,

I've attached a before.txt file which is the Alternative JSON structure that I manually create in my code.

The "after.txt" file is what gets created as a result of processing my structure using my code above:

 var objtree = $("#jstree_sch").jstree(true).get_json('#', { 'flat' : true });
 var fulltree = JSON.stringify(objtree); 
alert(full tree);  // Add an alert here to see what gets created
......

Maybe my manually created structure is faulty?

J
before.txt
after.txt

Slim Slam

unread,
Mar 19, 2014, 11:56:43 AM3/19/14
to jst...@googlegroups.com
I forgot to mention that I instantiate the tree using this code:

$(function() {

$('#jstree_sch').jstree({
  "plugins" : [ "checkbox" ],
  "checkbox" : {
      "visible" : true,
      "whole_node" : false,
      "keep_selected_style" : false
    },
  "core" : {
    "animation" : 0,
    "check_callback" : true,
    "themes" : { "stripes" : true },
     "data" :  { myJson }
   }
});
});

Ivan Bozhanov

unread,
Mar 19, 2014, 11:57:39 AM3/19/14
to jst...@googlegroups.com
It looks fine to me. You have both the id and parent properties - so it is the alternative format that you are getting back, why did you think you are getting the standard JSON?

Slim Slam

unread,
Mar 19, 2014, 12:14:05 PM3/19/14
to jst...@googlegroups.com
Oops. I guess I didn't notice the parent element at the end.  :-}

Thanks Ivan!
Reply all
Reply to author
Forward
0 new messages