⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

jQuery Chosen doesn't update cascading dropdown on IE8



particleandparcel

particleandparcel
  • profile picture
  • Member

Posted 05 August 2013 - 17:36 PM

I have a nearly impossible-to-debug problem that I've been unable to track down, but that appears to be an issue with IE8, the jQuery Chosen plugin, and some Ajax code I wrote to query the database and return a "cascading" dropdown menu via HTML and replace the original dropdown with the new one.

 

I was never able to make any of the plugin solutions offered for dropdown menus work, so I wrote my own, narrowly tailored to my purposes:

 

In controllers/main.php:

public function ajax_getlist() {
	$fields = $this->input->get_post('fields');
	$table = $this->input->get_post('table');
	$where = $this->input->get_post('where');
	$selectedid = $this->input->get_post('selectedid');
	$this->load->model('database_model');
	echo json_encode($this->database_model->getListOptions($fields, $table, $where, $selectedid));
}

In models/database_model:

public function getListOptions($fields, $table, $where, $selectedid) {
	// $fields has format array('label' => label, 'id' => ID)
	// $where has format array('field' => field, 'value' => value)
	$query = $this->_query($fields, $table, $where);
	if ($query->num_rows()) {
		$result = "<select id=\'field-$fields[label]\' name=\'$fields[label]\' class=\'chosen-select\' data-placeholder=\'Choose a $fields[label]\'>";
		foreach($query->result_array() as $options) {
			$selected = ($selectedid == $options[$fields['label']]) ? " selected=\'selected\'" : "";
			$result .= "<option$selected>" . $options[$fields['label']] . "</option>";
		}
		$result .= "</select>";
		return $result;
	} else {
		return false;
	}
}

private function _query($field, $table, $where = false) {
	if (is_array($field)) {
		$this->db->select(implode(",", $field));
	} else {
		$this->db->select($field);
	}
	$this->db->order_by("ID");
	$this->db->from($table);
	if ($where) {
		$this->db->where($where['field'], $where['value']);
	}
	return $this->db->get();
}

And then, in the footer of my view:

$.ajax(
	"<?= site_url('main/ajax_getlist') ?>",
	{
		type: 'GET',
		data: {
			"fields": {
				"label": "JobTitles",
				"id": "ID"
			},
			"table": "jobtitlesdescriptions",
			"where": {
				"field": "Dept",
				"value": $("select#field-Dept").val()
			},
			"selectedid": $("select#field-JobTitles").val()
		},
		success: function(data) {
			data = data.replace(/\\/g, '');
			$("select#field-JobTitles").chosen().html(data);
			$("select#field-JobTitles").trigger("liszt:updated");
		}
	}
);
$("select#field-Dept").chosen().on('change', function() {
	$.ajax(
		"<?= site_url('main/ajax_getlist') ?>",
		{
			type: 'GET',
			data: {
				"fields": {
					"label": "JobTitles",
					"id": "ID"
				},
				"table": "jobtitlesdescriptions",
				"where": {
					"field": "Dept",
					"value": $("select#field-Dept").val()
				},
				"selectedid": $("select#field-Dept").val()
			},
			success: function(data) {
				data = data.replace(/\\/g, '');
				$("select#field-JobTitles").chosen().html(data);
				$("select#field-JobTitles").trigger("liszt:updated");
			}
		}
	);
});

This works fantastic in modern browsers (Chrome, Firefox, Safari, IE10). However, it does not work in IE 8. The list is never populated, and it looks like the attached image.

 

Any thoughts? Has anyone else had a problem like this?


davidoster

davidoster
  • profile picture
  • Member

Posted 05 August 2013 - 22:28 PM

Check here it might help you.


particleandparcel

particleandparcel
  • profile picture
  • Member

Posted 06 August 2013 - 14:35 PM

Check here it might help you.

 

Unfortunately I already tried that. It's not an internal Web site, and IE 8 doesn't indicate that it's running in compatibility mode. I suppose I could disable Chosen altogether for IE 8, but it works on everything else, just not this one piece.


davidoster

davidoster
  • profile picture
  • Member

Posted 07 August 2013 - 04:48 AM

Hmmm! I don't know then! Maybe somebody with a bit more experience on this might comment on it!