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.
Very Nice work!
So how to to if you like to have multiple independent dependent Dropdowns?
For example if you have do save city a, street a and city b, street b…
You can go down dependencies as many as you like. In my initial example I have 3, but if you have 5 or even 10 it can handle that.
Thanks for the information, but I would like to have a dependent Dropdown pair A and a dependent Dropdown pair B in one form.
Ahhh ok – let me ammend the tutorial for you, will have to write something up for you, easy to ammend
Ok, thx in advance, do you have to add this to your code or do you still have to ammend your tutorial?
Lovely Work kabadabra,
Pls how soon will the functionality that mike requested be available….I’m almost stuck on that?
mike, got it figured out….all u need is some tweaking around the dependent_dropdown view and a minor change on the way you call it using multidimensional arrays