mbraak / jqTree

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

empty children array renders as file #45

Closed SirCumz closed 9 years ago

SirCumz commented 12 years ago

if i parse a empty children array the tree renders as if it is a file node!.

i looked into the code an i found out that the code checks: if child count = 0 = CreateFileLi

but it should be CreateFolderLi, this way the folder icons are displayed. (parsing empty children array means its still a folder)

/\ Some additional requests **/

Events:

  1. onTreeComplete OR onTreeShow (so im able to fetch files according to the current selected node)
  2. onTreeMouseOver

Methods:

  1. getParents ( an array containing al the parent nodes till root )
mbraak commented 12 years ago

Version 0.11 of jqTree has the tree.init event. This event is fired when the tree data is loaded. It is not documented yet.

JqTree does not support mouse-over, but you can write it yourself like this:

$tree.on('hover', '.title', function(e) {
  var $li = $(e.target).closest('li');
  var node = $li.data('node');
  console.log(node);
});

I'm not sure if I want to add the getParents function to jqTree because I don't know how many people will use it. You could however implement it in your own code, like this:

var node = $tree.tree('getNodeByName', 'Hadrosaurids');
var parents = [];
var parent = node.parent;
while (parent) {
  parents.push(parent);
  parent = parent.parent;
}

It should indeed be possible to display an empty folder. More people have mentioned this, and I'm still thinking about the best api for this. Perhaps an empty children property is a good option.

SirCumz commented 12 years ago

maybe typeof() is also good.

i downloaded the latest version 0.11 and the tree.init event works :)

but when i try to get the selected node it returns null!

but i can see that a node (by cookie or savestate ) has been selected on startup, but when i try to get the selected node on the tree.init event it returns null.

here is the code:

$tree.bind( 'tree.init', function(e) { var node = $tree.tree('getSelectedNode'); alert(node) } );

For my application to work i realy need to get the selected node on tree.init, so i can fill my second tree with the associated files. any ideas?

mbraak commented 12 years ago

Strange. I tried it with example2 (http://mbraak.github.com/jqTree/examples/example2.html) with the following code:

$tree = $('#tree1');
$tree.tree({
  selectable: true,
  saveState: true
});

$tree.bind('tree.init', function() {
  var node = $tree.tree('getSelectedNode');
  console.log(node);
});

A small detail: for saveState to work, nodes must have an id property. Example:

{
  label: 'Sauropodomorphs',
  id: 16
}
SirCumz commented 12 years ago

well i use ID's and labels.

this is my tree config:

{
     data: data,

     autoOpen: 0,

      dragAndDrop: false,

      selectable: true,

      saveState: true
}

But on the tree.init event only null returns.

Like i said "i use ID's for nodes"

I looked at example2 but the log is empty. i use google chrome maybe thats the issue?

\ EDIT **

IE also returns null. just checked it.

mbraak commented 12 years ago

I'm afraid you found an error in jqTree. If you call getSelectedNode in the tree.init event it returns null.

However, you can call getSelectedNode after initializing the tree:

$tree.tree({
  data: data,
  selectable: true,
  saveState: true
});

var node = $tree.tree('getSelectedNode');
console.log(node);
SirCumz commented 12 years ago

Yes that works!

but i found also a workaround for the tree.init event


  $tree.bind(
      'tree.init',
      function(e) 
      {
        var $li = $tree.find('li.selected');
        var node = $li.data('node');
        alert(node.name)
      }
  );