+ Start a Discussion
Lukesh KarmoreLukesh Karmore 

my edit buttion is not working

my edit commandLink i s not working , when click no response  check the below code  help me to find error:
controller class:
public class contactSaveandEditClass {
    public List<wrapperclass> contactList{get; set;}
    public integer RowIndex{get; set;}
    public wrapperclass objwrapper;
    public  contactSaveandEditClass(){
        list<contact> contacts=[select id,FirstName,LastName,Phone,Title from contact];
        contactList=new list<wrapperclass>();
    for(integer i=0;i<contacts.size(); i++){
        objwrapper=new wrapperclass(); 
        objwrapper.con=contacts[i];
        objwrapper.doEdit=false;
        objwrapper.RowNo=contacts.size();
        contactList.add(objwrapper);
    }
    }
        public PageReference  EditMethod(){
            if(contactList.size()>RowIndex){
                contactList[RowIndex].doEdit=true;
            }
            return null;
        }
         public pageReference SaveMethod(){
            if(contactList.size()>RowIndex){
                contactList[RowIndex].doEdit=false;
            }
            return null;
        }
         public class wrapperclass{
        public contact con{get; set;}
        public boolean doEdit{get; set;}
        public integer RowNo{get; set;}
    }
}

VF class:
<apex:page controller="contactSaveandEditClass">
    <apex:sectionHeader title="Contacts"/>
    <apex:form >
        <apex:pageblock id="acc" title="Contact List">
            <apex:pageBlockSection >
                <apex:pageblocktable value="{!contactList}" var="a">
                    <apex:column headerValue="Action">
                         <apex:commandlink action="{!EditMethod}" value="Edit" reRender="acc"  rendered="{!NOT(a.doEdit)}">
                             <apex:param Name="rowNumber"  Value="{!a.RowNo}"  assignTo="{!RowIndex}"/>
                        </apex:commandlink>
                     <apex:commandlink action="{!SaveMethod}" value="Save" reRender="acc"  rendered="{!(a.doEdit)}">
                             <apex:param Name="rowNumber"  Value="{!a.RowNo}"  assignTo="{!RowIndex}"/>
                        </apex:commandlink>
                    </apex:column>
                    <apex:column headerValue="FirstName" >
                        <apex:outputField value="{!a.con.FirstName}" rendered="{!NOT(a.doEdit)}" />
                        <apex:inputField value="{!a.con.FirstName}" rendered="{!(a.doEdit)}"/>
                     </apex:column>
                     <apex:column headerValue="LastName" >
                    <apex:outputField value="{!a.con.LastName}" rendered="{!NOT(a.doEdit)}" />
                     <apex:inputField value="{!a.con.LastName}" rendered="{!(a.doEdit)}"/>
                     </apex:column>
                    <apex:column headerValue="Phone">
                     <apex:outputField value="{!a.con.Phone}"  rendered="{!NOT(a.doEdit)}"/>
                     <apex:inputField value="{!a.con.Phone}" rendered="{!(a.doEdit)}"/>
                    </apex:column>
                    <apex:column headerValue="Title" > 
                     <apex:outputField value="{!a.con.Title}" rendered="{!NOT(a.doEdit)}"/>
                     <apex:inputField value="{!a.con.Title}"  rendered="{!(a.doEdit)}"/>
                    </apex:column>
                </apex:pageblocktable>
             </apex:pageBlockSection>
         </apex:pageblock>
     </apex:form>
</apex:page>
Thank You
Vishwajeet kumarVishwajeet kumar
Hello,
Looks like issue is due to below code logic : 

public  contactSaveandEditClass(){
        list<contact> contacts=[select id,FirstName,LastName,Phone,Title from contact];
        contactList=new list<wrapperclass>();
    for(integer i=0;i<contacts.size(); i++){
        objwrapper=new wrapperclass(); 
        objwrapper.con=contacts[i];
        objwrapper.doEdit=false;
        objwrapper.RowNo=contacts.size();       // May be RowNo should be assigned as i.
        contactList.add(objwrapper);
    }
    }

public PageReference  EditMethod(){
            if(contactList.size()>RowIndex){     // ISSUE : This condition will be always false due to RowNo value assigned as size of contact list, they will be equal.
                contactList[RowIndex].doEdit=true;
            }
            return null;
        }
         public pageReference SaveMethod(){
            if(contactList.size()>RowIndex){    // ISSUE: This condition will be always false due to RowNo value assigned as size of contact list, they will be equal.
           contactList[RowIndex].doEdit=false;   
            }
            return null;
        }



Thanks
Lukesh KarmoreLukesh Karmore
Ya i need  contactlist.size();  there
Vishwajeet kumarVishwajeet kumar
Hello,
May be i was not clear, this is what i mean: 
Consider contacts or ContactList is of size 5, for each objwrapper RowNo will be assigned to 5 and from visualforce when EditMethod or SaveMethod is called it will assign RowIndex to 5(= RowNo ) always. so if condition (contactList.size() > RowIndex) => (5 > 5) will be never true. and it will not update the field doEdit.

Thanks
 
Lukesh KarmoreLukesh Karmore
So we using for loop we process each account in (objwrapper=new wrapperclass(); 
        objwrapper.con=contacts[i];)
So if   objwrapper.RowNo=contacts.size();    
If contacts.size =5  then all the contacts in objwrapper having row upto 5 ok. Understand this condition 1st
We create one variable rowIndex (which denote position of each row)
contactList.size()>rowIndex  
Let  contactList.size()=5
And we edit 2nd row so rowIndex=2
5>2 this is the condition becomes true
Vishwajeet kumarVishwajeet kumar
Hello,
RowIndex is getting assigned from page using objwrapper.RowNo, which is assigned as contactList.size() or 5 as per example.

 objwrapper.RowNo = contacts.size();       // May be RowNo should be assigned as i to avoid 5 > 5.

<apex:pageblocktable value="{!contactList}" var="a">
                    <apex:column headerValue="Action">
                         <apex:commandlink action="{!EditMethod}" value="Edit" reRender="acc"  rendered="{!NOT(a.doEdit)}">
                             <apex:param Name="rowNumber"  Value="{!a.RowNo}"  assignTo="{!RowIndex}"/>      <!--a.RowNo is 5 always-->
                        </apex:commandlink>
                     <apex:commandlink action="{!SaveMethod}" value="Save" reRender="acc"  rendered="{!(a.doEdit)}">
                             <apex:param Name="rowNumber"  Value="{!a.RowNo}"  assignTo="{!RowIndex}"/>      <!-- a.RowNo is 5 always-->
                        </apex:commandlink>
                    </apex:column>


Thanks.