You need to sign in to do that
Don't have an account?
Anil kumar Gorantala
get indexes of checkboxes and remove them from list at a time but not by iterating
I want know how to get indexes of checkboxes and remove them from list at a time but not by iterating. How to do this? plz someone help me...
Here are my apex class and vf page:
Class:
vf page:
Here are my apex class and vf page:
Class:
public class DeletingCheckedRowsController { public List<WrapperClass> listWrapper {get;set;} public list<integer> temp{get;set;} public Boolean allchecked {get;set;} public DeletingCheckedRowsController() { listWrapper = new List<WrapperClass>(); List<contact> contactList = [SELECT Id, firstName, lastname, Email, Phone FROM contact]; if(contactList.size() > 0) { for(contact con : contactList) { listWrapper.add(new WrapperClass(con)); } } } public class WrapperClass { public Boolean checked {get;set;} public contact con {get;set;} public WrapperClass(contact con) { this.con = con; } } public void del() { integer count=0; temp=new list<integer>(); List<contact> listconForDel = new List<contact>(); List<WrapperClass> listTempWrapper = new List<WrapperClass>(); for(WrapperClass w : listWrapper) { count++; if(w.checked) { if(w.con.id==null) { temp.add(count-1); } else listconForDel.add(w.con); } else { listTempWrapper.add(w); } } if(temp.size()>0){ for(integer i=temp.size();i>0;i--) { listWrapper.remove(temp[i-1]); } } if(listconForDel.size() > 0) { delete listconForDel; listWrapper = listTempWrapper; } } public void selectAll() { if(allchecked) { for(WrapperClass w : listWrapper) { w.checked = true; } } else { for(WrapperClass w : listWrapper) { w.checked = false; } } } public void AddRow() { contact c = new contact(); listWrapper.add(new WrapperClass(c)); } public void insertnewrows() { list<contact> conts=new list<contact>(); for(wrapperclass wrap:listwrapper){ if(wrap.checked) conts.add(wrap.con); } upsert conts; } }
vf page:
<apex:page controller="DeletingCheckedRowsController"> <apex:pagemessages /> <apex:form > <apex:pageBlock id="pb"> <apex:pageBlockTable value="{!listWrapper}" var="w" id="pbt"> <apex:column > <apex:facet name="header"> <apex:inputCheckbox value="{!allchecked}"> <apex:actionSupport reRender="pb" action="{!selectAll}" event="onchange"/> </apex:inputCheckbox> </apex:facet> <apex:inputCheckbox value="{!w.checked}"/> </apex:column> <apex:column headerValue="Contact First Name"> <apex:inputfield value="{!w.con.firstName}"/> </apex:column> <apex:column headerValue="Contact Last Name"> <apex:inputfield value="{!w.con.lastName}"/> </apex:column> <apex:column headerValue="Contact Email"> <apex:inputfield value="{!w.con.email}"/> </apex:column> <apex:column headerValue="Contact Phone Number"> <apex:inputfield value="{!w.con.phone}"/> </apex:column> </apex:pageBlockTable> <br/><apex:commandLink value="Add Row" action="{!Addrow}" reRender="pbt"/> <apex:pageBlockButtons > <apex:commandButton value="Save Selected rows" action="{!insertnewrows}" reRender="pbt"/> <apex:commandButton value="Delete Selected" action="{!del}" reRender="pbt"/> </apex:pageBlockButtons> </apex:pageBlock> </apex:form> </apex:page>
If you see I have assigned the row index to the title of inputCheckbox.. you can get from there. But not sure how we can use these index in apex calss because if you remove the value from one index then the index of the list get reset and other index will not work.
Thanks.
All Answers
If you see I have assigned the row index to the title of inputCheckbox.. you can get from there. But not sure how we can use these index in apex calss because if you remove the value from one index then the index of the list get reset and other index will not work.
Thanks.
thanks