25
Mar

Grocery CRUD – Dependent Dropdowns

Want to use categories with subcategories in Grocery CRUD? Or a vehicle list with models? Or dropdowns with Country, State and City? Below I have made an example using Grocery CRUD. Download the example and files here.

Step 1: Add the dependent dropdown data to your controller

Below are the basic setup details for your dependent dropdown. Just add it right below the “$output = $crud->render();” in your controller’s function.

  • dd_state = Gets the current CRUD state. E.g list, add etc.
  • dd_dropdowns = Set your dependent field variables here. In my example I used ‘countryID’,'stateID’ and ‘cityID’. The parent dropdown should always go first and its children should follow in order that they will be created.
  • dd_url = The URL to the function that gets and creates the child dropdown data. The parent URL always stays blank. They need to be in order as per the ‘dd_dropdowns’.
//DEPENDENT DROPDOWN SETUP
$dd_data = array(
    //GET THE STATE OF THE CURRENT PAGE - E.G LIST | ADD
    'dd_state' =>  $crud->getState(),
    //SETUP YOUR DROPDOWNS
    //Parent field item always listed first in array, in this case countryID
    //Child field items need to follow in order, e.g stateID then cityID
    'dd_dropdowns' => array('countryID','stateID','cityID'),
    //SETUP URL POST FOR EACH CHILD
    //List in order as per above
    'dd_url' => array('', site_url().'/examples/get_states/', site_url().'/examples/get_cities/'),
    //LOADER THAT GETS DISPLAYED NEXT TO THE PARENT DROPDOWN WHILE THE CHILD LOADS
    'dd_ajax_loader' => base_url().'ajax-loader.gif'
);
$output->dropdown_setup = $dd_data;

 

Step 2: Copy the ‘dependent_dropdown.php’ to your views folder.

The ‘dependent_dropdown.php’ file containts all the PHP code that will dynamically create the dropdown file for you.

 

Step 3: Load the view in the view/display file.

Load the following PHP code in the view (at the bottom), the same one that gets used by GCRUD to create the output, css and js files.

if(isset($dropdown_setup)) {
	$this->load->view('dependent_dropdown', $dropdown_setup);
}

 

That’s it! Now you can successfully create dependent dropdown files.

View a LIVE demo here.

Download this example here




© Copyright 2010-2013 KaBaDaBrA. All rights reserved. Created by Dream-Theme — premium wordpress themes. Proudly powered by WordPress.

Switch to our mobile site