function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
CharlieLangCharlieLang 

Dynamic Search Issue

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>

 

Sankalp JhingranSankalp Jhingran

Hi Charlie,

 

Can you try moving the following javascript on the page before the <apex:form> tag:

 

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

I faced the same issue and after some tweaking, found the issue in java script placement.

Let me know, if that works for you.

 

Thanks, Sankalp

CharlieLangCharlieLang

Hi,

 

I've tried moving this and it doesn't seem to make any difference.

 

 

Sankalp JhingranSankalp Jhingran

You can investigate further using firebug add-on in firefox and looking in the console.

I am sure, it is the issue with the java-script only.

 

Regards,

Sankalp

sfdc.developersfdc.developer

Hi Charlie,

 

There are few things wrong in your code: (both VF and Controller)

 

Controller Class Issues:

 

- In the function runSearch, you need to put an extra space before 'and'

 

your code:

 

soql += 'and Name LIKE \''+String.escapeSingleQuotes(Name)+'%\'';

 

Fix:

 

soql += ' and Name LIKE \''+String.escapeSingleQuotes(Name)+'%\'';

 

 

Visualforce Issues:

 

- The text fields need to have their Id changed

 

Your code:

<input type="text" id="abuy" onkeyup="doSearch();"/>

 

Fix:

<input type="text" id="fl_buy__c" onkeyup="doSearch();"/>

 

 

Your code:

<input type="text" id="asell" onkeyup="doSearch();"/>

 

Fix:

<input type="text" id="fl_sell__c" onkeyup="doSearch();"/>

 

 

Do try and let me know

 

Cheers