You need to sign in to do that
Don't have an account?
Krishnan Mishra
How do i retain value of checkbox?
In the followin code as soon as my page gets refreshed my record gets unchecked again,How can i avoid that?
Controller code:
VisualForce code:
Controller code:
public class t1 { list<contact> con = [SELECT Name,Account.name,Title,Phone,Email FROM Contact]; public list<wrapper> allContactList; public list<contact> SelectedContact = new list<contact> (); public list<wrapper> getWrapperContacts(){ allContactList = new list<wrapper>(); for(contact c: (list<contact>)stdSetController.getRecords()) allContactList.add(new wrapper(c)); return allContactList; } public void getSelectedListContacts(){ for(wrapper wc:allContactList){ System.debug(wc.isSelected+' '+wc.con); if(wc.isSelected==true){ SelectedContact.add(wc.con); } } System.debug(selectedContact); } public void getSelectedAllContacts(){ //To select all contacts for(wrapper wc:allContactList){ SelectedContact.add(wc.con); //new t1.wrapper().isSelected=true; } } public ApexPages.StandardSetController stdSetController{ //Instantiating a standard set controller get{ if(stdSetController==null){ stdSetController = new ApexPages.StandardSetController(con); } stdSetController.setPageSize(10); //Limiting Number of records to be displayed per page return stdSetController; } set; } public class wrapper{ public boolean isSelected{ get{ if(this.isSelected==null) this.isSelected = false; else this.isSelected = true; return isSelected; } set; } public Contact con{get;set;} wrapper(contact con){ //isSelected = false; this.con = con; } } }
VisualForce code:
<apex:page controller="t1" > <apex:form > <apex:pageBlock id="table"> <apex:commandButton action="{!getSelectedAllContacts}" value="Select All" reRender="table"/> <apex:pageBlockTable value="{!WrapperContacts}" var="c"> <apex:column > <apex:inputCheckbox value="{!c.isSelected}"> <apex:actionSupport event="onclick" action="{!getSelectedListContacts}" reRender="table"/> </apex:inputCheckbox> </apex:column> <apex:column value="{!c.con.name}"/> <apex:column value="{!c.con.title}"/> </apex:pageBlockTable> <!-- To return to first page of records--> <apex:commandButton action="{!stdSetController.first}" value="<<" title="First Page" disabled="{!!stdSetController.HasPrevious}" reRender="table,button"/> <!-- To return to Previous page of records--> <apex:commandButton action="{!stdSetController.previous}" value="Previous" disabled="{!!stdSetController.HasPrevious}" reRender="table,button"/> <!-- To return to next page of records--> <apex:commandButton action="{!stdSetController.next}" value="Next >" disabled = "{!!stdSetController.HasNext}" reRender="table,button"/> <!-- To return to last page of records--> <apex:commandButton action="{!stdSetController.last}" value=">>" title="Last Page" disabled="{!!stdSetController.HasNext}" reRender="table,button"/> </apex:pageBlock> </apex:form> </apex:page>
In your case, everytime the page is refreshed, it calls the controller and calls getWrapperContacts method. In this your list is getting instantiated again and so the checkbox also getting default value.
Here you can do, remove allContactList = new list<wrapper>(); from this method and add this in constructor.
Try this, in case of anny issue please let me know. If this fixes your issue, mark it as solved.
In the following code to achieve pagnination i have to clear allContactList so that only new records are shown but i want to retain the boolean values of the records stored previously in allContactList so that value of checkbox sustains.Can you please help me out in that too. All I want is pagination with retention of values of checkboxes!