⚠ 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

[Solved] Dependent Multiselect



Julio
  • profile picture
  • Member

Posted 18 June 2013 - 00:30 AM

Hi to all,

 

Following this link /topic/72-categories-and-subcategories/ I made some changes and fit the code to use in my project, but I need to use this issue not with dropdown lists, but with multiselect.

 

The problem I'm facing is that my multiselect doesn't update the list. With firebug I got no errors and the ajax return it's ok bringing the correct value, but when I click on the multiselect it shows the whole list.

 

I also found this link /topic/1730-triggerlisztupdated-not-working-on-multi-select/ but didn't help me.

 

In my code my multiselect field is the CODREG. With other dropdown fields like CODLOC or CODJOR work fine.

 

Could someone help me undertand where's the problem?

 

Controller:

...

        $crud->display_as('CODREG','Regionais');
        $regionais = $this->main_model->buscar_descricao_regionais();
        foreach ($regionais->result() as $reg){
            $array_regionais[$reg->CODREG] = $reg->DESREG;
        }
        $crud->field_type('CODREG','multiselect', $array_regionais);

...	  
        $output = $crud->render();
		
		$dd_data = array(
			'dd_state' =>  $crud->getState(),
			'dd_dropdowns' => array('CODPER','CODLOC','CODREG','CODJOR'),
			'dd_url' => array('', site_url().'main/carregar_locais_preparacao/',site_url().'main/carregar_regionais_preparacao/',site_url().'main/carregar_jornadas_preparacao/'),
			'dd_ajax_loader' => base_url().'ajax-loader.gif'
		);
		$output->dropdown_setup = $dd_data;

		$this->template($output);

...

    function carregar_regionais_preparacao()
    {
        $periodo = $this->uri->segment(3);
        $db = $this->main_model->carregar_regionais_por_periodo($periodo);
        $array = array();
        foreach($db->result() as $row):
            $array[] = array("value" => $row->CODREG, "property" => $row->DESREG);
        endforeach;
        
        echo json_encode($array);
        exit;
    }

Model:

function carregar_regionais_por_periodo($periodo)
	{
        return $this->db->query('select CODREG, DESREG
								   from e015reg
								  where e015reg.CODREG =1');										 
	}

Thanks to all.


Julio
  • profile picture
  • Member

Posted 20 June 2013 - 00:21 AM

Anyone could help me?


davidoster
  • profile picture
  • Member

Posted 20 June 2013 - 16:08 PM

Did you try to use this or this instead?


Julio
  • profile picture
  • Member

Posted 23 June 2013 - 00:17 AM

Yes, I did. I've already tried these libraries, but the only that gets closer for what I need is this /topic/72-categories-and-subcategories/.

 

I found out where's the problem in my code, but I don't how to fix it.

 

Inputs like multiselect have brackets in theirs name, example:

...multiple="multiple" name="CODREG[]"...

So in the code below I don't know how to set the code to accept both dropdown and multiselect treating the brackets as the field type.

<?php
if(isset($dd_state) && ($dd_state == 'add' || $dd_state == 'edit')) {
//DONT HAVE TO EDIT THE CODE BELOW IF YOU DONT WANNA :P
echo '<script type="text/javascript">';
echo '$(document).ready(function() {';
for($i = 0; $i <= sizeof($dd_dropdowns)-1; $i++):
	//SET VARIABLES
	echo 'var '.$dd_dropdowns[$i].' = $('select[name="'.$dd_dropdowns[$i].'"]');';
	//SET LOADING IMAGES
	if($i != sizeof($dd_dropdowns)-1) {
		echo '$('#'.$dd_dropdowns[$i].'_input_box').append('<img src="'.$dd_ajax_loader.'" border="0" id="'.$dd_dropdowns[$i].'_ajax_loader" class="dd_ajax_loader" style="display: none;">');';
	}
	
	if($i > 0 && $dd_state == 'add') {
		//HIDE ALL CHILD ITEMS
		echo '$('#'.$dd_dropdowns[$i].'_input_box').hide();';
		//REMOVE CHILD OPTIONS
		echo $dd_dropdowns[$i].'.children().remove().end();';
	}
endfor;

for($i = 1; $i <= sizeof($dd_dropdowns)-1; $i++):
	//CHILD DROPDOWNS
	echo $dd_dropdowns[$i-1].'.change(function() {';
	echo 'var select_value = this.value;';
	
	//SHOW LOADING IMAGE
	echo '$('#'.$dd_dropdowns[$i-1].'_ajax_loader').show();';
	//REMOVE ALL CURRENT OPTIONS FROM CHILD DROPDOWNS
	echo $dd_dropdowns[$i].'.find('option').remove();';
	//POST TO A CUSTOM CONTROLLER ADDING OPTIONS | JSON
	echo 'var myOptions = "";';
	//GET JSON REQUEST OF STATES
	echo '$.getJSON(''.$dd_url[$i].''+select_value, function(data) {';
	//APPEND RECEIVED DATA TO STATES DROP DOWN LIST
	echo $dd_dropdowns[$i].'.append('<option value=""></option>');';
	echo '$.each(data, function(key, val) {';
	echo $dd_dropdowns[$i].'.append(';
	echo '$('<option></option>').val(val.value).html(val.property)';
	echo ');';
	echo '});';
	//SHOW CHILD SELECTION FIELD
	echo '$('#'.$dd_dropdowns[$i].'_input_box').show();';
	//MAKE SURE CITY STILL HIDDEN INCASE OF COUNTRY CHANGE
	for($x = $i+1; $x <= sizeof($dd_dropdowns)-1; $x++):
		echo '$('#'.$dd_dropdowns[$x].'_input_box').hide();';
	endfor;
	//RESET JQUERY STYLE OF DROPDOWN LIST WITH NEW DATA
	echo $dd_dropdowns[$i-1].'.each(function(){';
	echo '$(this).trigger("liszt:updated");';
	
	//aqui
	//echo '$(this).multiselect('refresh');';	
	
	echo '});';
	echo $dd_dropdowns[$i].'.each(function(){';
	echo '$(this).trigger("liszt:updated");';
	
	//aqui
	//echo '$(this).multiselect('refresh');';		
	
	echo '});';
	//HIDE LOADING IMAGE
	echo '$('#'.$dd_dropdowns[$i-1].'_ajax_loader').hide();';
	echo '});';
	echo '});';
endfor;
echo '});';
echo '</script>';
}
?>

Julio
  • profile picture
  • Member

Posted 23 June 2013 - 22:33 PM

Hi, problem solved.

 

Added an if condition on the line below to receive brackets after field name when field type like multiselect:

echo 'var '.$dd_dropdowns[$i].' = $('select[name="'.$dd_dropdowns[$i].'[]"]');';