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
Anil kumar GorantalaAnil 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:
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>

 
Best Answer chosen by Anil kumar Gorantala
sslodhi87sslodhi87
You can get the index of each row by using the apex:variable, here is code for same.
<apex:page controller="DeletingCheckedRowsController">
<apex:pagemessages />
    <apex:form >
        <apex:pageBlock id="pb">
		<apex:variable value="{!0}" var="rowNum"/>
           <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}" title="{!rowNum}"/>
				   <apex:variable var="rowNum" value="{!rowNum + 1}"/>
               </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

sslodhi87sslodhi87
You can get the index of each row by using the apex:variable, here is code for same.
<apex:page controller="DeletingCheckedRowsController">
<apex:pagemessages />
    <apex:form >
        <apex:pageBlock id="pb">
		<apex:variable value="{!0}" var="rowNum"/>
           <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}" title="{!rowNum}"/>
				   <apex:variable var="rowNum" value="{!rowNum + 1}"/>
               </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.
 
This was selected as the best answer
Anil kumar GorantalaAnil kumar Gorantala
ya thank u.. we can do this by iterating list from backwards even if we remove last one previous one's index does'nt change. thank you again
sslodhi87sslodhi87
Ohhk cool go it.

thanks