PROBLEM
There are times when the users want to have control in the add / edit form. For example, there is a need to autoupdate a few fields based on the value set by user on one field. Such a scenario is a custom requirement rather then generic requirement. Hence there is no provision for the same.
SOLUTION
There is a simple solution for the same. One need to control the same with custom javascript. Lets figure out the steps / process involved in the same.
- Create a new javascript file. The same shall consist of a function that will trigger a capture of on blur event / on change event (whatever is required .. suitable for the situation).
- The same then can have the relevant piece of code that will make the changes elsewhere.
- Remember 1 thing .. this is very important point, we will not be able to use $.ready, instead use the following
window.onload = function(){ // Code. . . }
- The above is very important step and the simplest of reason for the same is, while this file gets loaded, the script yet is not loaded. The thing you need to do is tell crud to load this custom script. The same can be done using $crud->set_js() function.
The above is the mechanism you can attain control over add / edit / listing, anywhere as required.
A more prominent requirement / criteria that one may encounter like the query / requirement shared by the user in following link: https://www.grocerycrud.com/forums/topic/4106-showhide-field-from-dropdown/
Here the user wants to take control of the field to be shown / hidden when a value from dropdown is changed. For such scenario, as explained above, we need to hook an event in javascript to the dropdown. On change of the value in dropdown – we take a decision to either hide a field or show a field.
Every field in the GC add / edit form is encompassed in a div. Each such div is having a class attached which consist of <fieldname>_form_group. Using this, we can take control of the field container and show / hide – as required.
Happy GCing 🙂