⚠ 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

[updated 24/11/2012] Dependent dropdown (library)



Gustavo Teles
  • profile picture
  • Member

Posted 26 November 2014 - 18:52 PM

Hi, I have the following problem:
ReferenceError: $ is not defined add: 307

public function index(){
        $crud = new grocery_CRUD();

        $crud->set_table('tb_estudo_biblico');
        $crud->set_subject('Estudo Biblico');

        $crud->display_as(
            array(
                'id_usuario' => 'Professor',
                'id_aluno'   => 'Aluno',
                'id_estudo'  => 'Estudo',
                'id_tema'    => 'Tema',
                'id_dia'     => 'Dia',
                'id_status'  => 'Status'
            )
        );

        $crud->set_relation('id_usuario', 'tb_usuario', 'nome');
        $crud->set_relation('id_aluno', 'tb_aluno', 'nome');
        $crud->set_relation('id_estudo', 'tb_estudo', 'estudo');
        $crud->set_relation('id_tema', 'tb_tema', 'tema');
        //$crud->set_relation_dependency('id_tema','id_estudo','id_estudo');
        $crud->set_relation('id_dia', 'tb_dia', 'dia', null, 'tb_dia.id asc');
        $crud->set_relation('id_status', 'tb_status', 'status');
        $crud->set_rules('id_usuario', 'Professor', 'required');
        $crud->set_rules('id_aluno', 'Aluno', 'required');
        $crud->set_rules('id_estudo', 'Estudo', 'required');
        $crud->set_rules('id_dia', 'Dia', 'required');
        $crud->set_rules('hora', 'Hora', 'required');
        $crud->set_rules('id_status', 'tb_status', 'required');
        $crud->field_type('hora', 'time');
        $crud->field_type('id_perfil', 'hidden', 3);
        $crud->unset_columns('id_perfil');
        $crud->unset_read();


        $fields = array(
            'id_estudo' => array(
                'table_name' => 'tb_estudo',
                'title' => 'estudo',
                'relate' => null
            ),
            'id_tema' => array(
                'table_name' => 'tb_tema',
                'title' => 'tema',
                'id_field' => 'id',
                'relate' => 'id_estudo',
                'data-placeholder' => 'Selecione o Tema'
            )
        );

        $config = array(
            'main_table' => 'tb_estudo_biblico',
            'main_table_primary' => 'id',
            //"url" => base_url() . 'admin/estudo_biblico' . '/' . __FUNCTION__ . '/',


            //"url" => base_url() . 'admin/estudo_biblico/index',
            //"url" => base_url() . 'admin/estudo_biblico' . '/' . __FUNCTION__ . '/',
            //"url" => base_url() . strtolower(__CLASS__) . '/' . strtolower(__FUNCTION__) . '/',
            //"url" => base_url() . 'admin/' . strtolower(__CLASS__) . '/' . strtolower(__FUNCTION__) . '/',

            //"url" => base_url() . 'index.php/admin/estudo_biblico/index',
            //"url" => base_url() . 'index.php/admin/estudo_biblico' . '/' . __FUNCTION__ . '/',
            //"url" => base_url() . 'index.php/' . strtolower(__CLASS__) . '/' . strtolower(__FUNCTION__) . '/',
            "url" => base_url() . 'index.php/admin/' . strtolower(__CLASS__) . '/' . strtolower(__FUNCTION__) . '/',
            'ajax_loader' => base_url('public/image') .'/'. 'loading.gif'
        );

        $categories = new gc_dependent_select($crud, $fields, $config);

        $js = $categories->get_js();
        $output = $crud->render();
        $output->output.= $js;

        $this->load->view('admin/home', $output);
    }

26066-a.png
 

0c67c-b.png


victor
  • profile picture
  • Member

Posted 30 November 2014 - 08:27 AM

You need to include Jquery in the <head> element.


denitriyandi
  • profile picture
  • Member

Posted 11 December 2014 - 03:01 AM

I have controller in a folder, but always show this error:

http://192.168.2.172/dev/master/Perkiraan/rekening/get_items/master_sub_kelompok_rekening_id/1

my folder is 'master' then my class perkiraan with function rekening....

 

what wrong with this? thanks viktor


gbarrerasaez.cl
  • profile picture
  • Member

Posted 06 January 2015 - 13:33 PM

What about having the option to remove the selected option in the dependent? it's possible?

 

91mbme.jpg


victor
  • profile picture
  • Member

Posted 06 January 2015 - 13:44 PM

What about having the option to remove the selected option in the dependent? it's possible?

 

91mbme.jpg

It works by default. I don't know why it doesn't work for you.

 

Updated:
Sorry. It doesn't work for dependents.


machinehd
  • profile picture
  • Member

Posted 14 January 2015 - 23:49 PM

Hi Victor!

First of all, thank you for your great work with this library! :)

But I'm facing a great difficult in implement the example 2 that you posted at the first page: for 2 dependent dropdown. I'm facing the same issue experienced by tofayelahmed user. May you help to get this solved?

thank you!


victor
  • profile picture
  • Member

Posted 16 January 2015 - 07:16 AM

Hello Machinehd and welcom to the forum. Give me a link to the tofayelahmed's post. 


machinehd
  • profile picture
  • Member

Posted 21 January 2015 - 13:42 PM

Hello Machinehd and welcom to the forum. Give me a link to the tofayelahmed's post. 

 

Hi Victor, 

 

He started to explain the issue at the page 2, post #33.

/topic/1087-updated-24112012-dependent-dropdown-library/page-2

 

Thank you!


Pedro Bernal
  • profile picture
  • Member

Posted 16 February 2015 - 20:49 PM

Hi Victor I am having a problem trying to use your library with one dependent select for country and city, I am getting this in my view:

 

 

 

foto%201_zpsg4atbt9t.png

 

This is my db configuration:

 

luc%20db_zpskcbax1iq.png

 

Finally this is my controller code:

public function registro1()
{
	$crud = new grocery_CRUD();
	$crud->set_table('empresa');
	$crud->set_rules('nombre_subscriptor','Nombre del Subscriptor','required|min_length[4]');
	$crud->set_rules('nombre','Nombre empresa que Representa','required|callback_empresa_nombre_check|min_length[4]');
	$crud->set_rules('nit','NIT','required|callback_empresa_nit_check|min_length[4]');
	$crud->set_rules('mail','Mail','required|callback_usuario_mail_check|min_length[7]|valid_email');
	$crud->set_rules('cargo','Cargo dentro de la Empresa','required|min_length[3]');
	$crud->set_rules('direccion','Dirección','required|min_length[3]');
	$crud->set_rules('pais','Pais','required');
	$crud->set_rules('ciudad','Ciudad','required');
	$crud->set_rules('decisiones_contratacion','Encargado decisiones de contratacion','required|min_length[2]');
	$crud->set_rules('celular','Celular','required|exact_length[10]|numeric');
	$crud->set_rules('telefono','Telefono', 'exact_length[7]|numeric');
	$crud->display_as('cargo','Cargo dentro de la Empresa');
	$crud->display_as('decisiones_contratacion','Depende la toma de decisiones de contratación de su empresa');
	$crud->display_as('nombre','Nombre de la empresa que Representa');
	$crud->field_type('decisiones_contratacion','dropdown',
                          array( "Si"  => "Si", "No" => "No", "Soy parte del proceso" => "Soy parte del proceso"));
	$crud->set_language("spanish");
	$crud->unset_fields('membresia');
	$crud->unset_back_to_list();
	$crud->unset_edit();
	$crud->unset_export();
	$crud->unset_print();
	$crud->unset_delete();
	$crud->unset_read();

	$crud->set_lang_string('insert_success_message',
	 'Tu información ha sido almacenada.<br/>Por favor espera.
	 <script type="text/javascript">
	  window.location = "'.site_url(strtolower('administrador').'/'.strtolower('confirmacion_registro')).'";
	 </script>
	 <div style="display:none">
	 '
   	);

	//INICIO SELECT LIST DEPENDIENTES
	$this->load->library('gc_dependent_select');
	$fields = array(
	// first field:
	'pais' => array( 
	'table_name' => 'paises', 
	'title' => 'nombre', 
	'relate' => null 
	),
	// second field
	'ciudad' => array( 
	'table_name' => 'ciudades', 
	'title' => 'nombre', 
	'id_field' => 'id', 
	'relate' => 'pais', 
	'data-placeholder' => 'Seleccione Ciudad' 
	)
	);
	$config = array(
		'main_table' => 'empresa',
		'main_table_primary' => 'id',
		"url" => base_url() .'index.php/'. __CLASS__ . '/' . __FUNCTION__ . '/' 
		);
	$categories = new gc_dependent_select($crud, $fields, $config);
	$js = $categories->get_js();
	$output = $crud->render();
	$output->output.= $js;
      	$output ->titular="Información de Destinos.";
      	$this->load->view('crud_registro',$output); 
}

victor
  • profile picture
  • Member

Posted 20 February 2015 - 20:43 PM

Try to use "strtolower" function here: "url" => base_url() .'index.php/'. __CLASS__ . '/' . __FUNCTION__ . '/'

 

or type full path as string (No constants)


Origin
  • profile picture
  • Member

Posted 23 March 2015 - 18:29 PM

Hi Victor, very good work that u did, can you help me with my problem. The problem i cant choose a farm that belongs to a choosen entity, the dropdown is not editable, i have to choose first the entity, save, and then edit so i can choose it, can you see the problem in my code? Thz

 

see the photos, and sorry for my english

[attachment=914:error1.jpg]

[attachment=915:error11.jpg]


Origin
  • profile picture
  • Member

Posted 23 March 2015 - 19:49 PM

Hi Victor, very good work that u did, can you help me with my problem. The problem i cant choose a farm that belongs to a choosen entity, the dropdown is not editable, i have to choose first the entity, save, and then edit so i can choose it, can you see the problem in my code? Thz

 

see the photos, and sorry for my english

attachicon.giferror1.jpg

attachicon.giferror11.jpg

Already resolved, i need to put  'index.php/'the path. thx :D


ryuusoultaker
  • profile picture
  • Member

Posted 10 April 2015 - 02:19 AM

hi victor,

 

I tried to follow your instruction, but I got this error 

 

 

Fatal error: Class 'gc_dependent_select' not found in C:\xampp\htdocs\etender2\application\controllers\admbarang.php on line 165

 

 

I don't know why it won't read the gc_dependent_select.php file even when I already put it on my libraries..

 

can you tell me where I'm doing wrong?


skekeu
  • profile picture
  • Member

Posted 20 April 2015 - 03:10 AM

Hi guys!

 

I have a little problem with this library.

 

The parent dropdown works, but the child dropdown keeps disabled with no data after changing the parent.

 

Here is my code

public function clientes(){
       	$crud = new grocery_CRUD();
        $crud->set_theme('bootstrap');
        $crud->set_table('clientes')
        ->set_subject('Cliente')
        ->columns('nome','telres','telcel','email')
        ->display_as('telres','Tel Residencial')
		->display_as('endereco','Endereço')
		->display_as('uf','UF')
		->display_as('cep','CEP')
        ->display_as('telcel','Tel Celular');
		
		$crud->set_rules('email','Email','valid_email');
 
        //$crud->fields('nome','telres','telcel','email','endereco','bairro','cidade','uf','cep');
        $crud->required_fields('nome','telres','endereco','bairro','cidade','uf');
	
	    //$crud->field_type('uf','dropdown',
            //array('RJ' => 'RJ', 'SP' => 'SP','MG' => 'MG' , 'ES' => 'ES'));
        
				
		$crud->set_relation('uf','estados','SIGLA');
        $crud->set_relation('cidade','cidades','NOME');
		
		$this->load->library('gc_dependent_select');
		
		$fields = array(

// first field:
'uf' => array( // first dropdown name
'table_name' => 'estados', // table of country
'title' => 'SIGLA', // country title
'relate' => null // the first dropdown hasn't a relation
),
// second field
'cidade' => array( // second dropdown name
'table_name' => 'cidades', // table of state
'title' => 'NOME', // state title
'id_field' => 'CODIGO_CIDADE', // table of state: primary key
'relate' => 'C_CODIGO_ESTADO', // table of state:
'data-placeholder' => 'Selecione a Cidade' //dropdown's data-placeholder:
)
);

		$config = array(
'main_table' => 'clientes',
'main_table_primary' => 'id',
"url" => base_url() . 'index.php/' . strtolower(__CLASS__) . '/' . strtolower(__FUNCTION__) . '/', //path to method
'ajax_loader' => base_url() . 'fancybox_loading.gif' // path to ajax-loader image. It's an optional parameter
);
$categories = new gc_dependent_select($crud, $fields, $config);

// first method:
//$output = $categories->render();

// the second method:
$js = $categories->get_js();
$output = $crud->render();
$output->output.= $js;
$this->_example_output($output);
		      
    } 

Here is my db scheme

irOa6XR.png

 

 Here is the js generated:

<script type="text/javascript">
$(document).ready(function() {
​var uf = $('select[name="uf"]');$('#uf_input_box').append('<img src="http://localhost/adv/fancybox_loading.gif" border="0" id="uf_ajax_loader" class="dd_ajax_loader" style="display: none;">');
var cidade = $('select[name="cidade"]');cidade.children().remove().end();uf.change(function() {var select_value = this.value;$('#uf_ajax_loader').show();
cidade.find('option').remove();var myOptions = "";$.getJSON('http://localhost/adv/index.php/main/clientes/get_items/cidade/'+select_value, 
function(data) {
if(data==''){cidade.children().remove().end();cidade.attr('disabled', true);cidade.find('option').remove();cidade.append('<option value=""></option>');cidade.trigger("liszt:updated");}
else{cidade.append('<option value=""></option>');$.each(data, function(key, val) {cidade.append($('<option></option>').val(val.value).html(val.property));});cidade.removeAttr('disabled');cidade.trigger("liszt:updated");}
uf.each(function(){$(this).trigger("liszt:updated");});cidade.each(function(){$(this).trigger("liszt:updated");});$('#uf_ajax_loader').hide();});});
});
</script>

I just can't see what I'm missing here.

Thanks for the help


skekeu
  • profile picture
  • Member

Posted 20 April 2015 - 11:41 AM

Hi guys!

 

I have a little problem with this library.

 

The parent dropdown works, but the child dropdown keeps disabled with no data after changing the parent.

 

Here is my code

public function clientes(){
       	$crud = new grocery_CRUD();
        $crud->set_theme('bootstrap');
        $crud->set_table('clientes')
        ->set_subject('Cliente')
        ->columns('nome','telres','telcel','email')
        ->display_as('telres','Tel Residencial')
		->display_as('endereco','Endereço')
		->display_as('uf','UF')
		->display_as('cep','CEP')
        ->display_as('telcel','Tel Celular');
		
		$crud->set_rules('email','Email','valid_email');
 
        //$crud->fields('nome','telres','telcel','email','endereco','bairro','cidade','uf','cep');
        $crud->required_fields('nome','telres','endereco','bairro','cidade','uf');
	
	    //$crud->field_type('uf','dropdown',
            //array('RJ' => 'RJ', 'SP' => 'SP','MG' => 'MG' , 'ES' => 'ES'));
        
				
		$crud->set_relation('uf','estados','SIGLA');
        $crud->set_relation('cidade','cidades','NOME');
		
		$this->load->library('gc_dependent_select');
		
		$fields = array(

// first field:
'uf' => array( // first dropdown name
'table_name' => 'estados', // table of country
'title' => 'SIGLA', // country title
'relate' => null // the first dropdown hasn't a relation
),
// second field
'cidade' => array( // second dropdown name
'table_name' => 'cidades', // table of state
'title' => 'NOME', // state title
'id_field' => 'CODIGO_CIDADE', // table of state: primary key
'relate' => 'C_CODIGO_ESTADO', // table of state:
'data-placeholder' => 'Selecione a Cidade' //dropdown's data-placeholder:
)
);

		$config = array(
'main_table' => 'clientes',
'main_table_primary' => 'id',
"url" => base_url() . 'index.php/' . strtolower(__CLASS__) . '/' . strtolower(__FUNCTION__) . '/', //path to method
'ajax_loader' => base_url() . 'fancybox_loading.gif' // path to ajax-loader image. It's an optional parameter
);
$categories = new gc_dependent_select($crud, $fields, $config);

// first method:
//$output = $categories->render();

// the second method:
$js = $categories->get_js();
$output = $crud->render();
$output->output.= $js;
$this->_example_output($output);
		      
    } 

Here is my db scheme

irOa6XR.png

 

 Here is the js generated:

<script type="text/javascript">
$(document).ready(function() {
​var uf = $('select[name="uf"]');$('#uf_input_box').append('<img src="http://localhost/adv/fancybox_loading.gif" border="0" id="uf_ajax_loader" class="dd_ajax_loader" style="display: none;">');
var cidade = $('select[name="cidade"]');cidade.children().remove().end();uf.change(function() {var select_value = this.value;$('#uf_ajax_loader').show();
cidade.find('option').remove();var myOptions = "";$.getJSON('http://localhost/adv/index.php/main/clientes/get_items/cidade/'+select_value, 
function(data) {
if(data==''){cidade.children().remove().end();cidade.attr('disabled', true);cidade.find('option').remove();cidade.append('<option value=""></option>');cidade.trigger("liszt:updated");}
else{cidade.append('<option value=""></option>');$.each(data, function(key, val) {cidade.append($('<option></option>').val(val.value).html(val.property));});cidade.removeAttr('disabled');cidade.trigger("liszt:updated");}
uf.each(function(){$(this).trigger("liszt:updated");});cidade.each(function(){$(this).trigger("liszt:updated");});$('#uf_ajax_loader').hide();});});
});
</script>

I just can't see what I'm missing here.

Thanks for the help

 

Never mind.

My code was working fine all along. I had an error on my masked input script that was preventing my dropdown to work.

 

Thanks victor for this amazing library. It will save tons of time.

 

CST


Adnan Ahmed
  • profile picture
  • Member

Posted 29 April 2015 - 12:59 PM

Hi Victor,

Can you see what mistake i am making?

Thanks


Adnan Ahmed
  • profile picture
  • Member

Posted 29 April 2015 - 13:40 PM

Sorted.

i was trying to create a separate function to operator.

My bad.

Anyway, Thanks Victor for your great work.


Fernando Bussmann
  • profile picture
  • Member

Posted 06 May 2015 - 13:03 PM

     Hello everyone, I`m using dependent dropdown Gc librarie exaclty as example of the librarie does, and it`s working fine. But when I was tryng to make a jquery to change the current value of the select, it doesen`t work.
     About structure: I have a main select that contains categories, and when I select one category another sub select opens with the respective subcategories. Until here it`s fine.
About the problems: I would like to start my select in the first position, so i did a jquery like this:
Obs.: select name: 'field-Cat_Codigo'
$(document).ready(function(){
     $("#field-Cat_Codigo").val(1);
});
 
But it doesen't work. It works fine to normal dropdowns, but not for dependent ones. So I tried a lot of things like this in differents order of trials as: 
$("#field-Cat_Codigo").trigger("liszt:updated");
$("#field-Cat_Codigo").val(1).trigger('change');
$("#field-Cat_Codigo").val(1).change();  
$("#field-Cat_Codigo").change(); 
 
In other application i did it, but when I need to change the sub dropdown I got the same error. So the thing is, How can I change both selects using jquery ?
 
Any idea?

 


draven
  • profile picture
  • Member

Posted 28 May 2015 - 13:52 PM

Hi all, i have two table with this fields:

table1:

id1

title1

 

table2:

id2

id_of_table1

title2

 

after this i have another table with this fields:

table3:

id3

id_of_table1

id_of_table2

title3

 

i would like to create a form of insert (in the table3) with two dropdown and an input field, when i make this the first dropdown is populated correctly but when i select one of the records, the second dropdown remain in disable mode.

 

this is my code:

 

$this->load->library('grocery_CRUD');
$crud = new grocery_CRUD();

$crud->set_theme('flexigrid');
$crud->set_table('gruppi');
$crud->set_subject('Gruppo');
$crud->required_fields('idapc', 'idset', 'gruppo');
$crud->columns('idapc', 'idset', 'gruppo');
$crud->display_as('idapc','Ass. Prima Casa');
$crud->display_as('idset','Settore');
$crud->display_as('gruppo','Gruppo');
$crud->set_relation('idset','settori','settore');
$crud->set_relation('idapc','apc','apc');

$this->load->library('gc_dependent_select');

$fields = array(
'idapc' => array(// first dropdown name
'table_name' => 'apc', // table of country
'title' => 'apc', // country title
'relate' => null // the first dropdown hasn't a relation
),
'idset' => array(// second dropdown name
'table_name' => 'settori', // table of state
'title' => 'settore', // state title
'id_field' => 'idset', // table of state: primary key
'relate' => 'idapc', // table of state:
'data-placeholder' => 'Scegli Settore' //dropdown's data-placeholder:
)
);
$config = array(
'main_table' => 'gruppi',
'main_table_primary' => 'idgrp',
'url' => base_url() .'index.php/'. __CLASS__ . '/' . __FUNCTION__ . '/'
);
$categories = new gc_dependent_select($crud, $fields, $config);
$js = $categories->get_js();

$output = $crud->render();
$output->output.= $js;

$this->load->view('gruppi_view',$output);

 

the project of this code contains codeigniter, grocery crud, Victor's library and bootstrap.... with bootstrap, the project have more and more js contents... so i thinked that the problem was this and i created a controller without loading bootstrap views (only grocery and Victor's library) but the result is the same :(

 

the gc_dependent_select is the 1.2 version and grocery is the 1.3.3 version

 

help me :( thanks a lot


Sebastián
  • profile picture
  • Member

Posted 14 July 2015 - 16:01 PM

Dear Victor, thanks a lot for this library. It works fine but I don't understand how to recieve the second dropdown data.
I get an html aswer from my config url (base_url() . __CLASS__./_MYFUNCTION), but how must be it structured? as a json array or like a select>option ?

Thanks for your help!
Best regards.