Hey everyone,
I would like to share my extended library for GroceryCRUD, it has DependentDropdown(custom query) + PostAjaxCallbacks + MoreButtons + MoreUnsets + CustomColumn. This is based off the 1.6.1 version. Be aware some of the features are specifically tied to the flexigrid theme, but it can be adjusted to work for other themes as well.
Downloading and using:
Download the zip file and unzip it in some directory. Manually copy the files in the directory to your codeigniter directory as they are in the directory you unzipped. Some of the files have the same name as the original GroceryCRUD files, if you want to preserve the old file you can rename it, but for the library to work with all of its features the filenames have to replace the original files.
Setup: Load the library after you loaded the original GroceryCRUD library.
$this->load->library('grocery_CRUD'); $this->load->library('grocery_CRUD_mod');
Then to use in you controller:
function my_boss_is_in_a_hurry() { $crud = new grocery_CRUD_mod(); // this is the only difference $crud->set_table('customers'); $crud->columns('customerName','phone','addressLine1','creditLimit'); $output = $crud->render(); $this->_example_output($output); }
---------FEATURES---------
set_custom_column($column_array):
This will trigger the inner search of GroceryCRUD to perform HAVING clause instead of the WHERE clause on the columns listed in the array argument.
I used this for when generating custom query views and needed to search on specific rows, because the original WHERE did not work on those custom columns.
Example:
$crud->set_custom_column(array('col1', 'col3')); $crud->columns('col1', 'col2', 'col3');
The above example will perform a HAVING search whenever the search is done on col1 or col3, but a WHERE will still be performed on col2. When search all is done, a HAVING will be performed on all the columns.
unset_pagnation():
unsets the pagination view. Need to have the assets directory copied over. Only works for flexigrid theme.
Example:
function employees_delete_management() { $crud = new grocery_CRUD_mod(); $crud->set_theme('datatables'); $crud->set_table('employees'); $crud->set_relation('officeCode','offices','city'); $crud->display_as('officeCode','Office City'); $crud->set_subject('Employee'); $crud->unset_pagnation(); $output = $crud->render(); $this->_example_output($output); }
unset_search():
unsets the search components. Will not see them in view. Need to have the assets directory copied over. Only works for flexigrid theme.
Example:
function employees_delete_management() { $crud = new grocery_CRUD_mod(); $crud->set_theme('datatables'); $crud->set_table('employees'); $crud->set_relation('officeCode','offices','city'); $crud->display_as('officeCode','Office City'); $crud->set_subject('Employee'); $crud->unset_search(); $output = $crud->render(); $this->_example_output($output); }
post_ajax_callbacks():
calls your javascript function after a refresh on the table specified occurred. This is useful if need to perform an action right after a CRUD action was done, since the refresh is triggered after all of those actions. Need to have the assets directory copied over. Only works for flexigrid theme.
Example:
function employees_delete_management() { $crud = new grocery_CRUD_mod(); $crud->set_js("assets/my_js_function.js") $crud->post_ajax_callbacks(array("employees_delete_management"=>"js_func")); $crud->set_theme('datatables'); $crud->set_table('employees'); $crud->set_relation('officeCode','offices','city'); $crud->display_as('officeCode','Office City'); $crud->set_subject('Employee'); $output = $crud->render(); $this->_example_output($output); }
In the above example, the javscript js_func() will be called after a refresh on the employees_delete_management table was performed.
add_state(new_state, view_filename, final_url, validation_url, callback, type_of_render):
This adds a custom state inside the GroceryCRUD render framework. This is used in the custom form creation, a more detailed explanation and example is here.
It is also used to create the dependent dropdown. If needed to use this function, when putting the type_of_render argument you can have an immediate_layout. This is like the delete action in the original GroceryCRUD, when delete is pressed no form is popped up, just a statement is executed. I will show an example of the use of immediate layout, but the custom_form type of render is in the link above.
Example:
function offices_management_with_actions() { $crud = new grocery_CRUD_mod(); $crud->set_table('offices'); $crud->set_subject('Office'); $crud->required_fields('city'); $crud->columns('city','country','phone'); $crud->add_action('update_blah', '', '[name of controller class]/offices_management_with_actions/update_blah','ui-icon-plus custom-action'); // custom immediate action $crud->add_state('update_blah', '', '', '', function($state_info) { $error_msg = ''; $success_msg = 'Successfully updated blah.'; $flag = true; /* Perform database actions and/or other things */ return array('success' => $flag, 'error_msg' => $error_msg, 'success_msg' => $success_msg); }, 'immediate_layout'); $output = $crud->render(); $this->_example_output($output); }
In the above example when the action, update blah, is pressed it will be routed to the function placed in add_state at the callback argument. Also do not forget to put the name of the controller class in the route in add_action. Also you can have a custom success or error msg come out as in the original GroceryCRUD library. When success is false, the error_msg will be reported, when success is true the success_msg will be reported.
add_button(button_name, $link, [alignment, css_classes]):
This adds a button like the original add button in the framework, it is similar to add_action, but for placing a custom button on the top of the view. You can select whether to align the button left or right, and also put css classes on it. I usually use this will add_state, to route the action to a custom form.
Example:
function offices_management_with_actions() { $crud = new grocery_CRUD_mod(); $crud->set_table('offices'); $crud->set_subject('Office'); $crud->required_fields('city'); $crud->columns('city','country','phone'); $crud->add_button('add a blah', 'offices_management_with_actions/add_blah', 'left', 'ui-icon-plus custom-action'); $crud->add_state('add_blah', '', '', '[name of controller class]/offices_management_with_actions/add_blah', '', 'custom_form'); $output = $crud->render(); $this->_example_output($output); }
The above example will create a button left aligned on top of the table not in each row. This button is linked to the add_blah state, refer here for what to put in the add_state to make it work.
dependent_dropdown_field(field_name, dependent_field, query, [query_binding=array()]):
This will make the field_name depend on the dependent_field by the query specified and the query bindings. In the query binding the order is crucial and directly related to the question mark symbols placed in the query argument. The options for query binding are the dependent_field, other_field, and primary_key.
This means if the key is dependent_field then the value does not really matter in the argument since the value used in the query will be the value selected for the dependent_field. If the other_field is the key, then you need to specify from what field to substitute the value into the query. If primary_key is used in the key, then it will substitute the primary key value of the selected row, the value parameter in the array will not matter since you specified it is the primary_key. The output from the query MUST return an 'id' column and a 'name' column which will be put into the dropdown options.
Example:
function offices_management_with_dependent_dropdown() { $crud = new grocery_CRUD_mod(); $crud->set_table('offices'); $crud->set_subject('Office'); $crud->required_fields('city'); $crud->columns('city','country','phone'); // dependent dropdown $crud->dependent_dropdown_field('city', 'country', ' SELECT city_country.city_id AS id, cities.name AS name FROM city_country LEFT JOIN cities ON (cities.city_id = city_country.country_id) WHERE country_id = ? ', array('dependent_field' => 'dependent_field')); $output = $crud->render(); $this->_example_output($output); }
The above query will have city depend on country, and whenever country is changed the cities will be updated accordingly. If you need to nest that city is dependent on country and state, just say for example. You do not need to place two dependencies on city, just put a dependency that state is dependent on country and city is dependent on state. They will adjust accordingly. You can have the query use values from other fields as described above and also use the primary key value in the query if needed.
I hope this helps, let me know if you have any questions.