+ Start a Discussion
symantecAPsymantecAP 

Search on the VF Page

Hi All 

 

I have built a VF Page which displays list of products up to 1000. I need to create a Search bar where we can type product name and get the desired PRODUCT. 

 

I need help on it please. 

Ankit AroraAnkit Arora
Rahul S.ax961Rahul S.ax961

Your data might be in a list,

Below are steps,

Take a Input text and command button,

Onclick of command button, call a apex function and value of input text as a parameter to thst function,

And in that function, dynamically query using something like this - Where name like : %InputtextValue%.

and add rerender Table on Command button.

Let me know if you have questions.

 

Bro, i dont think app would be necessary for such a small task. :)

SfdcBlueFishSfdcBlueFish

Hai Rahul,

 

please check below code this may be helps u

 

 

<apex:page controller="searchcontroller">
<apex:form >
<apex:pageBlock mode="edit" id="block"  title="Search and Edit Records">
<apex:pageBlockButtons location="bottom/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel for="searchText">Search Text</apex:outputLabel>
<apex:panelGroup >
<apex:inputText id="searchText" value="{!searchText}"/>
<apex:commandButton value="Go!" action="{!doSearch}"
rerender="block" status="status"/>
</apex:panelGroup>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:actionStatus id="status" startText="requesting..."/>
<apex:pageBlockSection title="Results" id="results" columns="1" rendered="{!get}">
<apex:pageBlockTable value="{!results}" var="l"
rendered="{!NOT(ISNULL(results))}">
<apex:column value="{!l.Name}"/>
<apex:column headerValue="Email">
<apex:inputText value="{!l.Email__c}"/></apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller
public class searchcontroller {
    string get='False';
    
    public string getGet()
    {
       return get;
     
    }
String searchText;
List<Employee__c> results{get;set;}
public String getSearchText() {
return searchText;
}
public void setSearchText(String s) {
searchText = s;
}
public List<Employee__c> getResults() {
get='true';
return results;
}
public PageReference doSearch() {
results = (List<Employee__c>)[FIND :searchText RETURNING Employee__c(Name, Email__c)][0];
return null;
}
}

 

 

Haribabu

Force.com certified developer

flexandsalesforce.blogspot.com

symantecAPsymantecAP

Dont we have to use AJAX to display the  result on the same screen . ?

Rahul S.ax961Rahul S.ax961

Try this:

 

Page:

 

<apex:page controller="searchcontroller">
<apex:form >
<apex:pageBlock mode="edit" title="Search and Edit Records">
<apex:pageBlockButtons location="bottom/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel for="searchText">Search Text</apex:outputLabel>
<apex:panelGroup >
<apex:inputText id="searchText" value="{!searchText}"/>
<apex:commandButton value="Go!" action="{!doSearch}"
rerender="block" status="status"/>
</apex:panelGroup>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:actionStatus id="status" startText="requesting..."/>
<apex:pageBlockSection title="Results" id="results" columns="1" rendered="{!get}">
<apex:pageBlockTable value="{!results}" var="l"
rendered="{!NOT(ISNULL(results))}" id="block" >
<apex:column value="{!l.Name}"/>
<apex:column headerValue="Email">
<apex:inputText value="{!l.Email__c}"/></apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 Controller:

 

public class searchcontroller 
{
string get='False';

public string getGet()
{
return get;
}
String searchText;
List<Employee__c> results
{get;set;}
public String getSearchText()
{
return searchText;
}
public void setSearchText(String s)
{
searchText = s;
}
public List<Employee__c> getResults()
{
get='true';
return results;
}
public PageReference doSearch()
{
String SearchString = '%' + searchText + '%';
results = new List<Employee__c>();
results = database.query('Select Name, Id, Email__c from Employee__c where Name like :SearchString');
// Dynamic query which checks with name
}
}

 

 

 

symantecAPsymantecAP

Hi Rahul I have been trying the code proposed by u . 

Here is my page which diplay a list of products and I need display Search button on the top of it..which search the products on the page.

 

Here is my code. 

<apex:page controller="ProductPriceBook23" tabStyle="Product2">

<apex:pageBlock >
         <h1><u>Report : Products and PriceBooks for Region= NA and Product Family=PRODUCT </u>  </h1>
 </apex:pageBlock> 
    



    <apex:pageBlock >
        <apex:pageblockTable value="{!products}" var="product">
                    <apex:column headerValue="Product Code" width="150">
                    <a href="/{!product.prod.id}">{!product.prod.Productcode}</a>
                    </apex:column>
                    
                     <apex:column headerValue="Product Name" width="500">
                                         <a href="/{!product.prod.id}">{!product.prod.Name}</a>

                    </apex:column>

          <apex:column value="{!product.prod.Description}"  width="25" />
          <apex:column headerValue="List Price">           
                <apex:repeat value="{!product.pbes}" var="pbe">
                    <apex:outputField value="{!pbe.UnitPrice}" /> <br/>
                </apex:repeat>
            </apex:column>  
    
            <apex:column headerValue="NA DIST (USD) " width="200" >           
                <apex:repeat value="{!product.pbef}" var="pbe">
                    <apex:outputField value="{!pbe.unitprice}"  /> <br/>
                </apex:repeat>
            </apex:column>  
                 
                <apex:column headerValue="NA DIRECT (USD) " width="200" >           
                <apex:repeat value="{!product.pbes}" var="pbe">
                    <apex:outputField value="{!pbe.unitprice}"  /> <br/>
                </apex:repeat>
            </apex:column>  
            
            <apex:column headerValue="NA AP (USD) " width="200" >           
                <apex:repeat value="{!product.pbea}" var="pbe">
                    <apex:outputField value="{!pbe.unitprice}"  /> <br/>
                </apex:repeat>
            </apex:column>  
            
             <apex:column headerValue="NA LA (USD) " width="200" >           
                <apex:repeat value="{!product.pbeb}" var="pbe">
                    <apex:outputField value="{!pbe.unitprice}"  /> <br/>
                </apex:repeat>
            </apex:column>  
                 
            
        </apex:pageblockTable>
    </apex:pageBlock>
</apex:page>

 here is controller

 

 

public class ProductPriceBook23 {
    
   public class productInfo {
    
        public Product2 prod{get; set;}
        public List<PriceBookEntry> pbes {get; set;}
                public List<PriceBookEntry> pbef {get; set;}
                                public List<PriceBookEntry> pbea {get; set;}

                                public List<PriceBookEntry> pbeb {get; set;}
                                  // public List<PriceBookEntry> pbec {get; set;}


        public productInfo() {
            pbes = new List<PriceBookEntry>();
                        pbef = new List<PriceBookEntry>();
           pbea = new List<PriceBookEntry>();
           pbeb = new List<PriceBookEntry>();
                     // pbec = new List<PriceBookEntry>();



        }
        
    }
    
    public List<ProductInfo> products {get; set;}
    
    public ProductPriceBook23() {
        products = new List<productInfo >();
        Map<Id, productInfo> pInfoMap = new Map<Id, productInfo>();
     for (PricebookEntry pEntry :[Select id, unitPrice, product2Id, product2.Name, product2.Description, product2.Family,product2.ATFE_Status__c, product2.ProductCode, pricebook2id, pricebook2.name from PricebookEntry where   product2.ATFE_ProductRegion__c='NA' and product2.Family= 'PRODUCT' and pricebook2.IsActive=true and Pricebook2.name='NA LA (USD)'      LIMIT 1000  ]) {
            ProductInfo pInfo = new ProductInfo();
            if(!pInfoMap.containsKey(pEntry.product2Id)) {
                pInfo.prod = new Product2(id=pEntry.product2Id, Name=pEntry.product2.Name, Description=pEntry.product2.Description, Family=pEntry.product2.Family,ATFE_Status__c=pEntry.product2.ATFE_Status__c, ProductCode=pEntry.product2.ProductCode);
                pInfo.pbes.add(pEntry);

                pInfoMap.put(pEntry.product2Id, pInfo);
            } else {
                pInfo = pInfoMap.get(pEntry.product2Id);
                pInfo.pbes.add(pEntry);
            }
        }
        
        for (PricebookEntry pEntry1 :[Select id, unitPrice, product2Id, product2.Name, product2.Description, product2.Family,product2.ATFE_Status__c, product2.ProductCode, pricebook2id, pricebook2.name from PricebookEntry where   product2.ATFE_ProductRegion__c='NA' and product2.Family= 'PRODUCT' and pricebook2.IsActive=true and Pricebook2.name='NA DIST (USD)'  LIMIT 1000  ]) {
            ProductInfo pInfo = new ProductInfo();
            if(!pInfoMap.containsKey(pEntry1.product2Id)) {
                pInfo.prod = new Product2(id=pEntry1.product2Id, Name=pEntry1.product2.Name, Description=pEntry1.product2.Description, Family=pEntry1.product2.Family,ATFE_Status__c=pEntry1.product2.ATFE_Status__c, ProductCode=pEntry1.product2.ProductCode);
                pInfo.pbef.add(pEntry1);

                pInfoMap.put(pEntry1.product2Id, pInfo);
            } else {
                pInfo = pInfoMap.get(pEntry1.product2Id);
                pInfo.pbef.add(pEntry1);
            }
        }
        
         for (PricebookEntry pEntry2 :[Select id, unitPrice, product2Id, product2.Name, product2.Description, product2.Family,product2.ATFE_Status__c, product2.ProductCode, pricebook2id, pricebook2.name from PricebookEntry where   product2.ATFE_ProductRegion__c='NA' and product2.Family= 'PRODUCT' and pricebook2.IsActive=true and Pricebook2.name='NA AP (USD)'      LIMIT 1000  ]) {
            ProductInfo pInfo = new ProductInfo();
            if(!pInfoMap.containsKey(pEntry2.product2Id)) {
                pInfo.prod = new Product2(id=pEntry2.product2Id, Name=pEntry2.product2.Name, Description=pEntry2.product2.Description, Family=pEntry2.product2.Family,ATFE_Status__c=pEntry2.product2.ATFE_Status__c, ProductCode=pEntry2.product2.ProductCode);
                pInfo.pbea.add(pEntry2);

                pInfoMap.put(pEntry2.product2Id, pInfo);
            } else {
                pInfo = pInfoMap.get(pEntry2.product2Id);
                pInfo.pbea.add(pEntry2);
            }
        }
       
      for (PricebookEntry pEntry3 :[Select id, unitPrice, product2Id, product2.Name, product2.Description, product2.Family,product2.ATFE_Status__c, product2.ProductCode, pricebook2id, pricebook2.name from PricebookEntry where   product2.ATFE_ProductRegion__c='NA' and product2.Family= 'PRODUCT' and pricebook2.IsActive=true and Pricebook2.name='NA LA (USD)'      LIMIT 1000  ]) {
            ProductInfo pInfo = new ProductInfo();
            if(!pInfoMap.containsKey(pEntry3.product2Id)) {
                pInfo.prod = new Product2(id=pEntry3.product2Id, Name=pEntry3.product2.Name, Description=pEntry3.product2.Description, Family=pEntry3.product2.Family,ATFE_Status__c=pEntry3.product2.ATFE_Status__c, ProductCode=pEntry3.product2.ProductCode);
                pInfo.pbeb.add(pEntry3);

                pInfoMap.put(pEntry3.product2Id, pInfo);
            } else {
                pInfo = pInfoMap.get(pEntry3.product2Id);
                pInfo.pbeb.add(pEntry3);
            }
        }
   /*  for (PricebookEntry pEntry4 :[Select id, unitPrice, product2Id, product2.Name, product2.Description, product2.Family,product2.ATFE_Status__c, product2.ProductCode, pricebook2id, pricebook2.name from PricebookEntry where   product2.ATFE_ProductRegion__c='NA' and product2.Family= 'PRODUCT' and pricebook2.IsActive=true and Pricebook2.name like'%(USD)'      LIMIT 1000  ]) {
            ProductInfo pInfo = new ProductInfo();
            if(!pInfoMap.containsKey(pEntry4.product2Id)) {
                pInfo.prod = new Product2(id=pEntry4.product2Id, Name=pEntry4.product2.Name, Description=pEntry4.product2.Description, Family=pEntry4.product2.Family,ATFE_Status__c=pEntry4.product2.ATFE_Status__c, ProductCode=pEntry4.product2.ProductCode);
                pInfo.pbec.add(pEntry4);

                pInfoMap.put(pEntry4.product2Id, pInfo);
            } else {
                pInfo = pInfoMap.get(pEntry4.product2Id);
                pInfo.pbec.add(pEntry4);
            }
        }*/
       
    
        
        products = pInfoMap.values();
    }
     
    
}