2

I have a nested set for my categories as given by Mike Hillyer. I am using JSTree and I have followed the advice as given here in SO: PHP Convert a Nested Set into Data I created a flat format as suggested, however, there is no explanation as how to deliver the Root with a parent of #. My Root has a parent of "null" and so, consequently, the progress wheel just spins and spins and I am getting an error:

Uncaught TypeError: Cannot read property 'toString' of null

The JSTree Docs also recommend setting the Root parent to #, but also do not explain how to do that. I have tried using the AJAX method, however, I could not get that to work effectively as the tree and the page must load together. How do I make JsTree work?

So this is all I have got:

Controller:

 public function jsTree(){   // THIS COMES OUT OF A LARAVEL MODEL. 
   $nodes = $this->category->orderByDepth();
   return view ('categories.jsTree',compact('nodes'));
}

Bottom of my View:

 var jsNodes = <?php echo json_encode($nodes); ?>

         $(function () {
             $('#jstree').jstree({
                 "core": {
                     "themes": {
                         "variant": "large"
                     },
                    data:jsNodes
                 },
                 "plugins": ["checkbox"]
             });
         });
      });

Sample of my JSON from my console log:

0: {id: 1, text: "Root", parent: null}
1: {id: 22, text: "Materials", parent: "Root"}
2: {id: 42, text: "Roofing", parent: "Materials"}
3: {id: 48, text: "Hardware", parent: "Materials"}
4: {id: 43, text: "Services", parent: "Root"}

EDIT: I tried the Ajax route again, as given by Javascripting dot com and I just got the endless spinning wheel again, with the same error message.

Vince
  • 1,405
  • 2
  • 20
  • 33
  • Not sure if you ever found the answer, but the way I achieved it was simply by changing your first result to: 0: {id: 1, text: "Root", parent: '#'} – Boidy Apr 01 '20 at 06:33

0 Answers0