+ Start a Discussion
DeepikareddyDeepikareddy 

Apex input checkbox selection

public class class2 {

 public list<string> lststatename{get;set;}
       
       public list<string> lstrating{get;set;}
       
        public map<string, string>statemap{get;set;} 
        
        
          public list<string>selectedvalues{get;set;}

 public  class2(){
 
 
 
 
 
      string  jsonexample1 =  ' { "data": [ {"stateName": "Andrapradesh",  "value": "apx" }, { "stateName": "Telangana",  "value":"tsx"}, {"stateName": "Banglore",  "value": "bngx"} , {"stateName": "Maharastra",  "value":"mhx" }  ] } ';
       

      map<string,object>  metadatamap= (map<string,object>)json.deserializeuntyped(jsonexample1); 

    list<object>  values1= (list<object>)metadatamap.get('data');  
    
     lststatename= new list<string>();
    
    lstrating= new list<string>();
    
         statemap= new map<String, string>(); 
        
        
          for(object parsed : values1){
          
             map<string,object>  values = (map<string,object>)parsed;
          
          string statename = string.valueof(values.get('stateName'));
          
          
          string value = string.valueof(values.get('value'));
          
          
          lststatename.add(statename);
          
          
       
          
          
           statemap.put(statename, value);
}


 }
 
 
 
   public PageReference test(){
   
  
   
   //code to be written 
   
   
   
    return null;
   
   
   
   }
}
 
<apex:page controller="class2">
  
  
  <apex:form >
  
  <ul style="list-style-type: none;">
 
  <apex:repeat value="{!lststatename}" var="a">
      <li ><apex:inputCheckbox value="{!statemap[a]}" /><label>{!a}</label></li>
        </apex:repeat>
        </ul>
  
  
   <apex:commandButton action="{!test}" value="Test" reRender="fm"/>
   
   
    <apex:outputLabel id="fm">
    
      the selected values are :
    </apex:outputLabel>
  </apex:form>
  
  
</apex:page>

User-added image

onclicking on the test button , the values are to be displayed, how to achieve this functionality ?

Thanks in advance
Best Answer chosen by Deepikareddy
shaik murthujavalishaik murthujavali
Hi DEEPIKA,
Please follow the below code.
public class class2 {
    public list<string> lststatename{get;set;}
    public list<string> lstrating{get;set;}
    public map<string, string> statemap {get;set;} 
    public list<string>selectedvalues{get;set;}
    public list<wrapperclass> listwrap {get;set;}
    public  class2(){
        string  jsonexample1 =  ' { "data": [ {"stateName": "Andrapradesh",  "value": "apx" }, { "stateName": "Telangana",  "value":"tsx"}, {"stateName": "Banglore",  "value": "bngx"} , {"stateName": "Maharastra",  "value":"mhx" }  ] } ';
        listwrap = new list<wrapperclass>();
        map<string,object>  metadatamap= (map<string,object>)json.deserializeuntyped(jsonexample1); +
            system.debug(metadatamap.keyset());
        system.debug(metadatamap.values());
        list<object>  values1= (list<object>)metadatamap.get('data');
        lststatename= new list<string>();
        lstrating= new list<string>();
        statemap= new map<String, string>(); 
        
        for(object parsed : values1){
            map<string,object>  values = (map<string,object>)parsed;
            string statename = string.valueof(values.get('stateName'));
            string value = string.valueof(values.get('value'));
            lststatename.add(statename);
            statemap.put(statename, value);
        }
        
        for(String s :statemap.keyset()){
            wrapperclass w = new wrapperclass();
            w.selected = false;
            w.statevalue = s;
            listwrap.add(w);
        }
    }
    public void test(){
        lstrating = new list<string>();
        for(wrapperclass wrap: listwrap){
            if(wrap.selected == true){
                lstrating.add(statemap.get(wrap.statevalue));
            }
        }
    }

public class wrapperclass{
    public boolean selected {get;set;}
    public string statevalue {get;set;}
}
}
 
<apex:page controller="class2">
  
  
  <apex:form >
  
  <ul style="list-style-type: none;">
 
  <apex:repeat value="{!listwrap}" var="a">
      <li ><apex:inputCheckbox value="{!a.selected}" /><label>{!a.statevalue}</label></li>
        </apex:repeat>
        </ul>
  
  
   <apex:commandButton action="{!test}" value="Test" reRender="fm" />
   
   
    <apex:outputLabel id="fm">
    
      the selected values are :
        <apex:repeat value="{!lstrating}" var="a">
            {!a}<br/>
        </apex:repeat>
    </apex:outputLabel>
  </apex:form>
  
  
</apex:page>

Hope it helps for you.
If it works fine please mark as a best answers so that it helps others too.

Thanks,
Shaik Murthujavali

All Answers

Rounak SharmaRounak Sharma
hi dipeeka,

I think you have to go with wrapper class.For the check box functionality you have to write the wrapper class and in that just query the data and return it. call that wrapper class in your test method.
Please let me if it helps
thanks
shaik murthujavalishaik murthujavali
Hi DEEPIKA,
Please follow the below code.
public class class2 {
    public list<string> lststatename{get;set;}
    public list<string> lstrating{get;set;}
    public map<string, string> statemap {get;set;} 
    public list<string>selectedvalues{get;set;}
    public list<wrapperclass> listwrap {get;set;}
    public  class2(){
        string  jsonexample1 =  ' { "data": [ {"stateName": "Andrapradesh",  "value": "apx" }, { "stateName": "Telangana",  "value":"tsx"}, {"stateName": "Banglore",  "value": "bngx"} , {"stateName": "Maharastra",  "value":"mhx" }  ] } ';
        listwrap = new list<wrapperclass>();
        map<string,object>  metadatamap= (map<string,object>)json.deserializeuntyped(jsonexample1); +
            system.debug(metadatamap.keyset());
        system.debug(metadatamap.values());
        list<object>  values1= (list<object>)metadatamap.get('data');
        lststatename= new list<string>();
        lstrating= new list<string>();
        statemap= new map<String, string>(); 
        
        for(object parsed : values1){
            map<string,object>  values = (map<string,object>)parsed;
            string statename = string.valueof(values.get('stateName'));
            string value = string.valueof(values.get('value'));
            lststatename.add(statename);
            statemap.put(statename, value);
        }
        
        for(String s :statemap.keyset()){
            wrapperclass w = new wrapperclass();
            w.selected = false;
            w.statevalue = s;
            listwrap.add(w);
        }
    }
    public void test(){
        lstrating = new list<string>();
        for(wrapperclass wrap: listwrap){
            if(wrap.selected == true){
                lstrating.add(statemap.get(wrap.statevalue));
            }
        }
    }

public class wrapperclass{
    public boolean selected {get;set;}
    public string statevalue {get;set;}
}
}
 
<apex:page controller="class2">
  
  
  <apex:form >
  
  <ul style="list-style-type: none;">
 
  <apex:repeat value="{!listwrap}" var="a">
      <li ><apex:inputCheckbox value="{!a.selected}" /><label>{!a.statevalue}</label></li>
        </apex:repeat>
        </ul>
  
  
   <apex:commandButton action="{!test}" value="Test" reRender="fm" />
   
   
    <apex:outputLabel id="fm">
    
      the selected values are :
        <apex:repeat value="{!lstrating}" var="a">
            {!a}<br/>
        </apex:repeat>
    </apex:outputLabel>
  </apex:form>
  
  
</apex:page>

Hope it helps for you.
If it works fine please mark as a best answers so that it helps others too.

Thanks,
Shaik Murthujavali
This was selected as the best answer
DeepikareddyDeepikareddy
hi , thank you , it was solved .. 

thanks 
deepika