Jump to content


How to add radiobutton/radiobutton with custom value

radiobutton dropdown

  • Please log in to reply
12 replies to this topic

#1 demisjs

demisjs

    Newbie

  • Members
  • Pip
  • 1 posts

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

#2 casachit

casachit

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts

Posted 29 May 2012 - 12:52 PM

whenever you set enum 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 callback_add_field 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';
}

Attached Thumbnails

  • drop.JPG


#3 casachit

casachit

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts

Posted 29 May 2012 - 01: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 callback_field function

#4 kenvogt

kenvogt

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts

Posted 29 May 2012 - 04:34 PM

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

#5 kenvogt

kenvogt

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts

Posted 29 May 2012 - 04: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.

#6 kenvogt

kenvogt

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts

Posted 29 May 2012 - 05: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.

#7 rikoy

rikoy

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 28 June 2012 - 05:59 PM

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

#8 weezerwes

weezerwes

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 18 March 2013 - 08: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');


#9 yurispy

yurispy

    Newbie

  • Members
  • Pip
  • 5 posts

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

 



#10 Arjunraj

Arjunraj

    Member

  • Members
  • PipPip
  • 10 posts

Posted 28 August 2017 - 01: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'; 
                     
    }

 



#11 Arjunraj

Arjunraj

    Member

  • Members
  • PipPip
  • 10 posts

Posted 28 August 2017 - 03: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..
 

 



#12 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

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.



#13 Arjunraj

Arjunraj

    Member

  • Members
  • PipPip
  • 10 posts

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. 







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users