⚠ 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)



pbcomput
  • profile picture
  • Member

Posted 04 April 2014 - 10:27 AM

Hi,

 

I am using this library to make something like category and subcategory. But I am not getting proper value in select boxes of category and sub category. Below is my code, can anyone please check my code and tell me, where I am doing wrong.

 

In my example the themes are as category and subthemes are as subcategory.

// THEME & SUBTHEME
	function check_theme ()
	{
		$crud = new grocery_CRUD();
		$crud->set_table('articles');
		$crud->set_relation('article_theme_id', 'themes', 'theme_name');
		$crud->set_relation('article_subtheme_id', 'sub_themes', 'subtheme_name');
		$crud->unset_add_fields('article_content');

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

		$fields = array(

		// first field:
		'Themes' => array( // first dropdown name
		'table_name' => 'themes', // table of country
		'title' => 'theme_name', // country title
		'relate' => null // the first dropdown hasn't a relation
		),
		// second field
		'SubThemes' => array( // second dropdown name
		'table_name' => 'sub_themes', // table of state
		'title' => 'subtheme_name', // state title
		'id_field' => 'subtheme_id', // table of state: primary key
		'relate' => 'theme_ids', // table of state:
		'data-placeholder' => 'select subthem' //dropdown's data-placeholder:

		),
		);

		$config = array(
		'main_table' => 'articles',
		'main_table_primary' => 'article_id',
		"url" => base_url().'index.php/'.__CLASS__.'/'.__FUNCTION__.'/', //path to method
		'ajax_loader' => base_url().'img/ajax-loaders/ajax-loader-2.gif', // path to ajax-loader image. It's an optional parameter
		'segment_name' =>'Your_segment_name' // It's an optional parameter. by default "get_items"
		);
		$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 the screenshots of my above code. By default its showing all records of themes and subthemes. But I want only those subtheme which theme selected from the theme dropdown.

 

wP9Preh.png

 

MWsVNU9.png

Thanks for your help.! :)


carmad94
  • profile picture
  • Member

Posted 12 August 2014 - 03:46 AM

What if I use this library for un normalized database. For example dd_city, dd_state, and dd_country are merged into one table with null values pertains to no relation. The primary key for this table is used to identify immediately to determine the value of the city, state and, country. Is it possible and applicable? If yes, how so?


carmad94
  • profile picture
  • Member

Posted 13 August 2014 - 06:14 AM

Hello my grocery crud is not working. It does not show anything from the second field if I select the first field. Anyone knows how to fix it?


Sanket Bhatt
  • profile picture
  • Member

Posted 21 August 2014 - 09:15 AM

Hello,
 
i am new to grocerycrud and this my first post here
i have implemented this Dependent dropdown (library) to achive the dependent field of country, state, city tables
the simple purpose is to show related states when some country is selected and also related cities to be shown when some state is selected.
i have achieved this reading the instructions and discussion on forum, very thanks to @victor
 
however i am stuck at a point now, i only want to show the cities and states that have status equal to true and active in my database
i not able to find a way or method using which i can do so.
 
can someone please help me doing this, when i select a country, i should only get the list of all active (status=1) states in that country, and same way when i select a state i should get a list of all active (status=1) cities in that state.
i hope the question is not confusing, please check the below code and attached database for reference
 
Any help or suggestions would be very helpful to me
 
    public function index() {
        try {
            $this->load->library('ajax_grocery_CRUD');
            $crud = new ajax_grocery_CRUD();
            $crud->where('advertisements.status','1');
            $crud->set_theme('datatables');
            $crud->set_table('advertisements');
            $crud->set_subject('Advertisements');
 
            $crud->fields('module_id', 'category_id', 'country_id' , 'state_id' , 'city_id' , 'frame_location' ,
                    'start_date' , 'end_date' , 'image' , 'firstname' , 'lastname' ,
                    'address' , 'contact' , 'email' , 'price' ,'date_modified' , 'date_create');
            $crud->field_type('module_id', 'dropdown', array('1' => 'fanchise', '2' => 'service',
                '3' => 'coupon'))
                    ->field_type('frame_location', 'dropdown' , array('1'=>'Home Page' , 
                        '2'=>'Internal Page'));
            $crud->set_field_upload('image' , 'assets/uploads/advertisements');
            $crud->columns('module_id', 'name', 'parent_id');
            $crud->set_relation('category_id', 'categories', 'name');
            $crud->set_relation_dependency('category_id','module_id','module_id');
            $crud->set_relation('country_id','countries','name');
            $crud->set_relation('state_id','states','name');
            $crud->set_relation_dependency('state_id','country_id','country_id');
            $crud->set_relation('city_id', 'cities', 'name');
            $crud->set_relation_dependency('city_id','state_id','state_id');
            $crud->display_as('module_id', 'module')
                    ->display_as('category_id', 'category')
                    ->display_as('city_id', 'city');
            $crud->required_fields('module_id', 'category_id', 'country_id' , 'state_id' , 'city_id' , 'frame_location' ,
                    'start_date' , 'end_date' , 'image' , 'firstname' , 'lastname' ,
                    'address' , 'contact' , 'email' , 'price');
            
            $crud->change_field_type('date_create','invisible');
            $crud->change_field_type('date_modified','invisible');
            
            $crud->callback_delete(array($this, 'delete_advertisements'));
            $output = $crud->render();
            $output->subject = "Advertisements";
            $this->_advertisements_output($output);
        } catch (Exception $e) {
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }
    }
    function insert_data_callback($post_array) {
        $post_array['date_create'] = date('Y-m-d H:i:s');
        $post_array['date_modified'] = date('Y-m-d H:i:s');        
        return $post_array;
    }
 
    public function delete_advertisements($primary_key) {
        return $this->db->update('advertisements', array('status' => 0), array('id' => $primary_key));
    }
 
    function _advertisements_output($output = null) {
        $this->load->view('view.php', $output);
    }
 
 
[attachment=818:advertisements-page.jpg]

 

http://www.files.com/image/53f5b894828a4/database.jpg

 


Pedro Bernal
  • profile picture
  • Member

Posted 20 October 2014 - 18:01 PM

Hi,

 

I am using this library to make something like category and subcategory. But I am not getting proper value in select boxes of category and sub category. Below is my code, can anyone please check my code and tell me, where I am doing wrong.

 

In my example the themes are as category and subthemes are as subcategory.

// THEME & SUBTHEME
	function check_theme ()
	{
		$crud = new grocery_CRUD();
		$crud->set_table('articles');
		$crud->set_relation('article_theme_id', 'themes', 'theme_name');
		$crud->set_relation('article_subtheme_id', 'sub_themes', 'subtheme_name');
		$crud->unset_add_fields('article_content');

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

		$fields = array(

		// first field:
		'Themes' => array( // first dropdown name
		'table_name' => 'themes', // table of country
		'title' => 'theme_name', // country title
		'relate' => null // the first dropdown hasn't a relation
		),
		// second field
		'SubThemes' => array( // second dropdown name
		'table_name' => 'sub_themes', // table of state
		'title' => 'subtheme_name', // state title
		'id_field' => 'subtheme_id', // table of state: primary key
		'relate' => 'theme_ids', // table of state:
		'data-placeholder' => 'select subthem' //dropdown's data-placeholder:

		),
		);

		$config = array(
		'main_table' => 'articles',
		'main_table_primary' => 'article_id',
		"url" => base_url().'index.php/'.__CLASS__.'/'.__FUNCTION__.'/', //path to method
		'ajax_loader' => base_url().'img/ajax-loaders/ajax-loader-2.gif', // path to ajax-loader image. It's an optional parameter
		'segment_name' =>'Your_segment_name' // It's an optional parameter. by default "get_items"
		);
		$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 the screenshots of my above code. By default its showing all records of themes and subthemes. But I want only those subtheme which theme selected from the theme dropdown.

 

wP9Preh.png

 

MWsVNU9.png

Thanks for your help.! :)

 

 

I think that we share the same problem I could fix mine just changing the Caps here:

 

'Themes' => array( // first dropdown name

'SubThemes' => array( // second dropdown name

 

should be like that:

 

'themes' => array( // first dropdown name

'subthemes' => array( // second dropdown name


fbn
  • profile picture
  • Member

Posted 05 November 2014 - 17:46 PM

Hi,

 

This is my first post.

 

Sorry for my english.

 

I am not getting a value on the second select - "caso".

Below is my code and database structure, can anyone please check my code and tell me, where I am doing wrong.

 

<?php

class VisualizarServicos extends CI_Controller {

    function index() {

        $crud = new Grocery_CRUD();
        $crud->set_theme('datatables');
        $crud->set_table('servico');
        $crud->set_subject('Serviço');
        $crud->columns('tituloServico', 'assunto', 'servico_caso_id', 'servico_cliente_id');
        $crud->display_as('servico', 'Serviço');
        $crud->display_as('servico_caso_id', 'Caso');
        $crud->display_as('servico_cliente_id', 'Cliente');
        $crud->display_as('numeroProcesso', 'Número de Processo');
        $crud->display_as('tipoDeAcao', 'Tipo de Ação');
        $crud->display_as('numeroInquerito', 'Número de inquérito');
        $crud->display_as('dataEntrada', 'Data de entrada');
        $crud->display_as('tituloServico', 'Título');
        $crud->display_as('juizo', 'Juízo');
        $crud->order_by('tituloServico');
        $crud->edit_fields('tituloServico', 'assunto', 'numeroProcesso', 'tipoDeAcao', 'numeroInquerito', 'tribunal', 'juizo', 'servico_cliente_id', 'servico_caso_id');
        $crud->add_fields('tituloServico', 'assunto', 'numeroProcesso', 'tipoDeAcao', 'numeroInquerito', 'tribunal', 'juizo', 'servico_cliente_id', 'servico_caso_id');
        $crud->set_relation('servico_cliente_id', 'cliente', 'nome');
        $crud->set_relation('servico_caso_id', 'caso', 'tituloCaso');

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

        $fields = array(
            'servico_cliente_id' => array(// first dropdown name
                'table_name' => 'cliente', // table of country
                'title' => 'nome', // country title
                'relate' => null // the first dropdown hasn't a relation
            ),
            'servico_caso_id' => array(// second dropdown name
                'table_name' => 'caso', // table of state
                'title' => 'tituloCaso', // state title
                'id_field' => 'casoid', // table of state: primary key
                'relate' => 'caso_cliente_id', // table of state:
                'data-placeholder' => 'Selecionar Caso' //dropdown's data-placeholder:
            )
        );
        $config = array(
            'main_table' => 'servico',
            'main_table_primary' => 'servicoid',
            "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->_servico_output($output);
        //------------------------ Dependency end --------------------------        
    }


    function _servico_output($output = null) {


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


}
 

 

 

 

 

 

 

 

 

 

 


victor
  • profile picture
  • Member

Posted 06 November 2014 - 13:12 PM

Hi fbn and welcom to the forum! Do you get any errors? What does shows debug panel in your brouser?


fbn
  • profile picture
  • Member

Posted 06 November 2014 - 16:05 PM

Thank you Victor,

 

I got the following error:

 

Uncaught ReferenceError: $ is not defined add:448

(anonymous function)

 

<script type="text/javascript">
    $(document).ready(function() {
        var servico_cliente_id = $('select[name="servico_cliente_id"]');
        var servico_caso_id = $('select[name="servico_caso_id"]');
        servico_caso_id.children().remove().end();
        servico_cliente_id.change(function() {
            var select_value = this.value;
            servico_caso_id.find('option').remove();
            var myOptions = "";
            $.getJSON('http://localhost/advocatus_iuris/index.php/VisualizarServicos/index/get_items/servico_caso_id/' + select_value, function(data) {
                if (data == '') {
                    servico_caso_id.children().remove().end();
                    servico_caso_id.attr('disabled', true);
                    servico_caso_id.find('option').remove();
                    servico_caso_id.append('<option value=""></option>');
                    servico_caso_id.trigger("liszt:updated");
                } else {
                    servico_caso_id.append('<option value=""></option>');
                    $.each(data, function(key, val) {
                        servico_caso_id.append($('<option></option>').val(val.value).html(val.property));
                    });
                    servico_caso_id.removeAttr('disabled');
                    servico_caso_id.trigger("liszt:updated");
                }
                servico_cliente_id.each(function() {
                    $(this).trigger("liszt:updated");
                });
                servico_caso_id.each(function() {
                    $(this).trigger("liszt:updated");
                });
            });
        });
    });
</script>

 

I am a novice programmer.

 


victor
  • profile picture
  • Member

Posted 06 November 2014 - 16:10 PM

is Jquery included in your html?


victor
  • profile picture
  • Member

Posted 06 November 2014 - 16:18 PM

It looks like Jquery included at the bottom of your code or the library is not included at all


fbn
  • profile picture
  • Member

Posted 06 November 2014 - 16:18 PM

Yes, jQuery v1.10.2, maybe this jquery version is out of date.


fbn
  • profile picture
  • Member

Posted 06 November 2014 - 16:25 PM

Yes, the Jquery is included at the bottom of my code.


fbn
  • profile picture
  • Member

Posted 06 November 2014 - 16:28 PM

Now it's working!!   :D  Thank you Victor!!  :D Your library is amazing!


Mohammed
  • profile picture
  • Member

Posted 09 November 2014 - 18:43 PM

Hi,
I have one doubt, I'm new to grocery CRUD library , I'm using your library dependent Drop down list version 1.2.0 .
Have an issue second drop down is always disable on add new fields, But dependent drop down is working only on edit mode based on existing value only , not for new selected value.
 
My table structure is
 
Stud_info-  attributes are

Stud_id, stud_name, depart_id, date_of_joining.


dept_info :  attributes are 
dept_id, dept_name, dept_branch
 
My controller code:
$crud->set_relation('stud_id','stud_info','stud_name');



        $crud->set_relation('dept_id','dept_info','dept_name');
        
        $this->load->library('gc_dependent_select');
        $fields = array(


// first field:
        'dept_id' => array( 
        'table_name' => 'dept_info',
        'title' => 'dept_name',
        'relate' => null ),


        // second field
        'stud_id' => array( 
        'table_name' => 'stud_info', 
        'title' => 'stud_name', 
        'id_field' => 'stud_id',
        'relate' => 'dept_id', 
        'data-placeholder' => 'select student name'));
        
        
        $config = array(

                        'main_table' => 'class',
                        'main_table_primary' => 'class_id',
                        "url" => base_url() . __CLASS__ . '/' . __FUNCTION__ . '/',
                        'ajax_loader' => base_url() . 'ajax-loader.gif' ,
                       'segment_name' =>'get_items' 
                        );
                        $categories = new gc_dependent_select($crud, $fields, $config);
        
        $js = $categories->get_js();
        $output = $crud->render();
        $this->_example_output($output);  

victor
  • profile picture
  • Member

Posted 10 November 2014 - 14:25 PM

Hi @Mohammed! Welcome to the forum!
Read please all pages of the topic. There are a lot of messages about common mistakes. If you then will not have the answer on your question I will try to help you.

Mohammed
  • profile picture
  • Member

Posted 11 November 2014 - 09:43 AM

Thanks Victor.


Mohammed
  • profile picture
  • Member

Posted 12 November 2014 - 06:23 AM

Hi Victor,

 

Dependent drop down is working fine, but one problem ,

The problem is working only on parent window, But child window(Script window) is not working always disable on second drop down.

 

Thanks in advance


step
  • profile picture
  • Member

Posted 23 November 2014 - 22:44 PM

Hi all GC Friends,

 

i'm a little noob developer and so... please help

 

this is my code:

 

                     
                        
//-------------------------------------------------------------------------------
                        $fields = array(
                        'id_pp' => array(// first dropdown name
                        'table_name' => 'anag_puntiprelievo', // table of pp
                        'title' => 'descrizione_pp', // pp title
                        'relate' => null // the first dropdown hasn't a relation
                        ),
                        'id_infermiere' => array(// second dropdown name
                        'table_name' => 'anag_infermieri', // table of infermieri
                        'title' => 'descr_infermiere', // state title
                        'id_field' => 'id_infermiere', // table of infermieri: primary key
                        'relate' => 'fk_id_pp', // table of infermieri:
                        'data-placeholder' => 'Seleziona Infermiere' //dropdown's data-placeholder:
                        )
                        );
                       
                        $config = array(
                        'main_table' => 'prenotazioni',
                        'main_table_primary' => 'id_prenotazione',
                        'url' => base_url() . 'index.php/' . strtolower(__CLASS__) . '/' . strtolower(__FUNCTION__) . '/',
                        'ajax_loader' => base_url() . 'style/images/'. 'ajax-loader.gif',
                        'segment_name' =>'get_items' // It's an optional parameter. by default "get_items"
                        );
                  

                        //echo $config['url'];
                        
      $pp = new gc_dependent_select($crud, $fields, $config);

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

    // the second method:
      $js = $pp->get_js();
      $output = $crud->render();
      $output->output.= $js;
      $this->_prenotazioni_output($output); 

 

dependent dropdown doesn't load related data but all table.... please help me and TY :))

 

these are my tables:

 

CREATE TABLE `prenotazioni` (
  `id_prenotazione` int(11) NOT NULL AUTO_INCREMENT,
  `nome` varchar(45) DEFAULT NULL,
  `cognome` varchar(45) DEFAULT NULL,
  `fk_id_puntoprelievo` int(11) DEFAULT NULL,
  `fk_id_paziente` int(11) DEFAULT NULL,
  `note` varchar(250) DEFAULT NULL,
  `data_prelievo` date DEFAULT NULL,
  `fk_id_infermiere` int(11) DEFAULT NULL,
  `ora_da` tinyint(4) DEFAULT NULL,
  `ora_a` tinyint(4) DEFAULT NULL,
  PRIMARY KEY (`id_prenotazione`),
  UNIQUE KEY `id_prenotazione_UNIQUE` (`id_prenotazione`),
  KEY `fk_id_puntoprelievo` (`fk_id_puntoprelievo`),
  KEY `fk_id_infermiere` (`fk_id_infermiere`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1;

 

CREATE TABLE `anag_puntiprelievo` (
  `id_pp` int(11) NOT NULL AUTO_INCREMENT,
  `descrizionepp` varchar(255) DEFAULT NULL,
  `attivo` tinyint(4) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id_pp`),
  UNIQUE KEY `id_pp_UNIQUE` (`id_pp`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
 

CREATE TABLE `anag_infermieri` (
  `id_infermiere` int(11) NOT NULL AUTO_INCREMENT,
  `descr_infermiere` varchar(45) DEFAULT NULL,
  `riferimento` varchar(45) DEFAULT NULL,
  `fk_id_pp` int(11) NOT NULL DEFAULT '1',
  `disponibilita` int(11) NOT NULL DEFAULT '8',
  PRIMARY KEY (`id_infermiere`),
  UNIQUE KEY `id_infermieri_UNIQUE` (`id_infermiere`),
  KEY `fk_id_pp` (`fk_id_pp`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;


Gustavo Teles
  • profile picture
  • Member

Posted 25 November 2014 - 17:14 PM

Hello friends would like to know if you have a new technique to use ajax in the dropdown.
I've tried these approaches:
/topic/1087-updated-24112012-dependent-dropdown-library/

/topic/1561-simple-dependant-dropdown-extension/

But neither worked.

I wonder if some already developed something simpler and more productive to work with ajax in Grud grocery.


Laasya
  • profile picture
  • Member

Posted 26 November 2014 - 05:35 AM

Hi victor

I used your instructions but i did not get result can you please help me

 

public function cities()
 
    {
        $this->check_login();
        $crud = new grocery_CRUD(); 
        $crud->set_subject('City');   
        $crud->set_table('cities');
        $crud->set_theme('twitter-bootstrap');
        $crud->fields('country_id','state_id','city','status');
$crud->display_as('city','City');
$crud->display_as('country_id','Country');
$crud->display_as('state_id','State');
    $crud->set_relation('country_id','country','country');
$crud->set_relation('state_id','states','statename');
$this->load->library('gc_dependent_select');
 
$fields = array(
 
// first field:
'Country' => array( // first dropdown name
'table_name' => 'country', // table of country
'title' => 'country', // country title
'relate' => null // the first dropdown hasn't a relation
),
// second field
'State' => array( // second dropdown name
'table_name' => 'states', // table of state
'title' => 'statename', // state title
'id_field' => 'state_id', // table of state: primary key
'relate' => 'country_id', // table of state:
'data-placeholder' => 'select state' //dropdown's data-placeholder:
 
)
);
 
 
$config = array(
'main_table' => 'cities',
'main_table_primary' => 'city_id',
"url" => base_url() . __CLASS__ . '/' . __FUNCTION__ . '/',// path to method
'ajax_loader' => base_url() . 'ajax-loader.gif' // path to ajax-loader image. It's an optional parameter
//'segment_name' =>'Your_segment_name' // It's an optional parameter. by default "get_items"
);
 
$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->load->view('admin_header',$output);
        $this->load->view('admin_crud',$output);
 
    }

 

 

I am struck at the URL i think. please let me clearly at the 

"url" => base_url() . __CLASS__ . '/' . __FUNCTION__ . '/',// path to method

 

 My Class is Admin and Fucntion name is ciites