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
ajaybharathajaybharath 

How to add a checkbox to a dynamic search

Hi
I'm new to salesforce .. i have been asked to do a dynamic search on the records for the custom object  .. say ratings .. i have 3 fields in it and i have implemented the dynamic search . its working fine .. now i need to add a checkbox for the search results , but its throwing an error ..

P.S leave the standard set controller part of my code .. its working fine too

 

Thanks in advance

my vf page:

 

<apex:page controller="Search" sidebar="false">
  <apex:form >
  <apex:pageMessages id="errors" />
  <apex:pageBlock title="Ratings Search" mode="edit">
  <apex:pageBlockButtons >
        <apex:commandButton action="{!process}" value="Add to Cart"/>
        <apex:commandButton action="{!cancel}" value="Cancel"/>
  </apex:pageBlockButtons>
  <table width="100%" border="0">
  <tr>  
  <td width="200" valign="top">
      <apex:pageBlock title="" mode="edit" id="criteria">
  
      <script type="text/javascript">
      function doSearch() {
        searchServer(
         document.getElementById("name").value,
         document.getElementById("phone").value,
         document.getElementById("status").options[document.getElementById("status").selectedIndex].value
          );
      }
      </script> 
 
      <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
          <apex:param name="name" value="" />
          <apex:param name="phone" value="" />
          <apex:param name="status" value="" />
      </apex:actionFunction>
 
      <table cellpadding="2" cellspacing="2">
      <tr>
        <td style="font-weight:bold;">Rating Member Name<br/>
        <input type="text" id="name" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Customer's Phone Number<br/>
        <input type="text" id="phone" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Rating Status<br/>
          <select id="status" onchange="doSearch();">
            <option value=""></option>
            <apex:repeat value="{!statuses}" var="stat">
              <option value="{!stat}">{!stat}</option>
            </apex:repeat>
          </select>
        </td>
      </tr>
      </table>
      </apex:pageBlock>
    </td>
  
    <td valign="top">
    <apex:pageBlock mode="edit" id="results">
        <apex:pageBlockTable value="{!rate}" var="sqrs">
        <apex:column width="25px">
            <apex:inputcheckbox /></apex:column>
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Rating Member Name" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="name" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
       <apex:outputLink value="/{!sqrs.id}">
       <apex:outputField value="{!sqrs.name}"/></apex:outputlink>
            </apex:column>
             
             
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Phone" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Customer_s_Phone_No__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!sqrs.Customer_s_Phone_No__c}"/>
            </apex:column><apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="statuses" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Rating_Status__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!sqrs.Rating_Status__c}"/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
  </td>
  </tr>
  </table>
    <apex:pageBlockSection title="Rating Results" columns="1">
                <apex:pageBlockTable value="{!categories}" var="c">
                     <apex:column width="25px">
                          <apex:inputCheckbox value="{!c.checked}"/>
                     </apex:column>
                     <apex:column value="{!c.cat.Name}" headerValue="Name"/>
                     <apex:column value="{!c.cat.Customer_s_Phone_No__c}" headerValue="Phone Number"/>
                     <apex:column value="{!c.cat.Rating_Status__c}" headerValue="Status"/>
                </apex:pageBlockTable>
        </apex:pageBlockSection>

  </apex:pageBlock>
    <apex:panelGrid columns="4">
    <apex:commandLink action="{!first}">First</apex:commandlink>
    <apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous</apex:commandlink>
    <apex:commandLink action="{!next}" rendered="{!hasNext}">Next</apex:commandlink>
    <apex:commandLink action="{!last}">Last</apex:commandlink>
    </apex:panelGrid>
  </apex:form>
</apex:page>

 

search class :

public with sharing class Search {
 
  // the soql without the order and limit
  private String soql {get;set;}
  // the collection of contacts to display
  public List<Rating__c> rate {get;set;}
  public List<categoryWrap> selected {
    get{
    if (selected == null) 
    selected = new List<categoryWrap>();
    return selected;
    }
    set;
  }
  List<categoryWrapper> categories {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 20'; }
    set;
  }
 
  // init the controller and display some sample data when the page loads
  public Search() {
    soql = 'select id,Name,Comments__c,Customer_s_Phone_No__c,Rating_Status__c FROM Rating__c 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 {
      rate = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20');
    } catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
    }
 
  }
  public ApexPages.StandardSetController con {
        get {
            if(con == null) {
                con = new ApexPages.StandardSetController(rate);
                con.setPageSize(5);
            }
            return con;
        }
        set;
  }
  public List<categoryWrap> getCategories() {
        categories = new List<categoryWrap>();
        for (Rating__c category : (List<Rating__c>)con.getRecords())
            categories.add(new CategoryWrapper(category));
 
        return categories;
  }
 
  // runs the search with parameters passed via Javascript
  public PageReference runSearch() {
 
    String name = Apexpages.currentPage().getParameters().get('name');
    //String lastName = Apexpages.currentPage().getParameters().get('lastname');
    //String accountName = Apexpages.currentPage().getParameters().get('accountName');
    String phone = Apexpages.currentPage().getParameters().get('phone');
    String status = Apexpages.currentPage().getParameters().get('status');
 
    soql = 'select id,name,Comments__c,Customer_s_Phone_No__c,Rating_Status__c FROM Rating__c where name != null';
    if (!name.equals(''))
      soql += ' and name LIKE \''+String.escapeSingleQuotes(name)+'%\'';
    if (!phone.equals(''))
      soql += ' and Customer_s_Phone_No__c LIKE \''+String.escapeSingleQuotes(phone)+'%\'';
    if (!status.equals(''))
      soql += ' and Rating_Status__c = \''+status+'\'';

    // run the query again
    runQuery();
 
    return null;
  }
  public PageReference process() 
  {
  
       for (CategoryWrap cw : categories) {
            if (cw.checked)
               selected.add(new CategoryWrapper(cw.cat));
               //ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,cw.cat.name));
         }
	 if (selected.size() > 0) {
             return Page.sel;
         } else {
             ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select at least one Category.'));
             return null;
         }
  }
  public PageReference back() {
        return Page.Search;
  }
  public void clear() {
	selected.clear();
	ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'List Cleared'));
  }
     public Boolean hasNext {
        get {
            return con.getHasNext();
        }
        set;
    }
    public Boolean hasPrevious {
        get {
            return con.getHasPrevious();
        }
        set;
    }
    public Integer pageNumber {
        get {
            return con.getPageNumber();
        }
        set;
    }
    public void first() {
        con.first();
    }
    public void last() {
        con.last();
    }
    public void previous() {
        con.previous();
    }
    public void next() {
        con.next();
    }
    public void cancel() {
        con.cancel();
    }
}

 
  // use apex describe to build the picklist values
  public List<String> statuses {
    get {
      if (statuses == null) {
 
        statuses = new List<String>();
        Schema.DescribeFieldResult field = Rating__c.Rating_Status__c.getDescribe();
 
        for (Schema.PicklistEntry f : field.getPicklistValues())
          statuses.add(f.getLabel());
 
      }
      return statuses;          
    }
    set;
  } 
}

 category wrap class :

public class CategoryWrap {
 
    public Boolean checked{ get; set; }
    public Rating__c cat { get; set;}
 
    public CategoryWrap(){
        cat = new Rating__c();
        checked = false;
    }
 
    public CategoryWrap(Rating__c c){
        cat = c;
        checked = false;
    }
 
    public static testMethod void testMe() {
 
        CategoryWrap cw = new CategoryWrap();
        System.assertEquals(cw.checked,false);
 
        CategoryWrap cw2 = new CategoryWrap(new Rating__c(name='Test1'));
        System.assertEquals(cw2.cat.name,'Test1');
        System.assertEquals(cw2.checked,false);
 
    }
 
}