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>