Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Nested Sets Export Data to JsTree (JSON)
  • Hello,

    I use JsTree json_data plugin to handle my category trees with JsTree.
    http://www.jstree.com/documentation/json_data
    Therefore I need the nested data which I receive with dump_tree() in a special JSON format like here:
    http://bin.fuelphp.com/snippet/view/hx

    I try to rewrite the dump_tree() function to my needs, but when I do that I'll receive an error:
    Fuel\Core\PhpErrorException [ Notice ]: Undefined index: right_id

    Here is my code:
    http://bin.fuelphp.com/snippet/view/hy
    Error is appearing in line 42

    I have no idea what is the problem, as I don't really understand how this is working in the original function. :-(

    Hope someone can help me.
    Thanks Kay
  • Isn't a Format::forge($dumpresult)->to_json(); sufficient?

    You might have to use a dump_tree(true) to have an object tree returned, instead of an array.
  • I am afraid this is Not working. Was the First Thing I tried.

    the Data structure for JsTree Must be like I stated in my Example.

    So I need to Build this JSON somehow manually
  • It's not simple to change the logic of dump_tree, it makes heavy use of pointer referencing to keep track of current and parent nodes, to build the multi-dimensional view of the tree.

    You can try this as a starting point: http://bin.fuelphp.com/snippet/view/hz
  • I'll receive an error in this line:
    if ($treenode->{$left_field} > $tracker[$index][$right_field])
    ErrorException [ Error ]: Cannot use object of type stdClass as array

    I'll call this with:
    $treedata = $root->dump_jstree('children', 'name', array('id'));
  • I never said it was tested and bug free... ;-)

    Try

    if ($treenode->{$left_field} > $tracker[$index]->{$right_field})

  • Hi WanWizard, never expected this :-)

    if I'll change to that I'll get another error:
    Fuel\Core\PhpErrorException [ Notice ]: Undefined property: stdClass::$right_id
    Nearly similar to my own try. I've dumped $tracker[$index] and there is no $right_id.
  • $right_field you mean? It's a variable defined at the top of the method, and should contain the configured column name of the records right pointer (default 'right_id').


  • Sorry yes, $right_field I mean

    But the error is with $right_id:

    Fuel\Core\PhpErrorException [ Notice ]: Undefined property: stdClass::$right_id

    I changed the line as stated by you to:
    if ($treenode->{$left_field} > $tracker[$index]->{$right_field})
  • Ah, the jstree node constructed in the closure doesn't contain the pointers, which is why it fails.

    Try this: http://bin.fuelphp.com/snippet/view/hC
  • Yes this is working with 2 little changes:

    http://bin.fuelphp.com/snippet/view/hD

    $left_field and $right_field need to be configures before cosure, otherweise there is an error that they re not found.

    new \stdClass(); must be new \stdClass(); othewise stdClass is also not found.

    I receive now the correct data, but still have one problem:

    http://bin.fuelphp.com/snippet/view/hE

    JsTree cannot handle these keys. :-(
    Is there a way to have this data without the keys? Or can I parse them out later?

    Thanks so much for our help WanWizard...
  • Great product, this jstree. Why would it care what else is in the object?

    It's not going to be easy to rewrite this, is uses assignment by reference to keep track of elements in the tree, and it needs the pointers for that.

    You can remove them later using unset(), but it will be complex because you have to recurse the tree you have just created...
  • Yes, JsTree is really cool.

    I just debug what causes the problem:
    "left_id" and "right_id" can stay in JSON, it's ignored by JsTree, so no worry about that.

    We only have two problems:
    1) If children is filles {} are used instead of []
    2) The keys before the
    {"data":"xxx",...} don't need to be there.

    Then this should work perfectly. :-)
  • {} is an object, [] is an array.

    I used objects (stdClass) because your example used object notation. If that has to change, you need to change the use of stdClass to an array, and also every use of tracker then needs to use array notation instead of -> ...

    I leave this to you as an excercise ;)
  • You mean like this?

    http://bin.fuelphp.com/snippet/view/hH

    It is working, but the filled children are still with {} instead of [].
  • on line 34 you have to remove "(object)".
    on line 46, 'children' should be $children.
    on line 27, 'left_field' should be $left_field.
    on line 28 and 71, 'right_field' should be $right_field.

    Cosmetic details, which should not affect the outcome.

    Are you not confused with multi-dimensional arrays?

    Children will be one, the correct JSON notation for a multi-dimensional array is [] to define the array, and {} for every array element.
  • I know that I have to build a multidimensional array for children, but I have no idea how to do that.

    The problem is that I don't know where exactly the whole data is put together. So I Don't know where and how to set the [] for the children. :-(
  • From what I can see, you did it correctly. You can do a Debug::dump() on the result of the method call to check, but I'm pretty sure it's going to be all arrays.

    That is not my point though, my point was that

    array(array('color' => 'red', 'value' => '#f00'), array('color' => 'green', 'value' => '#0f0'))

    translates too

    [ { color: "red", value: "#f00" }, { color: "green", value: "#0f0" } ]

    so if you need a different layout, you have to construct the children array differently.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion