0

I have a table of 3 rows and 3 columns in which the data I enter should be saved in the account object. But what I am facing is , only the 3rd row data is inserted not the first two.

My controller code :

public class accountcontroller {


    public account acc {
        get;
        set;
    }

    public accountcontroller() {
        acc = new account();
    }

    public PageReference insertNewItem() {

        insert acc;
        //reset public property for new insert
        acc = new account();

        return null;
    }
}

Visualforce Page

<apex:page controller="accountcontroller">
  <apex:form >
    <apex:pageBlock >
      <apex:pageBlockSection >
        <apex:dataTable value="{!acc}"   var="a" cellpadding="4" border="1" >
          <apex:column headervalue="Account name"  width="20">
            <apex:inputField value="{!acc.Name}"/><apex:inputField value="{!acc.Name}"/><apex:inputField value="{!acc.Name}"/>
          </apex:column>
          <apex:column headervalue="Account site" width="20" >
            <apex:inputField value="{!acc.site}"/><apex:inputField value="{!acc.site}"/><apex:inputField value="{!acc.site}"/>
          </apex:column>
          <apex:column headervalue="Account phone" width="20" >
            <apex:inputField value="{!acc.phone}"/><apex:inputField value="{!acc.phone}"/><apex:inputField value="{!acc.phone}"/>
          </apex:column>
        </apex:dataTable>
      </apex:pageBlockSection>
      <apex:pageBlockButtons location="BOTTOM">
        <apex:commandButton action="{!insertNewItem}" value="save"/>
      </apex:pageBlockButtons>
    </apex:pageBlock>
  </apex:form>
</apex:page>

Please provide me a best solution. Thanks in advance !!!

1

You should update your code with this:

<apex:page controller="accountcontroller">
 <apex:form >
   <apex:pageBlock >
     <apex:pageBlockSection >

        <apex:dataTable value="{!acc}"   var="a" cellpadding="4" border="1" rows="3">
                <apex:column headervalue="account name"  width="20">
                     <apex:inputField value="{!a.Name}"/>
                </apex:column>

                 <apex:column headervalue="account site" width="20" >
                        <apex:inputField value="{!a.site}"/>
                  </apex:column>

                  <apex:column headervalue="account phone" width="20" >
                        <apex:inputField value="{!a.phone}"/> 
                  </apex:column>
        </apex:dataTable>
     </apex:pageBlockSection>

     <apex:pageBlockButtons location="BOTTOM">
        <apex:commandButton action="{!insertNewItem}" value="save"/>
    </apex:pageBlockButtons>

 </apex:pageBlock>
 </apex:form>
</apex:page>

class:

public class accountcontroller{

 public list<account> acc{get;set;}

 public accountcontroller(){
    acc= new list<account>();
    for(integer i=0 ; i<3 ;i++){
      account ac = new account();
      acc.add(ac);
    }
 }

  public PageReference insertNewItem() {

          insert acc;

      acc= new list<account>();
      for(integer i=0 ; i<3 ;i++){
         account ac = new account();
         acc.add(ac);
    }
      return null;
  }
}
  • it is working fine ..thank you..but after clicking the submit button the data table row disappears.only the data table heading is available..further I cant insert data again.. – prabu Jul 31 '15 at 4:34
  • see updated controller class ..... – Amit Chaudhary Jul 31 '15 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.