• sfdc.developer
  • NEWBIE
  • 0 Points
  • Member since 2009

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies

Hi, I am trying to implement the dynaimic search as found on Jeff Douglass' page (http://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/) but i've run into a problem...

 

What is supposed to happen is when you type into the input box the results are filtered out letter by letter - this is the bit that's not working, i'm not sure if the problem is in the SQL on the controller, i have a feeling its in the sql but i have tried almost everything and i've hit a blank so i thought i'd ask for some help!!!

 

If someone could give me a bit of help / guidance i would be very thankful!

 

Apex Class

 

public with sharing class ContactSearchController {

        

  // the soql without the order and limit
  private String soql {get;set;}
  // the collection of contacts to display
  public List <Account> accounts {get;set;}

  // the current sort direction. defaults to asc
  public String sortDir {
    get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;  }
    set;
  }

  // the current field to sort by. defaults to last name
  public String sortField {
    get  { if (sortField == null) {sortField = 'Name'; } return sortField;  }
    set;
  }

  // format the soql for display on the visualforce page
  public String debugSoql {
    get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 60'; }
    set;
  }

  // init the controller and display some sample data when the page loads
  public ContactSearchController() {
    soql = 'select Name,  fl_buy__c, fl_sell__c  from account where name != null';
    runQuery();
  }

  // toggles the sorting of query from asc<-->desc
  public void toggleSort() {
    // simply toggle the direction
    sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
    // run the query again
    runQuery();
  }

  // runs the actual query
  public void runQuery() {

    try {
      accounts = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 60');
    } catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
    }

  }

   // runs the search with parameters passed via Javascript
  public PageReference runSearch() {
  
    String Name = Apexpages.currentPage().getParameters().get('Name');
    String fl_buy = Apexpages.currentPage().getParameters().get('fl_buy__c');
    String fl_sell = Apexpages.currentPage().getParameters().get('fl_sell__c');

    
    soql = 'select Name from account where Name != null';
    if ( (null!= Name) && (!''.equals(Name)) )
      soql += 'and Name LIKE \''+String.escapeSingleQuotes(Name)+'%\'';
    if ( (null!= fl_buy) && (!''.equals(fl_buy)) )
      soql += ' and fl_buy CONTAINS \''+String.escapeSingleQuotes(fl_buy)+'%\'';
    if ( (null!= fl_sell) && (!''.equals(fl_sell)) )
      soql += ' and fl_sell CONTAINS \''+String.escapeSingleQuotes(fl_sell)+'%\'';
      
        

    // run the query again
    runQuery();

    return null;
  }

  

}

 

 

VF Page

 

<apex:page controller="ContactSearchController" sidebar="false">
  <apex:form >
  <apex:pageMessages id="errors" />
<br/>
  <apex:pageBlock title="Find Me An Account!" mode="edit">
<table width="100%" border="0">
    <tr>
        <td width="200" valign="top">

              <apex:pageBlock title="Search" mode="edit" id="criteria">

              <script type="text/javascript">
                  function doSearch() {
                        searchServer(
                              document.getElementById("Name").value,
                              document.getElementById("fl_buy__c").value,
                              document.getElementById("fl_sell__c").value
                        );
                  }
              </script> 

          <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results">
              <apex:param name="Name" value="" />
              <apex:param name="fl_buy__c" value="" />
              <apex:param name="fl_sell__c" value="" />
           </apex:actionFunction>
           
<table cellpadding="2" cellspacing="2">

    <tr>
        <td style="font-weight:bold;">Account Name<br/>
            <input type="text" id="Name" onkeyup="doSearch();"/>
        </td>
    </tr>
    
    <tr>
        <td style="font-weight:bold;">Buy Currency<br/>
            <input type="text" id="abuy" onkeyup="doSearch();"/>
        </td>
    </tr>
    
    <tr>
        <td style="font-weight:bold;">Sell Currency<br/>
            <input type="text" id="asell" onkeyup="doSearch();"/>
        </td>
    </tr>

    
 
    <tr>

    </tr>
</table>

      </apex:pageBlock>
</td>
<td valign="top">

    <apex:pageBlock mode="edit" id="results">

        <apex:pageBlockTable value="{!accounts}" var="account">

<apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Account Name" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Name" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputLink value="/{!account.id}">{!account.Name}</apex:outputLink>
            </apex:column>

         <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Buy Currency" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="fl_buy__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputLink value="/{!account.id}">{!account.fl_buy__c}</apex:outputLink>
            </apex:column>
            
         <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Sell Currency" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="fl_sell__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputLink value="/{!account.id}">{!account.fl_sell__c}</apex:outputLink>
            </apex:column>

           

        </apex:pageBlockTable>

    </apex:pageBlock>
</td>
</tr>
</table>

  

  </apex:pageBlock>

  </apex:form>

</apex:page>

 

We currently have two positions available:
  • Administrator
  • Consultant/Developer
Please contact me if you are interested.

Regards.

  • January 13, 2009
  • Like
  • 0