Saturday, June 14, 2014

How to call Apex method from VF page When Pressing of Enter Key.

 How to call Apex method from VF page When Pressing of Enter Key.

 Hi,

In this example i am going to call apex method when ever user enters Account name in search box and press enter key.the following is the code example.

Visualforce page :
<apex:page standardController="account" extensions="searchaccounts">
   <apex:form >
         <script type='text/javascript'>
           function runOnEnter(ev)
           {
            if (window.event && window.event.keyCode == 13 || ev.which == 13)
              {
               searchAccRecs();
               return false;
              }
              else
              {
                return true;
              }
           }
</script>
<apex:pageBlock >
      <apex:inputText value="{!searchString}" onkeypress="return runOnEnter(event);"/>
         <apex:commandButton value="Submit" action="{!searchAcc}" reRender="pgblcktbl"/>
          <apex:pageBlockTable value="{!accList}" var="acc" id="pgblcktbl">
            <apex:column value="{!acc.name}"/>
            <apex:column value="{!acc.accountnumber}"/>
            <apex:column value="{!acc.annualrevenue}"/>
            <apex:column value="{!acc.Industry}"/>
            <apex:column value="{!acc.AccountSource }"/>
         </apex:pageBlockTable>
</apex:pageBlock>
<apex:actionFunction name="searchAccRecs" action="{!searchAcc}" reRender="pgblcktbl"/>
</apex:form>
</apex:page>


Extension controller :
public with sharing class searchaccounts {

       Public List<account> accList{get;set;}
       Public string searchString{get;set;}

          public searchaccounts(ApexPages.StandardController controller) {
     }
     Public void searchAcc()
     {
         accList = new List<account>();
         accList = [select id,name,accountnumber,annualrevenue,Industry,AccountSource from account where name =: searchString limit 5];
     }

}



save image


That's it..

0 comments:

Post a Comment

 
| ,