Hi Wim,
Great module, thanks. I'm actually using the D6 version of this module (but it still doesn't have it's own branch so I filed this under 5.x-3.x-dev). I'm using Hierarchial Select with my own datasource, by implementing your hooks which is working great...

However, what I want to do is insert some html onto the page after each selection. For example, my hierarchy goes like this:

Country -> City -> School -> Course

After the user selects a Country, I need to insert some text about the country just below the Country selector. Then, when the user selects a City, I want to insert some more text, this time about the selected city, just below the City selector, and so on..

I have read the API docs, and know that to do this I need to bind a javascript function onto the 'change-hierarchical-select' event, something like this:

$('#hierarchical-select-'+hsid+'-wrapper')
  .bind('change-hierarchical-select', function() {
    $('#hierarchical-select-'+hsid+'-wrapper .hierarchical-select').append('TEST');
  });

This code is a start, and after making a selection, the string TEXT now gets appended just after all the selectors.

However, I need my Javascript function to know which element is currently selected inorder to insert my text at the right place. I also need to be able to pass my JavaScript function some extra information (description of country or city etc). But, I can't find the right place to add this extra information to the JavaScript - which I guess I'd do with Drupal.Settings. Presumably I'd do this in one of my hook implementations... but which one? Have you done something like this before? Can you give me any pointers?

Thanks,
Tom

Comments

Wim Leers’s picture

Assigned: Unassigned » Wim Leers
Status: Active » Postponed (maintainer needs more info)

For the change-hierarchical-select-event, you'll receive an array of settings. IIRC, this is only one setting: select_id. This allows you to do something like:

$('#hierarchical-select-'+hsid+'-wrapper')
  .bind('change-hierarchical-select', function(settings) {
    var selectedTermTid = $('#' + settings.select_id).value();

    // Do a callback to the server to obtain more information about the selected term, by using the tid we just detected.
    var info = $doAjaxCallbackToGetInfoAboutThisTerm();

    $('#hierarchical-select-'+hsid+'-wrapper .hierarchical-select').append(info);
  });

I think that answers your question?

mrfelton’s picture

Ah, so there is a parameter... However, it doesn't seem to be an array of settings, but instead it's an Object of type change-hierarchical-select. From what I can tell, this appears to be the hierarchical-select-0-wrapper element, rather than the actual select element that triggered the event. This is making it a little difficult to work with, since the only way I can find of getting the selectedIndex of the select element that caused the event is by working my way down the child nodes until I get to the penultimate one... Must be a better way...

mrfelton’s picture

OK, I got it. There are actually 3 paramaters that get passed to the event:

  Drupal.HierarchicalSelect.triggerEvents(hsid, updateType, settings);

So what I needed was:

  $('#hierarchical-select-'+hsid+'-wrapper')
    .bind('change-hierarchical-select', function(hsid, updateType, settings) {
      var selectedTermTid = $('#' + settings.select_id).val();
      // Do a callback to the server to obtain more information about the selected term, by using the tid we just detected.
      var info = $doAjaxCallbackToGetInfoAboutThisTerm(selectedTermTid);
  
      $('#hierarchical-select-'+hsid+'-wrapper .hierarchical-select').append(info);
    });
mrfelton’s picture

Now, the problem I have now is that HS rebuilds the form elements each time a select is made, so any edits I make get lost when a new select is made. As far as I can tell, this is because the HS Ajax callback is calling drupal_prepare_form() and drupal_render() to rebuild the form - but I'm not really sure how this works. Any pointers as to how I can get my edits to stick between selects?

Wim Leers’s picture

RE #3: right, what I typed was mostly off the top of my head. At least I got you in the right direction :)

RE #4: oh, so you're not just displaying text? But you're actually editing the selects? That's not supported, and will never be supported. You should write a custom implementation of the HS API then.
But before you jump into doing that: what exactly are you trying to do? And why are you doing it through JS?

mrfelton’s picture

OK, I'm not actually trying to edit the selects. I'm trying to insert text between the selects. I want it so that when you choose an option from the first select, some text about the selection should appear below the first select. Then, after choosing a selection from the second select, some text about that selection should then be inserted just below the second select (with the extra text I added from the first select remaining betwee the first and second select). I currently have two problems:

  1. The extra text I added from the first select should remain on screen after making a selection from the second select - likewise, after making a selection from the third select, the extra text from the first 2 selects should remain on screen. At the moment, all my extra text from previous selections gets removed each time a new selection is made - I think due to the way HS rebuilds the form element.
  2. Ideally, I would like to use the before-change-hierarchical-select, so that the text is displayed before the HS animation. However, if I do this, what happens is that the text briefly appears on screen, only to disappear again instantly. As far as I can tell, this is also due to the way the HS rebuilds the form element from scratch in it's Ajax callback.

Now I have two ideas about how to solve it: Either, I maintain my own cache and insert all my bits of text in to the relevant places each time a new selection is made. Or, I somehow use form_alter in my Ajax callback to alter the form itself - although I don't think this is going to work, again due to the way that HS dynamically builds the select elements.

Wim Leers’s picture

Ok, understood.

Your best choice is then to indeed use hook_form_alter(). If the HS is at $form['foo'], you'll want to look at $form['foo']['hierarchical_select']['selects'].

Wim Leers’s picture

Did you get this to work properly? I'll try to help you out if you haven't yet.

mrfelton’s picture

No, I didn't. I ended up just using one static dev on the page, and replacing the entire content of the div each time a selection is made. This doesn't give the effect I was after, but it'll do for now. Some help would be really appreciated if you have the time.

Wim Leers’s picture

Ok. I'll help you after I get a D6 branch out.

cerber0s’s picture

I was very stuck until I found this thread, thanks Wim/mrfelton.

Here's a working example;

$(document).ready(function(){
  $('#hierarchical-select-0-wrapper')
  .bind('change-hierarchical-select', function(hsid, updateType, settings) {
    var selectedTermTid = $('#' + settings.select_id).val();
    alert(selectedTermTid);
  });
});

PS great work Wim, keep it up :)

mrfelton’s picture

here is my working example:
http://www.geminicourses.com/book-now

Still not working how I originally intended, but it's good enough.

Wim Leers’s picture

Version: 5.x-3.x-dev » 6.x-3.x-dev
Component: Code » Documentation
Category: support » task
Status: Postponed (maintainer needs more info) » Postponed

This is a really advanced use case that I'll work on when I've got more time. Which will probably/hopefully be this summer.

jpdaut’s picture

Mrfelton: Did you use callbacks to the server to obtain more info about selected terms in your implementation?

I need to get more info from the taxonomy terms selected. I'm trying thru Drupal.settings.taxonomy, but am not successful so far.

Just wondering how you went about your implementation.

Summit’s picture

Hi, Could you use this method to show the taxonomy description underneath the selected term, before you select a term in a hierarchy deeper?
Thanks a lot in advance for your reply!

greetings,
Martijn

Wim Leers’s picture

I suppose so.

This needs to be defined better, and is something for HS 4.

Summit’s picture

Hi Wim,

Thanks for your reply! Hopefully HS 4 will also be on D6, right?

Greetings, Martijn

Wim Leers’s picture

I honestly don't know, because there are no actual plans for HS 4 yet.

Wim Leers’s picture

Issue tags: +HS4

Tagging for HS4. Included in the HS4 roadmap: http://drupal.org/node/1052670.

Wim Leers’s picture

Component: Documentation » Code
klonos’s picture

Version: 6.x-3.x-dev » 7.x-3.x-dev

This will happen in the 7.x branch.

eliosh’s picture

In D7 version it doesn't work.
Changing it to this:

jQuery("body").delegate('#hierarchical-select-0-wrapper', 'change-hierarchical-select', function(event, hsid, updateType, settings){
  debugger;
  console.log("Hola");
});

works again.