Another Salesforce blog!!!

Salesforce, Apex

Visualforce page Search: InputText with Button


Visualforce page:

<apex:page standardcontroller="Offer__c" extensions="OfferPreviewExtController"  title="Offer - {!Offer__c.Name}" sidebar="false" showHeader="false">
 
<!-- Page Header -->
    <!--apex:sectionHeader title="Candidate Edit" subtitle="New Candidate" /-->
    <br/>
    <apex:image style="padding:5px;" value="http://www.rbcroyalbank.com/rds/v2/_assets/images/logos/logo-rbcroyalbank.com.svg" styleClass="logo" />
    <!-- Begin Form -->
    <br/> 

<style type="text/css">
    .bg
    {
        color:white !important;
        background:#00CC00 !important; 
    }
</style>

<apex:form >

<apex:pageBlock >
    <center>
       <apex:outputLabel value="Offer ID:  " style="font-size:22px;"></apex:outputLabel> 
       <apex:inputText value="{!searchText}" id="theSearchstring" styleClass="increaseSize" style="font-size:32px;width: 350px; height: 38px"/> &nbsp;  
       <apex:commandButton rerender="op_detail" Id="btnSearch" action="{!search}"  title="Search" value=" GO " style="font-size:27px;" styleClass="bg"> </apex:commandButton>
    </center>
  </apex:pageBlock>     
  

<apex:outputPanel id="op_detail">
    <apex:outputPanel rendered="{!searchResultsFound==false}" >
    <apex:pageMessage summary="No Offer found matching criteria!" severity="warning" strength="3" /><apex:pageMessages /></apex:outputPanel>

  <apex:pageBlock title="Offer Preview" id="pb" rendered="{!searchResultsFound }" >  
  <apex:pageBlockSection title="General Information" columns="2"  >
    
    <apex:repeat value="{!SearchResults}" var="off">
        <apex:pageBlockSection >
            <apex:outputField value="{!off.Offer_Id__c}"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection >
            <apex:outputField value="{!off.Offer_Status__c}"/>
        </apex:pageBlockSection>
         <apex:pageBlockSection >
            <apex:outputField value="{!off.Name}"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection >
            <apex:outputField value="{!off.Inactive_Reason__c}"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection >
            <apex:outputField value="{!off.Display_Name_En__c}"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection >
            <apex:outputField value="{!off.Display_Name_Fr__c}"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection >
            <apex:outputField value="{!off.Requested_Approval_Date__c}"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection >
            <apex:outputField value="{!off.Merchant__c}"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection >
            <apex:outputField value="{!off.Offer_API_Name__c}"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection >
            <apex:outputField value="{!off.Offer_Program__c}"/>
        </apex:pageBlockSection>
      
    </apex:repeat>
</apex:pageBlockSection>
 
  </apex:pageBlock>
  </apex:outputPanel>
 </apex:form>
</apex:page>

Controller:

public class OfferPreviewExtController
{ 
 
    transient public List<Offer__c> searchResults {get; set; }
    public boolean searchClicked {get; set; }
    
    public OfferPreviewExtController(ApexPages.StandardController controller) 
    {         
        searchClicked = false;
    }  

    public string searchText
    {
        get
        {
            if (searchText==null) searchText = '';
            return searchText;
        }
        set;
    }
    
    public boolean searchResultsFound {get; set;}
    
    public void isRecordFound()
    {
       searchResultsFound = true;
       if(SearchResults !=null) {
        if(SearchResults.isEmpty())
        {
            searchResultsFound = false; //not found
        }
        }
    }
  
    public PageReference search()
    {
        searchClicked = true;
        if(SearchResults == null)
        {
            SearchResults = new List<Offer__c >();
        }
        else
        {
            SearchResults.Clear();
        }  
        SearchResults = [Select Offer_Id__c,Id, Name,Offer_Status__c,Inactive_Reason__c,Display_Name_En__c,Display_Name_Fr__c,Requested_Approval_Date__c,Merchant__r.Name,Offer_API_Name__c,Offer_Program__c from Offer__c where name =: searchText];
      
        if(!SearchResults.isEmpty()) 
        {
            searchResultsFound = true;
        }
        else
        {
            searchResultsFound = false;
        }
        return null;         
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*