CodeIgniter + Grocery Crud - how to set fields to either on/off (bool, checkbox) and also how to set it to be a select dropdown of numbers (1-10)
-
14-07-2021 - |
Question
CodeIgniter + Grocery Crud - how to set fields to either on/off (bool, checkbox) and also how to set it to be a select dropdown of numbers (1-10) ?
I have grocerycrud set up, and on some fields they are either:
bool values (but set as ints or varchars so groceycrud shows it as a
or
select dropdown with values 0-10 (these are stored as ints, but it is for a rating system)
any ideas?
thanks
Solution
For this you can check field_type method. In your case you need:
field type true_false for on/off boolean. For example:
$crud->field_type('my_field','true_false');
The default on/off for grocery CRUD is active/inactive (1/0) but you can change the text from the lang file at: assets/grocery_crud/languages/english.php (or your language) to on off.
For the second scenario of dropdown list you can use the field_type with type "dropdown" or "enum". For more you can go to: http://www.grocerycrud.com/documentation/options_functions/field_type#dropdown-field and http://www.grocerycrud.com/documentation/options_functions/field_type#enum-field that also have examples.
Also consider that the dropdown type is available only for versions >= 1.3.2
OTHER TIPS
You can change the values of radio buttons in the config file for your language. for english, it's here:
assets\grocery_crud\languages\english.php
Just change these 2 lines:
$lang['form_inactive'] = 'inactive';
$lang['form_active'] = 'active';
To whatever you want, in your case, something like:
$lang['form_inactive'] = 'female';
$lang['form_active'] = 'male';
and make sure to change the field to boolean, if you haven't yet:
$this->grocery_crud->change_field_type('field_name','true_false');
// now This part is for update or edit
$crud->callback_edit_field('government',array($this,'radio_edit_callback'));
//Function for the callback_edit_field
public function radio_edit_callback($value) {
//$value will be having the present value(Y or N) that is in the list or database.
if($value == 'Y') {
return '<input type="radio" name="government" value=" '.$value.' " checked="checked" /> Yes
<input type="radio" name="government" value="N" /> No ';
} else {
return '<input type="radio" name="government" value="Y" /> Yes
<input type="radio" name="government" value=" '.$value.' " checked="checked" /> No';
}
}