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 [attachment=511:ajax_grocery_crud.php] to the application\libraries folder), we would be writting the following php code:
function addresses_management() { $this->load->library('grocery_CRUD'); $this->load->library('ajax_grocery_CRUD'); //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 $crud->set_table('address'); $crud->set_relation('ad_country_id','country','c_name'); $crud->set_relation('ad_state_id','state','s_name'); //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 $crud->set_relation_dependency('ad_state_id','ad_country_id','s_country_id'); $output = $crud->render(); $this->_example_output($output); }
The SQL table definition for this example would be:
CREATE TABLE country ( c_id INT PRIMARY KEY, c_name VARCHAR(50) ); CREATE TABLE state ( 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:
function highlights_management() { $crud = new ajax_grocery_CRUD(); $crud->set_table('highlight'); $crud->columns('date','performance_id'); $crud->set_relation('performance_id','performance','name'); $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 $crud->set_relation_dependency('performance_id','date','eventDate'); $output = $crud->render(); $this->_example_output($output); }
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!