Jump to content

Simple dependant dropdown extension

dropdown select dependancy relation date filetring ajax

  • Please log in to reply
86 replies to this topic

#81 sivakumar



  • Members
  • Pip
  • 1 posts

Posted 09 February 2018 - 12:20 PM

Hi all,


First of all thanks the GC guys for this magnific piece of software. I love it's simplicity and neatness.


I'd like to share with you a short extension I built to allow dependant dropboxes to be declared easily and in the same fashion that we declare the grocery CRUD model:


Dependant drop downs:


Say we have an address table that has both country and state fields and we want to enforce that the state dropdown only shows states from the selected country.


Using the ajax_grocery_CRUD.php library (copy the attached file attachicon.gifajax_grocery_crud.php to the application\libraries folder), we would be writting the following php code:


function addresses_management()

//create ajax_grocery_CRUD instead of grocery_CRUD. This extends the functionality with the set_relation_dependency method keeping all original functionality as well
            $crud = new ajax_grocery_CRUD();

//this is the default grocery CRUD model declaration

//this is the specific line that specifies the relation.
// 'ad_state_id' is the field (drop down) that depends on the field 'ad_country_id' (also drop down).
// 's_country_id' is the foreign key field on the state table that specifies state's country

            $output = $crud->render();


The SQL table definition for this example would be:

CREATE TABLE country ( 
     c_id INT PRIMARY KEY,
     c_name VARCHAR(50)

     s_id INT PRIMARY KEY,
     s_name VARCHAR(50),
     s_country_id INT,
     FOREIGN KEY (`s_country_id`) REFERENCES `country` (`c_id`)

CREATE TABLE address ( 
     ad_id INT PRIMARY KEY,
     ad_country_id INT,
     ad_state_id INT,
     FOREIGN KEY (`ad_country_id`) REFERENCES `country` (`c_id`),
     FOREIGN KEY (`ad_state_id`) REFERENCES `state` (`s_id`)

Dependant date filtering:


The source filed that provides the value to filter the dependent field can also be a date. I use it for of an agenda where I have a large set of 'performances' (musical shows) which I want to select on another table ('highlights') based on an input date. 
function highlights_management()
	$crud = new ajax_grocery_CRUD();



	$crud->set_relation('image_id','image','name','category_id IN (2,3,4)');

//'eventDate' is the field on the performance table that indicates the date of the performance
// The drop down will list only the performances that happen on the selected date

	$output = $crud->render();



Table highlights has both date (date) and performance_id (foreign key for performance table) fields.

Table performance table has a date field that is used to filter the performances to show on the dropdown.



I hope this may be useful for anyone else.

Keep up the good work GC people!


Hi ricardomduarte,


There is a clarification, the second dependent dropdown is not refreshed when the first dropdown is changed.... any idea? 


#82 venkatesh



  • Members
  • Pip
  • 3 posts

Posted 30 March 2018 - 03:54 AM

well my friend - u have every where clause possible in set_relation.. now why all of a sudden u need a where clause in set_relation_dependancy?

I am in the same situation .In set relation the where condition works fine,but it doesnt retrieve the same records based on where condition in set relation for set relation dependency. 

This is my code


$crud->set_relation('CONTRY_CODE','contry','CONTRY_NAME',array('CONTRY_STATUS' => 'E'));
 $crud->set_relation('STATE_CODE','state','STATE_NAME',array('STATE_STATUS' => 'E'));
 $crud->set_relation('CITY_CODE','city','CITY_NAME',array('CITY_STATUS' => 'E'));
 $crud->set_relation_dependency('STATE_CODE','CONTRY_CODE','CONTRY_CODES',array('STATE_STATUS' => 'E'));
when i click on state code it returns only the states for which status are enabled.But when I select country,then state(due to dependency) it retrieves all the state records (even disabled status records).
Can anyone please suggest any possible solution??

#83 avi



  • Members
  • Pip
  • 2 posts

Posted 11 April 2018 - 03:29 AM


Well this if i am not mistakened - this one you talking about i edit and not add. I do understand the situation - i faced it mysef. Even for add - it just shows all the entries at the first instance.. This has been a drawback of the plugin but fortunately i found a way around for the same.


i have had the same scenario for add / edit drop down dependency for country / state / city


i am sharing u my piece of code - it might help u understand a solution for the same

$crud->set_relation('country', 'countries', 'country_name');				
		$segments = $this->uri->segments;
		$segmentFound = false;
		foreach($segments as $num => $value) {
			if($value == 'ajax_extension') {
				$segmentFound = true;
		if($segmentFound) {
			$crud->set_relation('city', 'cities', 'city_name');
			$crud->set_relation('state', 'states', 'state_name');
		} else {
			if($crud->getState() == 'edit') {
				//$pk = $crud->getStateInfo()->primary_key;
				//$row = $this->cModel->getByField('retailers', 'rid', $pk);
				if($curRow['country'] != '') {
					$allstates = $this->cModel->getAllFor('states', 'country_code', $curRow['country']);
					$states = array('1'=>'select state');
					foreach ($allstates as $state) {
						$states[$state['state_code']] = $state['state_name'];
				$state_code = $curRow['state'];
				if($state_code != '') {
					$allcities = $this->cModel->getAllFor('cities', 'state_code', $state_code);
					$cities = array('1'=>'select city');
					foreach ($allcities as $city) {
						$cities[$city['city_code']] = $city['city_name'];
				} else {
					$cities = array('1'=>'select city');	
				if($curRow['country'] == '') {
					$cities = array('1'=>'select state');
			} else {
				$cities = array('1'=>'select city');
				$states = array('1'=>'select state');
			$crud->field_type('city', 'dropdown', $cities);
			$crud->field_type('state', 'dropdown', $states);

Dear Amit,


I hope you are well :)


I'm experiencing the same problem you had years ago, i'm trying to understand your solution here.. can you possibly post the complete code for your solution above? Where do you implement the set_relation_dependency part?


The library is loading the whole list of complete dropdown items when adding new items so it become so slow. and also at edit situation, it also loads the whole list..

#84 analuthfa



  • Members
  • Pip
  • 1 posts

Posted 02 May 2018 - 01:03 AM

Hi all, i'm new in GC. i have used library ajax_grocery_crud for dependant dropdown, but the second dropdown not filtering value based on first selected dropdown value.

Here is my code


public function surat()


$template = $this->Surat_model->get_template();


        $crud = new ajax_grocery_CRUD();
        $crud->display_as('jenis_surat_id','Jenis Surat');
        $crud->set_relation_n_n('penerima', 'detil_penerima', 'pegawai', 'id_surat', 'nipp', 'employee_name');
        $crud->set_relation_n_n('tembusan', 'detil_tembusan', 'pegawai', 'id_surat', 'nipp', 'employee_name');
        $crud->set_relation_n_n('pemeriksa', 'detil_pemeriksa', 'pegawai', 'id_surat', 'nipp', 'employee_name');
            $output = $crud->render();
            $output->extra = $template;



thanks in advance

#85 kingwash



  • Members
  • Pip
  • 3 posts

Posted 23 January 2019 - 10:57 AM

Thank you so much Mr ricardomduarte for you awesome library.

I tried to use your library with Grocory CURD 1.4.0 but I got this error message

Fatal error: Access level to ajax_grocery_CRUD::$state_code must be protected (as in class Grocery_CRUD) or weaker in C:\wamp\www\cpuwebsite\application\libraries\ajax_grocery_crud.php on line 255
Go "application\libraries\ajax_grocery_crud.php" open the file and change "private $state_code  = null;"   to  protected $state_code  = null;

please any idea about it, let me know i will be waiting

#86 kingwash



  • Members
  • Pip
  • 3 posts

Posted 23 January 2019 - 10:58 AM

Fatal error: Access level to ajax_grocery_CRUD::$state_code must be protected (as in class Grocery_CRUD) or weaker in C:\wamp\www\cpuwebsite\application\libraries\ajax_grocery_crud.php on line 255





Go to "application\libraries\ajax_grocery_crud.php" open the file and change "private $state_code = null;" to protected $state_code = null;

#87 Ralph



  • Members
  • Pip
  • 1 posts

Posted 07 August 2020 - 03:09 PM

Hi im a newbie to grocery crud. I need the functionality of ajax_grocery_crud. I took php file updated by Amit Shah and made that trigger('chosen:updated') to  trigger("liszt:updated") on function 'render_relation_dependency' in  ajax_grocery_crud.php modification. It does not act as expected, the dependant selector is not restricted to the selection from the independant dropdown. It does not do any harm anyway, just as it would not be there. The grocery_CRUD library is loaded in the constructor. The language of GC is still english. Here is my code:


    public function component_parts() {
        $this->session->header = 'Tabelle: Bauteile der Komponente';  // German language
            $crud = new ajax_grocery_CRUD();

            $output = $crud->render();


        }catch(Exception $e){
            show_error($e->getMessage().' --- '.$e->getTraceAsString());

My concerns are: My naming strategy for db table attributes is to use the name of the primary key beeing the same in foreign keys in other tables. Is this not applicable to grocery crud and causing the problem? Also, i set up the tables with phpMyAdmin and its seems not possible to declare a foreign key in sql, so that is missing in MariaDB table setup. Can this cause the fault?

Last not least: I thing, ajax_grocery_crud is so important to GC, you should consider to integrate it into GC and maintain it. As far as i can see, it only adds one function to GC dokumentation. My GC version is 1.6.3 and CI is 3.1.10

Also tagged with one or more of these keywords: dropdown, select, dependancy, relation, date filetring, ajax

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users