+ Start a Discussion
Puja ChoudharyPuja Choudhary 

Not able to uncheck a checkbox ,Once it is checked

Hi,
I have a apex checkbox field in the vf page called "ONLY VEG". When I first click on the check box it selects only veg item from menu object and shows in the vf page. But if i want to uncheck it again so thai it will again show all the veg and non veg items,,,,,But I am not able to uncheck it again.
Checkbox in vf page:
<apex:inputCheckbox value="{!myCheckbox}"> <apex:actionSupport event="onchange" action="{!onlyveg}" /> </apex:inputCheckbox>
Controller:
 public boolean myCheckbox {get; set;}

  public PageReference onlyveg(){
                if(myCheckbox=TRUE){
                system.debug('%%%%%%%%%%%%%%%'+myCheckbox);
                menu = [SELECT Category__c,CreatedById,Total_Price__c,Chef_recommended__c, CreatedDate, IsDeleted, Description__c, Image__c, ImageURL__c, LastActivityDate, LastModifiedById, LastModifiedDate, Name, Dish_Name__c, OwnerId,Price__c, Id, Remarks__c,Kids_Menu__c, Special_Dish__c,Spicy__c, Status__c, SubCategory__c, SystemModstamp, veg_non_veg__c,Information__c,Carbohydrate__c,Fats__c,Iron__c,Protein__c,Vitamin__c, (SELECT Menu__c,Price__c,Topping__c,Name FROM Toppings__r) FROM Menu__c where veg_non_veg__c='Veg'];
                }else{
                system.debug('%%%%%%%%%%%%%%%'+myCheckbox);
                menu = [SELECT Category__c,CreatedById,Total_Price__c,Chef_recommended__c, CreatedDate, IsDeleted, Description__c, Image__c, ImageURL__c, LastActivityDate, LastModifiedById, LastModifiedDate, Name, Dish_Name__c, OwnerId,Price__c, Id, Remarks__c,Kids_Menu__c, Special_Dish__c,Spicy__c, Status__c, SubCategory__c, SystemModstamp, veg_non_veg__c,Information__c,Carbohydrate__c,Fats__c,Iron__c,Protein__c,Vitamin__c, (SELECT Menu__c,Price__c,Topping__c,Name FROM Toppings__r) FROM Menu__c where veg_non_veg__c='Non-veg'];
                }
                
                return null;
         
    }
Best Answer chosen by Puja Choudhary
Pradeep SinghPradeep Singh
  1. In IF condition use "==" instead of "=" OR simply use IF(myCheckbox){-----}
  2. In Else i.e when the checkbox is not checked, you are quering records having veg_non_veg__c = 'Non-veg'. To show all veg and non-veg items remove where clause from the query.

All Answers

Pradeep SinghPradeep Singh
  1. In IF condition use "==" instead of "=" OR simply use IF(myCheckbox){-----}
  2. In Else i.e when the checkbox is not checked, you are quering records having veg_non_veg__c = 'Non-veg'. To show all veg and non-veg items remove where clause from the query.
This was selected as the best answer
Puja ChoudharyPuja Choudhary
Thank you Pradeep  :D