+ Start a Discussion
VSK98VSK98 

Edit the record in vf page

Hi,

Here i am using the wrapper class to dispaly records ............
My concern : I want to edit the record after the display how ?

My VF Page

<apex:page controller="CheckAllUsingJavascriptController"> <script type="text/javascript"> function selectAllCheckboxes(obj,receivedInputID){ var inputCheckBox = document.getElementsByTagName("input"); for(var i=0; i<inputCheckBox.length; i++){ if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){ inputCheckBox[i].checked = obj.checked; } } } </script> <apex:form > <apex:pageBlock > <apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" id="table" title="All Accounts"> <apex:column > <apex:facet name="header"> <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/> </apex:facet> <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/> </apex:column> <apex:column value="{!accWrap.acc.Name}" /> <apex:column value="{!accWrap.acc.BillingState}" /> <apex:column value="{!accWrap.acc.Phone}" /> </apex:pageBlockTable> </apex:pageBlock> </apex:form> </apex:page>


My Controller

public class CheckAllUsingJavascriptController {
    public List<wrapAccount> wrapAccountList {get; set;}
     
    public CheckAllUsingJavascriptController(){
        if(wrapAccountList == null) {
            wrapAccountList = new List<wrapAccount>();
            for(Account a: [select Id, Name,BillingState, Website, Phone from Account limit 10]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapAccountList.add(new wrapAccount(a));
            }
        }
    }
     
    public class wrapAccount {
        public Account acc {get; set;}
        public Boolean selected {get; set;}
 
        //This is the contructor method. When we create a new wrapAccount object we pass a Account that is set to the acc property. We also set the selected value to false
        public wrapAccount(Account a) {
            acc = a;
            selected = false;
        }
    }
}

Advance Thanks
Siv
 
pconpcon
What do you mean you want to edit after displaying?   You have input areas there and that lets you edit them.  Are you talking about updating your controller to handle saving the records?  If so then you'll want to add something like a apex:commandButton bound to an update method in your controller.  The update method would look something like
 
public PageReference updateAccounts() {
    // Code to get updated accounts from wrapper
    update accountList;

    return new PageReference('/');
}
Prasad Avala(SFDC)Prasad Avala(SFDC)
Hi Siva,
From the pageblock table display change state or phone number as your wish.

Then wrapAccountList contains all the updated values(no need to do anything from your end to capture the updated values(by default  wrapAccountList holds the new values)
Now as Pcon mentioned you should have action function (like save in satndard functionality) to update the records.

so create a pageblock button and implement this function in controller exactly as pcon mentioned.

Below is the example for your reference , i did not compile(please correct if any minor sytax issues)


VF page:
<apex:page controller="CheckAllUsingJavascriptController"> <script type="text/javascript"> function selectAllCheckboxes(obj,receivedInputID){ var inputCheckBox = document.getElementsByTagName("input"); for(var i=0; i<inputCheckBox.length; i++){ if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){ inputCheckBox[i].checked = obj.checked; } } } </script> <apex:form > <apex:pageBlock >
 <apex:pageBlockButtons location="both" id="mubut">
                <apex:commandButton value="Save" action="{!save}" id="savebutton"  </apex:commandButton>
                <apex:commandButton value="Cancel" action="{!cancel}" id="cancelbutton" </apex:commandButton>
            </apex:pageBlockButtons>
<apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" id="table" title="All Accounts"> <apex:column > <apex:facet name="header"> <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/> </apex:facet> <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/> </apex:column> <apex:column value="{!accWrap.acc.Name}" /> <apex:column value="{!accWrap.acc.BillingState}" /> <apex:column value="{!accWrap.acc.Phone}" /> </apex:pageBlockTable> </apex:pageBlock> </apex:form> </apex:page>\


controller:
public class CheckAllUsingJavascriptController {
    public List<wrapAccount> wrapAccountList {get; set;}
     
    public CheckAllUsingJavascriptController(){
        if(wrapAccountList == null) {
            wrapAccountList = new List<wrapAccount>();
            for(Account a: [select Id, Name,BillingState, Website, Phone from Account limit 10]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapAccountList.add(new wrapAccount(a));
            }
        }
    }
     
    public class wrapAccount {
        public Account acc {get; set;}
        public Boolean selected {get; set;}
 
        //This is the contructor method. When we create a new wrapAccount object we pass a Account that is set to the acc property. We also set the selected value to false
        public wrapAccount(Account a) {
            acc = a;
            selected = false;
        }
    }
 public PageReference save() {
update wrapAccountList;

return null;
}
Prasad Avala(SFDC)Prasad Avala(SFDC)
sorry forgot to implement cancel function very simple: 

below is the complete controller code:


public class CheckAllUsingJavascriptController {
    public List<wrapAccount> wrapAccountList {get; set;}
     
    public CheckAllUsingJavascriptController(){
        if(wrapAccountList == null) {
            wrapAccountList = new List<wrapAccount>();
            for(Account a: [select Id, Name,BillingState, Website, Phone from Account limit 10]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapAccountList.add(new wrapAccount(a));
            }
        }
    }
     
    public class wrapAccount {
        public Account acc {get; set;}
        public Boolean selected {get; set;}
 
        //This is the contructor method. When we create a new wrapAccount object we pass a Account that is set to the acc property. We also set the selected value to false
        public wrapAccount(Account a) {
            acc = a;
            selected = false;
        }
    }
 public PageReference save() {
update wrapAccountList;

return null;
}
 public PageReference Cancel() {

return null;
​}
}