+ Start a Discussion
Bijay SahuBijay Sahu 

onClick is not firing for apex:inputCheckbox to call apex class method

Hi All,
I have a dynamic table and reach row binding with inputCheckbox. onClick i am calling an Apex class methos but method is not calling. Could anyone please help and Thanking you in advance.

My Apex Page
<apex:page controller="DynamicCheckBox_Ctrl">
  <apex:form >
      <apex:pageBlock >
          <apex:pageBlockSection columns="1">
              <apex:inputText value="{!firstValue}" label="Enter Field Name"/>
              <apex:inputText value="{!secondValue}" label="Enter another Filed Name"/>
          </apex:pageBlockSection>
          <apex:pageBlockButtons location="top">
              <apex:commandButton value="Get Data" action="{!showValues}"/>
              <apex:commandButton value="Delete" onclick="if (!confirmDelete()) return false;" action="{!deleteAccount}"  />
          </apex:pageBlockButtons>
          <apex:outputPanel rendered="{!hideTable}">
              <apex:pageblockTable value="{!accList}" var="acct" >
                  <apex:column >
                    <apex:inputCheckbox onclick="storeAccoutIds({acct.id},selected)" />
                  </apex:column>
                  <apex:repeat value="{!lstFields}" var="filedName">
                      <apex:column value="{!acct[filedName]}"/>
                  </apex:repeat>
              </apex:pageblockTable>
          </apex:outputPanel>
      </apex:pageBlock>
  </apex:form>
  <script>
      function confirmDelete()
      {
        return confirm(‘Are you sure you want to delete?’);
      }
  </script>
</apex:page>

Apex Class

public class DynamicCheckBox_Ctrl {
    
    public string prepareQuery{get;set;}
    public string firstValue{get;set;}
    public string secondValue{get;set;}
    public boolean hideTable{get;set;}
    public boolean enableDelButton{get;set;}
    
    public list<Account> accList{get;set;}
    public List<String> lstFields{get;set;}
    
    //store account id, if checkbox is checkedselectedcheckbox
    public Map<String,Boolean> mapAccountId = new Map<String,Boolean>();

         
    public void storeAccoutIds(string accId, boolean isSelected)
    {
        if(isSelected)
        {
            mapAccountId.put(accId,isSelected);
            enableDelButton = true;
        }   
        
        system.debug(mapAccountId);
    }
    
       
    //Get records from datanase and send to apex page
    public void showValues()
    {
        try{

            //Add all the fileds into a list
            lstFields=new list<String>();
            lstFields.add('id');
            lstFields.add(firstValue);
            lstFields.add(secondValue);

            system.debug(lstFields);

            //Prepare dynamic query
            prepareQuery = 'SELECT id,'+firstValue+','+secondValue+' FROM Account';
            system.debug(prepareQuery);

            //Execute query
            accList = new List<Account>();
            accList = Database.query(prepareQuery);
            
            if(accList.size()>0)
            {
                hideTable=true;
            }
            system.debug(accList);

        }catch(Exception ex){
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage());
            ApexPages.addMessage(msg);         
        }
    }
ChellappaChellappa
I want to give you a preliminary thought..
onclick attribute on apex:inputcheckbox may not be a valid
if you want to use this, first use a JavaScript method and use actionFunction , this might work. 
otherwise, I will do some research on documentation and get back to you.