mbraak / jqTree

Tree widget for jQuery
https://mbraak.github.io/jqTree/
Apache License 2.0
1.02k stars 177 forks source link

Question, not issue. About declaring children only on first level. #672

Closed WriterStat closed 2 years ago

WriterStat commented 2 years ago

Hi Marco, hope this finds you well and a great piece of software.

This isn't really an issue, but more of a quick question. Thanks! The software is great!

How might one go about having children only declared in the 1st level node code? Note: we have changed children to item in the code to be compatible with other internal json/nodes software and positioned the id at the beginning.

function Node(o, isRoot, nodeClass) { if (isRoot === void 0) { isRoot = false; } if (nodeClass === void 0) { nodeClass = Node; } //sets the order id, title then others and then items. this.id = ""; this.title = ""; this.isEmptyFolder = false; this.setData(o); this.item = []; ...

For the life of me, I haven't figured out a way, thought maybe you could shed some light. We only have 2 levels and so would like to remove the 2nd level empty children arrays in the node.

Thanks for any thoughts. hope you are doing well, Best, -C

mbraak commented 2 years ago

If I understand the question correctly: your node data has a different shape, how can you make that work with jqtree?

Jqtree only works if the nodes have the children property. But you can transform the data before you pass it to jqtree.

If you use the dataUrl option to load data from a server, then you can use the dataFilter option: https://mbraak.github.io/jqTree/#options-datafilter

E.g:

('#tree1').tree({
  dataUrl: '/my/data/',
  dataFilter: function(data) {
    // This is an example, I don't know what your data looks like.
    // NB: I haven't tested this code
    function replaceItemRecursive(node) {
      if (node.item) {
        node.children = node.item;

        for (const child of node.children) {
           replaceItemRecursive(child);
        }
      }
    }

    replaceItemRecursive(data[0]);
  }
});

I hope this answers your question.

WriterStat commented 2 years ago

Hi Marco, thanks for the quick note back, appreciate it. Thanks!

It does work in jqtree just fine as is, but we'd like to store the json in our shape/format that we get/export back out of jqTree using the toJson.

Hope makes sense.

Note: The only way I found to be able to do that is to change the structure of the jqTree Node class/var.

I modified our current jqTree internal code base Node class/var structure to use this.item instead of this.children'. And to usethis.titleinstead ofthis.name'. And I added this.id = ""; above this.title = ""; in the jqTree Node class/var structure. It now follows our structure.

This all works great. Giving us. {"id":"1","title":"node 1","item":[]}

But, in our node format, only the top level/level 1 nodes ever have children. With the current jqTree Node class/var structure how do we stop the Child level nodes from having a children array in the toJson?

Possible?

Thanks again for the thoughts/pointers. Best, -C

mbraak commented 2 years ago

I'm afraid I don't know the answer to that question.

WriterStat commented 2 years ago

Hi Marco, no worries. Appreciate it.

I was just wondering if there was a best place in the jqtree code to check if a node was a child and if so, remove the empty children: [] property where not needed. Our second level tree items will never have children.

I think I've got it though. Thanks again!