Community & Support

Chained Select/Dropdown using aJax & jQuery using Taxonomy

Can any one use this:

<!--

/**
function module_menu() {

$items['module/someform'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('module_someform'),
'access callback' => TRUE
);

$items['module/get_taxonomy_tree/%/%'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('module_get_taxonomy_tree', 3, 4),
'access callback' => TRUE,
'type' => MENU_CALLBACK
);

return $items;

}

function module_get_taxonomy_tree($form_state = array(), $vid, $pid) {

      header('Content-Type: application/xml; charset="utf-8"');
      echo("<?xml version=\"1.0\" encoding=\"utf-8\"?" . ">\n");
      echo("<option>\n");

$parent_taxonomy = taxonomy_get_tree($vid, $pid, -1, 1);
echo("\t<entry field='-" . count($parent_taxonomy) . "' value='- - -'>- - - </entry>\n");
foreach ($parent_taxonomy as $key => $value) {
      echo("\t<entry field='" . $parent_taxonomy[$key]->tid . "' value='" . htmlentities($parent_taxonomy[$key]->name) . "'>" . htmlentities($parent_taxonomy[$key]->description) . "</entry>\n");
}

echo("</option>\n");

exit();
}

function module_someform() {

      drupal_add_js(drupal_get_path('module', 'module') . '/select-taxonomies.js');

$form['category'] = array(
'#type' => 'select',
'#title' => t('Category'),
'#options' => array(),
'#attributes'     => array(
'chain-parent' => 0,
'chain-child' => 'issue',
'vid' => vocabulary-vid,
'pid'             => 0,
'clean_url' => variable_get('clean_url', 0),
'request' => 'module/get_taxonomy_tree'
)
);

$form['issue'] = array(
'#type' => 'select',
'#title' => t('Issue'),
'#options' => array(),
'#attributes'     => array(
'chain-parent' => 'category',
'chain-child' => 'topic',
'vid' => 0,
'pid'             => vocabulary-vid,
'clean_url' => variable_get('clean_url', 0),
'request' => 'module/get_taxonomy_tree'
)
);

$form['topic'] = array(
'#type' => 'select',
'#title' => t('Topic'),
'#options' => array(),
'#attributes'     => array(
'chain-parent' => 'issue',
'chain-child' => 0,
'vid' => 0,
'pid'             => vocabulary-vid,
'clean_url' => variable_get('clean_url', 0),
'request' => 'module/get_taxonomy_tree'
)
);

return($form);

}

*/

if (Drupal.jsEnabled) {

function HideElement(CurrentElement, action) {

switch (action) {
      case "hide":
            $('#edit-' + CurrentElement + '-wrapper').hide();
            break;
case "slideUp":
      $('#edit-' + CurrentElement + '-wrapper').slideUp(300);
      break;
}

}

      function HideChildElements(CurrentElement, action, init) {

            HideElement(CurrentElement, action);
            var child = $("#edit-" + CurrentElement).attr('chain-child');

$('#edit-' + CurrentElement + ' option').each(function(i) {
$(this).remove();
});

            if (parseInt(child) == 0) {
                  /** end of chain */
                  return;
            } else {
                  /** during chain */
                  HideChildElements(child, action, init);
if (init) {
$("#edit-" + CurrentElement).change( function() {
      HideChildElements(child, "slideUp", false);
                        PopulateElement("edit-" + child, $("#edit-" + CurrentElement).attr('pid'), $("#edit-" + CurrentElement).val());
                  });
}
            }

      }
     
      function ShowElement(CurrentElement, action) {
     
switch (action) {
      case "show":
            $('#edit-' + CurrentElement + '-wrapper').show();
            break;
case "slideDown":
      $('#edit-' + CurrentElement + '-wrapper').slideDown(400);
      break;
}
     
      }

      function PopulateElement(CurrentElement, vid, pid) {

var default_value = $('#' + CurrentElement).attr('default_value');
var clean_url = $('#' + CurrentElement).attr('clean_url');
var request = $('#' + CurrentElement).attr('request');
var child = $('#' + CurrentElement).attr('chain-child');

if (pid != 0) {
      var thisid = $('#' + CurrentElement).attr('id');
      /** bit messy */
thisid = thisid.replace(/edit-/gi, "");
HideElement(thisid, "slideUp");
}

var req_url = clean_url ? '/?q=' + request + '/' + vid + '/' + pid : '/' + request + '/' + vid + '/' + pid;
$.ajax({
url: req_url,
type: 'GET',
dataType: 'xml',
timeout: 1000,
error: function() {
alert('PopulateElement: Error loading XML document: ' + req_url);
},
success: function(xml) {
      var show = true;
      var firstrecord = 0;
      var popchild = 0;
$(xml).find('entry').each(function() {
var option = document.createElement("option");
option.value = $(this).attr('field');
option.text = $(this).attr('value');
if (default_value == $(this).attr('field')) {
      option.setAttribute('selected', 'selected');
      popchild = $(this).attr('field');
}
$('#' + CurrentElement).get(0)[$('#' + CurrentElement + ' option').length] = option;

if ((firstrecord == 0) && (parseInt($(this).attr('field')) == 0)) {
show = false;
firstrecord++;
}

});

if ((show) && (pid != 0)) {
      /** messy */
      if (popchild != 0) {
            ShowElement(thisid, "show");
      } else {
ShowElement(thisid, "slideDown");
}
}

if (popchild != 0) {
      PopulateElement('edit-' + child, $('#edit-' + child).attr('pid'), popchild);
}

}
});

      }

$(document).ready( function() {

      $('select[chain-parent]').each(function(i) {
     
            var parent = this.getAttribute('chain-parent');
            if (parent == 0) {
                  /** start of chain */
var thisid = this.getAttribute('id');
var child = this.getAttribute('chain-child');
var pid = this.getAttribute('pid');
var vid = this.getAttribute('vid');

                  HideChildElements(child, "hide", true);
                  $("#" + thisid).change( function() {
                        HideChildElements(child, "slideUp", false);
                        PopulateElement("edit-" + child, $("#edit-" + child).attr('pid'), $("#" + thisid).val());
                  });

                        PopulateElement(thisid, vid, pid);
                 
            }
     
      });

});

}
-->

Comments

By the way, it's a bit of

By the way, it's a bit of javascript, copy and paste into a file called 'select-taxonomies.js'
The formatting has gone wierd after I posted it...

Anyone who wants to use it, there's no restrictions as I wrote it.

Also I'd like to made a slight change to the code:
Where it says:

if ((show) && (pid != 0)) {
/** messy */
if (popchild != 0) {
ShowElement(thisid, "show");
} else {
ShowElement(thisid, "slideDown");
}
}

if (popchild != 0) {
PopulateElement('edit-' + child, $('#edit-' + child).attr('pid'), popchild);
}

It should say:

if ((show) && (pid != 0)) {
      ShowElement(thisid, "slideDown");
}

if ((popchild != 0) && (child != 0)) {
PopulateElement('edit-' + child, $('#edit-' + child).attr('pid'), popchild);
}

Sorry...

I tried it but

I tried using you code because it seems really good but didn't get it to work. I went through your code to see how you got it to work, pretty neat.

I tried to follow you code and your logic as much as possible but nothing happened...just empty select boxes...hope you can notice what i am doing wrong? Thanks alot

my code is below

function article_menu() {
$items[] = array(
'path' => 'article/someform',
'callback' => 'drupal_get_form',
'callback arguments' => array('article_someform'),
'access' => TRUE
);

$items['article/get_taxonomy_tree/%/%'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('article_get_taxonomy_tree', 5, 0),
'access callback' => TRUE,
'type' => MENU_CALLBACK
);

return $items();
}
function article_someform() {

drupal_add_js(drupal_get_path('module', 'article') . '/select-taxonomies.js');

$form['category'] = array(
'#type' => 'select',
'#title' => t('Category'),
'#options' => array(),
'#attributes'     => array(
'chain-parent' => 0,
'chain-child' => 'issue',
'vid' => vocabulary-vid,
'pid'             => 0,
'clean_url' => variable_get('clean_url', 0),
'request' => 'article/get_taxonomy_tree'
)
);

$form['issue'] = array(
'#type' => 'select',
'#title' => t('Issue'),
'#options' => array(),
'#attributes'     => array(
'chain-parent' => 'category',
'chain-child' => 0,
'vid' => 0,
'pid' => vocabulary-vid,
'clean_url' => variable_get('clean_url', 0),
'request' => 'article/get_taxonomy_tree'
)
);

return ($form)

}
nobody click here