hColumns icon indicating copy to clipboard operation
hColumns copied to clipboard

Default value

Open elasti-co opened this issue 12 years ago • 7 comments

Thank you for this plugin. Very useful! Is it possible to make default selection of some particular element of the tree (on page load)?

elasti-co avatar Jul 19 '13 01:07 elasti-co

From what I've seen while improving this plugin, there is no way to do so. Maybe you could do it yourself within the nodeSource function : when node_id (the first parameter to the nodeSource function) is null, it means this is the initial load. So you could have something like this :

nodeSource: function(node_id, callback) {
  // Get your data
  // ...
  // Call the callback
  $cbResult = callback(null, data);

  // Select your element (the dirty way)
  $myElement.trigger('click');

  return $cbResult;
}

(Didn't test this)

pjparra avatar Jul 20 '13 08:07 pjparra

Thank you much for reply. But how can I select an element if there is no classes or ids or any properties?

elasti-co avatar Jul 20 '13 09:07 elasti-co

CSS-like selectors are quite powerful, for example, if you want to select the first element (assuming your hColumns has an the id "columns"):

$('#columns').find('ul li:first').trigger('click');

hColumns sets some data-attributes on elements, so if you want to select an element based on the data it represents, you can do it by iterating on the collection:

$('#columns').find('ul li').each(function() {
  if ($(this).data('node-data').yourProperty == 'what you are looking for') {
    $(this).trigger('click');
  }
});

pjparra avatar Jul 20 '13 10:07 pjparra

Ooooooh, at last I understood how to do it!!! Your second way works fine! Thank you very much, I spent many hours trying to do it myself. Thanx!

elasti-co avatar Jul 20 '13 10:07 elasti-co

You're welcome, but keep in mind this is just a workaround. There must, or at least may, be a better way to do this.

Sometimes, when some features are not documented, it's worth having a look at the code to understand how it works.

pjparra avatar Jul 20 '13 11:07 pjparra

Actually I've done this feature for my own use, I will put this in to our code later :+1:

bu avatar Jul 22 '13 06:07 bu

Any progress on this? I had a request for something similar (when a parent has only one child, they'd like it automatically selected).

brandonzylstra avatar Nov 21 '14 20:11 brandonzylstra