3

I have a visualforce page with a table in it with three lines that the user can fill in... it seems now that 3 lines is not enough, some users wanting up to ten. I do not want to add ten lines if I can avoid it I would rather there was an option to add an extra line to the table if the end user needs to..

I have no idea how to go about this any help would be appreciated

See image for example of what I would like :-

enter image description here

   <tr valign="top">
                            <td>1*</td>
                            <td><apex:inputText value="{!sfw.serialNumber1}" id="serialNumber1" styleclass="imInput imInput_0"/></td>
                            <td><apex:inputText value="{!sfw.modelNumber1}" id="modelNumber1" styleclass="imInput imInput_0"/></td>
                            <td><apex:inputTextArea value="{!sfw.errorDescription1}" id="errorDescription1" styleclass="imInput imInput_0" rows="4" cols="15" style="height: 60px; " /></td>
                        </tr>                            
                        <tr valign="top">
                            <td>2</td>
                            <td><apex:inputText value="{!sfw.serialNumber2}" id="serialNumber2" styleclass="imInput imInput_0"/></td>
                            <td><apex:inputText value="{!sfw.modelNumber2}" id="modelNumber2" styleclass="imInput imInput_0"/></td>
                            <td><apex:inputTextArea value="{!sfw.errorDescription2}" id="errorDescription2" styleclass="imInput imInput_0" rows="4" cols="15" style="height: 60px; " /></td>
                        </tr>
                        <tr valign="top">
                            <td>3</td>
                            <td><apex:inputText value="{!sfw.serialNumber3}" id="serialNumber3" styleclass="imInput imInput_0"/></td>
                            <td><apex:inputText value="{!sfw.modelNumber3}" id="modelNumber3" styleclass="imInput imInput_0"/></td>
                            <td><apex:inputTextArea value="{!sfw.errorDescription3}" id="errorDescription3" styleclass="imInput imInput_0" rows="4" cols="55" style="height: 60px; " /></td>
                        </tr>
                    </table><br/>

Controller

public with sharing class ServiceFormController {

  public ServiceFormWrapper sfw { get; set; }
   public ServiceFormEmailHandler sfeh { get; set; }

public ServiceFormController() {
  sfw = new ServiceFormWrapper();
    sfw.language = Apexpages.currentPage().getParameters().get('lang');
    if (sfw.language != null) {
      sfw.language = sfw.language.toLowerCase();
    }
    sfw.processed = 'false';
    sfw.hasErrors = false;
    sfw.imageFile = new Attachment();
    setInvalidLanguageCode();
    setAgreeToConditions();
}

  private void setInvalidLanguageCode() {
    if (sfw.language != 'be' && sfw.language != 'en' && sfw.language !      = 'es' && sfw.language != 'de' && sfw.language != 'fr'      && sfw.language != 'it' && sfw.language != 'nl' && sfw.language != 'pt') {
             sfw.language = 'en';
    }
  }

  // The following languages do not have an Agree to Conditions checkbox on the form. Therefore it is set to TRUE by default.
  private void setAgreeToConditions() {
    if (sfw.language == 'de' || sfw.language == 'it' || sfw.language == 'es' || sfw.language == 'pt') {
        sfw.agreeToConditions = true;
    }
  }

public PageReference save() {

    sfw.hasErrors = false;
    if (sfw.imageFile == NULL) {
      sfw.imageFile = new Attachment();
    }

// Check that all mandatory fields have been filled in on the form. If not, display an error
    if (!ServiceFormBusinessRules.validateMandatoryFields(sfw)) {
      initiateError(Label.Error_Message);
        return null;
    }

    // Validate that the User does check the Agree to Conditions field (NOTE: not applicable for DE, IT, ES and PT) 
    if (!sfw.agreeToConditions) {
      initiateError(Label.Error_Message);
  return null;
    } 

// Validate the file if a file has been uploaded
if (sfw.imageFile.Name != NULL) {
    // Validate that the file is less than 10MB in size
    if (!ServiceFormBusinessRules.validateImageFileSize(sfw)) {
      initiateError(Label.Invalid_File_Size);
        return null;
    }
}

// Validate that the email address entered is a valid email address 
    if (sfw.emailAddress != '' && !Pattern.matches('^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$', sfw.emailAddress)) {
        initiateError(Label.Email_Error_Message);
        return null;
    }

    if (sfw.serviceType == 'Storing melden' &&  ((!(sfw.serialNumber2 != '' && sfw.modelNumber2 != '' && sfw.errorDescription2 != '') && !(sfw.serialNumber2 == '' && sfw.modelNumber2 == '' && sfw.errorDescription2 == '')) || (!(sfw.serialNumber3 != '' && sfw.modelNumber3 != '' && sfw.errorDescription3 != '') && !(sfw.serialNumber3 == '' && sfw.modelNumber3 == '' && sfw.errorDescription3 == '')))) {
        initiateError(Label.Report_Fault_Error_Message);
        return null;    
    }

    sfw.errorMessage = '';
ServiceFormBusinessRules.populateEnteredData(sfw);
    sfw.processed = 'true';
sfeh = new ServiceFormEmailHandler(sfw);
sfeh.sendServiceAndUserEmail();
    sfw.imageFile = null;
    return null;
}

public PageReference reset() {
  sfw = new ServiceFormWrapper();
    return null;
}

private void initiateError(String errorMessageLabel) {
  sfw.hasErrors = true;
  sfw.processed = 'false';
    sfw.imageFile = null;
  sfw.errorMessage = errorMessageLabel;
}
}

Wrapper

public with sharing class ServiceFormWrapper {

public String language { get; set; }

public String errorMessage { get; set; }
public String processed { get; set; }
public boolean hasErrors { get; set; }    

public String serviceTypeTranslated { get; set; }
public String problemReportedTranslated { get; set; }
public String haveContractTranslated { get; set; }
public String callBackTranslated { get; set; }
public String equipmentAmountTranslated { get; set; }

public String companyName { get; set; }
public String contactPerson { get; set; }
public String addressStreet { get; set; }
public String addressCodeCity { get; set; }
public String phoneNumber { get; set; }
public String emailAddress { get; set; }
public String openingTimes { get; set; }
public String serviceType { get; set; }
public String serialNumber1 { get; set; }
public String modelNumber1 { get; set; }
public String errorDescription1 { get; set; }
public String serialNumber2 { get; set; }
public String modelNumber2 { get; set; }
public String errorDescription2 { get; set; }
public String serialNumber3 { get; set; }
public String modelNumber3 { get; set; }
public String errorDescription3 { get; set; }
public String reference { get; set; }
public String poValue { get; set; }
public String problemReported { get; set; }
public String equipmentAmount { get; set; }
public String numberOfCardio { get; set; }
public String numberOfStrength { get; set; }
public String haveContract { get; set; }
public String request { get; set; }
public String callBack { get; set; }
public String sparePartItemCode1 { get; set; }
public String sparePartItemQuantity1 { get; set; }
public String sparePartItemCode2 { get; set; }
public String sparePartItemQuantity2 { get; set; }
public String sparePartItemCode3 { get; set; }
public String sparePartItemQuantity3 { get; set; }
public String sparePartItemCode4 { get; set; }
public String sparePartItemQuantity4 { get; set; }
public String sparePartItemCode5 { get; set; }
public String sparePartItemQuantity5 { get; set; }
public String sparePartItemCode6 { get; set; }
public String sparePartItemQuantity6 { get; set; }
public String sparePartItemCode7 { get; set; }
public String sparePartItemQuantity7 { get; set; }
public String sparePartItemCode8 { get; set; }
public String sparePartItemQuantity8 { get; set; }
public String sparePartItemCode9 { get; set; }
public String sparePartItemQuantity9 { get; set; }
public String sparePartItemCode10 { get; set; }
public String sparePartItemQuantity10 { get; set; }
public boolean agreeToConditions { get; set; }

public String comments { get; set; }

public Attachment imageFile { get; set; }

}
  • This can be done within your apex controller. Can you post the relevant apex code? – Jeremy Nottingham Apr 29 '15 at 14:47
  • I have posted the Controller and Wrapper for the page – user18852 Apr 29 '15 at 15:00
3

Bob Buzzard wrote a great blog about this several years ago. http://bobbuzzard.blogspot.com/2011/07/managing-list-of-new-records-in.html

It describes how to implement the add rows type of functionality.

0

Oops! I am little late to this but still here is my code.

VF mark up

<apex:page controller="Exponential" >
<apex:form >
    <apex:actionFunction name="newItem" action="{!addNew}" reRender="tab1" oncomplete="alert('done');"/>
  <apex:pageBlock title="Viewing Accounts">
    <apex:pageBlockTable id="tab1" value="{!recList}" var="rec">
    <apex:column >
    <apex:inputText id="one" value="{!rec.one}"/>
    </apex:column>
    <apex:column >
    <apex:inputText id="two" value="{!rec.two}"/>
    </apex:column>
    </apex:pageBlockTable>
    <apex:commandButton onclick="myScript();return false;" value="Add one"/>

  </apex:pageBlock>
  <script>
  function myScript()
  {
  alert("called");
  newItem();
  }
  </script>
  </apex:form>
</apex:page>

Controller:

public class Exponential {
    public OneRecord rec{get;set;}
    public List<OneRecord> recList{get;set;}
public Exponential(ApexPages.StandardController std)
{

}
    public Exponential()
{
    recList = new List<OneRecord>();
    OneRecord new1 = new OneRecord();
    OneRecord new2 = new OneRecord();
    OneRecord new3 = new OneRecord();
        recList.add(new1);
    recList.add(new2);
    recList.add(new3);
}
  public  class OneRecord{
        public String one{get;set;}
        public String two{get;set;}
        public OneRecord()
        {

        }
    }
    public PageReference addNew()
    {
        OneRecord newone = new OneRecord();
        recList.add(newone);
        return null;
    }

}

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.