⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

How to add radiobutton/radiobutton with custom value



demisjs
  • profile picture
  • Member

Posted 29 May 2012 - 11:21 AM

Hi,

I am very much impressed with your tool.

I am yet to see some examples using radiobutton or dropdown boxes which are missing. Is there any tutorial for that?

Say for example.. I have a gender column in database table which expects values Male | Female (values are custom)
How do you implement this?

Also if I want to display it as a dropdownbox also.. Pls provide me sample codes for radiobutton and dropdown box.

Thanks,
Demis John

casachit
  • profile picture
  • Member

Posted 29 May 2012 - 12:52 PM

whenever you set [b]enum[/b] value in database table.. you will get drop down option as shown in image attached....

i dunno about radio button if it can be directly render like that of dropdown for enum data types... but you can use [b]callback_add_field[/b] function to add radio button.. i hope it may work for you


function example_callback_add_field(){
$crud = new grocery_CRUD();
$crud->set_table('offices');
$crud->set_subject('Office');
$crud->required_fields('city');
$crud->columns('city','country','gender','addressLine1','postalCode');

$crud->callback_add_field('gender',array($this,'add_field_callback_1'));


$output = $crud->render();

$this->_example_output($output);
}

function add_field_callback_1()
{
return ' <input type="radio" name="sex" value="male" /> Male
<input type="radio" name="sex" value="female" /> Female';
}

casachit
  • profile picture
  • Member

Posted 29 May 2012 - 13:03 PM

if you want drop down button then you can do similar as done for radio button in above code.... if you want radio button and drop down button in both add and edit fields like that of above... you can use[b] callback_field [/b]function

kenvogt
  • profile picture
  • Member

Posted 29 May 2012 - 16:34 PM

Very nice, this information would be a great addition to the Examples page of the website.

kenvogt
  • profile picture
  • Member

Posted 29 May 2012 - 16:40 PM

BTW, there is are some examples of sorts if you look at the api doc for callback_edit_field or callback_add_field. The thing is, someone looking for radio buttons or checkboxes may not realize that this is the place to look.

kenvogt
  • profile picture
  • Member

Posted 29 May 2012 - 17:28 PM

While this works well for radio buttons, checkboxes work a little differently. The value of a checkbox is fixed. The GC code has to determine if it is checked, not its value. I don't see anything in the GC code that knows how to process a checkbox.

Another simple possibility for radio buttons is to use:

$crud->change_field_type('your_field','true_false');

The only problem here is you are stuck with the default button labels of "active" and "inactive" and values of 1 and 0, respectively.

rikoy
  • profile picture
  • Member

Posted 28 June 2012 - 17:59 PM

thanks, it work in add form, so how if edit form?

weezerwes
  • profile picture
  • Member

Posted 18 March 2013 - 20:04 PM

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');

yurispy
  • profile picture
  • Member

Posted 17 September 2013 - 11:43 AM

Hello
I have wanted to know how to make the checkbox is the value of the db. Otherwise, when the checkbox is edict are always equal to 0 even if the value is 1

 


Arjunraj
  • profile picture
  • Member

Posted 28 August 2017 - 13:16 PM

Hi I am new to grocery CRUD , I surfed on internet to find the solution for this , but I found none,

Myself found one solution for radio button edit , Hope this helps 

 

           $crud->change_field_type('government','true_false');

 

           // This part is for inserting,  'government' is the name of the field or column name in the database table.

            $crud->callback_add_field('government', function() {
       return '<input type="radio" name="government" value="Y" /> Yes  &nbsp;     // "Y" and  "N" are the enum values that I have given in database.
                 <input type="radio" name="government" value="N" /> No';
        });

    

       // 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 &nbsp;
               <input type="radio"  name="government" value="N" /> No '; } 
                 
      else  return '<input type="radio" name="government" value="Y" /> Yes &nbsp;
        <input type="radio" name="government" value=" '.$value.' " checked="checked" /> No'; 
                     
    }

 


Arjunraj
  • profile picture
  • Member

Posted 28 August 2017 - 15:14 PM

Hi I am new to grocery CRUD , I surfed on internet to find the solution for this , but I found none,
Myself found one solution for radio button edit , Hope this helps 
 
           $crud->change_field_type('government','true_false');
 
           // This part is for inserting,  'government' is the name of the field or column name in the database table.
            $crud->callback_add_field('government', function() {
       return '<input type="radio" name="government" value="Y" /> Yes  &nbsp;     // "Y" and  "N" are the enum values that I have given in database.
                 <input type="radio" name="government" value="N" /> No';
        });
 
       // 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 &nbsp;
               <input type="radio"  name="government" value="N" /> No '; } 
 
      else  return '<input type="radio" name="government" value="Y" /> Yes &nbsp;
        <input type="radio" name="government" value=" '.$value.' " checked="checked" /> No'; 
 
    }
The above code works for insertion and if we change that radio button from 'y' to 'n' or 'n' to 'y' in an edit functionality.
 
But The only problem is , when I press edit and if I did not change anything and press update changes , then there will be nothing stored in government column(i.e Y or N), can anyone help me with this..
 

 


Amit Shah
  • profile picture
  • Member

Posted 29 August 2017 - 04:48 AM

Inspect in developer tools (Chrome / firefox) .. check the value of the field that gets submitted back. Is it the value that goes as expected or what.. if not, then u need to re-work on the form generation (Field alteration) where your value is getting passed on.


Arjunraj
  • profile picture
  • Member

Posted 29 August 2017 - 06:57 AM

Thanks for the tip Mr. Amit shah , I found the solution to that problem,  there was  spaces between the double quotes (value=" '.$value.' ")  that is the reason , The value was passing &nbsp; (one space) instead of the $value(Y or N), Now its working all good .  I corrected the above code as value="'.$value.'" (There should be no space between the double quotes and single quote).

 

Thank you Once again.