Tiffany
Tiffany

Reputation: 11

How to reload a jstree from local storage?

I am using jstree to access an xml. I need to save the nodes created,etc in the tree to local storage. So, what I'm doing right now is, when a node is created, I convert an instance of the tree to json form using 'get_json' and then using 'JSON.stringify', I convert it to a json string. I save this json string to local storage as 'SavedTree'.

  .bind('create_node.jstree', function(e, data){
   var jsonForm =$('#treeContainer').jstree('get_json', -1);
   localStorage.setItem('SavedTree', JSON.stringify(jsonForm)); 
   })
};

When I retrieve 'SavedTree' from localStorage, I parse it to convert the string back to json:

var NewTree = localStorage.getItem('SavedTree');
var NewTree = JSON.parse(NewTree);

'NewTree' is in Json form. How do I convert it back to an instance of the tree so that I can use it as 'data' in the below code ?

  ("#treeContainer").jstree({
    "json_data" : {
      "data": data
      "progressive_render":"true"
        },
    "plugins": [ "json_data", "contextmenu", "crrm", "dnd", "unique"],
  })

I am new to javascript so this may be a silly question but any advice on how to proceed will be appreciated. Thanks.

Upvotes: 1

Views: 1417

Answers (1)

Halcyon
Halcyon

Reputation: 57729

This line is weird:

var NewTree = localStorage.getItem('SavedTree', JSON.parse(SavedTree));

getItem doesn't have a 2nd argument.

I think you want:

var NewTree = JSON.parse(localStorage.getItem('SavedTree'));

Upvotes: 1

Related Questions