Search code examples
datatableapex-codevisualforce

Take row data from data table - apex class


I am having one grid (data table). In each row , I am having SAVE button to save the record. I want to write controller code for taking row data on SAVE button click. I am new for apex. Please guide me.

Thank you in advance.


Solution

  • On Page

    <apex:dataTable value="{!accountList}" var="a" >
    <apex:column >
        <apex:column headerValue="Action">
            <apex:commandLink value="Save" action="{!save}" rerender="table" >
                <apex:param name="Id" value="{!a.Id}" />
            </apex:commandLink>
    </apex:column>
    </apex:dataTable>
    

    Controller

    List<accountwrapper> accountList = new List<accountwrapper>();
    
    public class accountwrapper
    {
        public Account acc{get; set;}
        public string id {get; set;}
        public accountwrapper(Account a,string id)
        {
            this.acc = a;
            this.id = id;
        }
    }
    
    public PageReference save()
    {
        string accId = ApexPages.CurrentPage().getParameters().get('id');
        for(accountwrapper accwrapper : accountList){
            if(accwrapper.id == accId)
                upsert accwrapper.acc;
        }
        return null;
    }