0

I have a page that has a search function in it. Search results are displayed on a table wherein the user can select each item. This whole page works, but there's a requirement that needs to maintain the state of those selected items.

Meaning, list of checked records must not disappear even though different keyword has been typed into the search box. In my case, selected records disappear each time I input different keywords.

Apex Class:

public class ContactController {

public ContactController(ApexPages.StandardController controller)
{
 cid=System.currentPageReference().getParameters().get('id');
}


Public String cid;                                                            
public List<ContactWrapper> contactWrap {get; set;}                                                                                                             
String searchString;                                                             

Public List<Contact> results = new List<Contact>();                                     

public String getuserinput(){return searchString;}
public void setuserinput(String srchStrng){this.searchString=srchStrng;}

public List<Contact> contactsearch()
{
     //contactWrap = new List< ContactWrapper>();
     List<ContactWrapper> ccWrap = new List<ContactWrapper>();   
     for(Contact c : [select Id,Name,Account.Name,Name,FirstName,LastName,Email,title from Contact where Name like :userinput+'%'])
     {
         //contactWrap.add(new ContactWrapper(c));
         ccWrap.add(new ContactWrapper(c));
     }

     contactWrap = ccWrap;
     return null;
}

public List<ContactWrapper> getSearchResults()
{

 return contactWrap;

}

public class ContactWrapper
{
 public Contact con {get; set;}
 public Boolean selected {get; set;}

 public ContactWrapper(Contact c)
 {
     con = c;
     selected = false;
 }
}

Visualforce Page :

<apex:page standardcontroller="Contact" extensions="ContactController" >

<script>
function checkAll(cb)
{
   var inputElem = document.getElementsByTagName("input");
   for(var i=0;i<inputElem.length;i++)
     {
             if(inputElem[i].id.indexOf("selectLine1")!=-1)
                   inputElem[i].checked = cb.checked;
      }
}
</script>

<apex:form >

<apex:pageblock >
<apex:pageBlockSection title="Search Contacts" columns="1"></apex:pageBlockSection>
    <apex:panelGrid columns="2">
        <apex:outputLabel value="Contact Name" ></apex:outputLabel>
        <apex:inputText value="{!searchString}"/>
    </apex:panelGrid>

    <apex:commandButton value="Search" action="{!contactsearch}" />

<apex:pageblocksection columns="1" title="Search results" rendered="{!NOT(ISNULL(SearchResults))}" >
  <apex:outputpanel id="Contactlist">

        <apex:pageBlockTable value="{!SearchResults}" var="contacts">
             <apex:column >
                <apex:facet name="header">
                    <apex:inputCheckbox onclick="checkAll(this)"/>
                </apex:facet>
                    <apex:inputCheckbox value="{!contacts.selected}" id="selectLine1"/>
            </apex:column>
            <apex:column headervalue="Contact Name">
                <apex:outputtext value="{!contacts.con.Name}"/>
            </apex:column>
            <apex:column headervalue="Account Name">
                <apex:outputtext value="{!contacts.con.Account.Name}"/>
            </apex:column>
            <apex:column headervalue="Title">
                <apex:outputtext value="{!contacts.con.Title}"/>
            </apex:column>
        </apex:pageBlockTable>  < 

    </apex:outputpanel>
</apex:pageblocksection>

</apex:pageblock>
</apex:form>
</apex:page>

1 Answer 1

1

You are reinitializing the list contactWrap in the contactSearch method each time a search is performed. If you want to retain selected results from the previous search, remove the line

 contactWrap = new List< ContactWrapper>();

Instead, create a new List<ContactWrapper> variable in your contactSearch method, iterate over the existing results, if any, and add selected items to that new list prior to performing the SOQL query. Then, at the end of the method, assign that new list to contactWrap.

2
  • I've modified my posted code based from my understanding of your description. But selected items still disappear from the previous search. Apologies if I don't quite get what you mean.
    – curdual
    Feb 18, 2018 at 4:19
  • You are not adding the selected items from the current instantiation of contactWrap to your ccWrap list.
    – David Reed
    Feb 18, 2018 at 4:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .