function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Srilakshmi B SSrilakshmi B S 

How to create a table which contains the data entered in the input text?

I have created an input form. The input form fields are Name, Status, Start time and End time which are the fields of Deliverable object. On click of Add button the values entered in the input text fields have to be displayed in the table. Here is the code i have written:

Controller:

public with sharing class TBN_ProjectDeliverable {

public List<Project__c> lstProjects = new List<Project__c>();
public List<Delivarable__c> lstDeliverables {get; set;}
public Delivarable__c objDelivarable = new Delivarable__c();
public String selectedProject {get; set;}
public String inputName {get; set;}
public String inputStatus {get; set;}
public String inputStartTime {get; set;}
public String inputEndTime {get; set;}
public boolean showInputForm {get; set;}
public boolean showTable {get; set;}
public Integer counter = 0;

public TBN_ProjectDeliverable(){

    lstProjects =   ([  SELECT Name
                        FROM Project__c
                        WHERE Status__c = 'Active'
                    ]);
    lstDeliverables = new List<Delivarable__c>();
}

public List<SelectOption> getActiveProjects(){

    List<SelectOption> lstSelectOptions = new List<SelectOption>();

    for(Project__c objProject: lstProjects) {

        lstSelectOptions.add(new SelectOption(objProject.Name , objProject.Name));
    }

    return lstSelectOptions;
}

public void btnGo() {

    showInputForm = true;
}

public Delivarable__c getDelivarable {

    get{

        System.debug('=====objDelivarable======'+objDelivarable);
        return objDelivarable;

    } set;
}

public pageReference addDataToTable() {

    showTable = true;
    System.debug('=====objDelivarable======'+objDelivarable);
    lstDeliverables.add(objDelivarable);
    System.debug('=====lstDeliverables======'+lstDeliverables);
    System.debug('=====lstDeliverables.Name======'+lstDeliverables[0].Name);

    return null;
}

public void btnEdit(){

}

public void btnSave() {

    Database.insert(objDelivarable);
}

public void btnCancel() {

}
}

Vf page:

<apex:page Controller="TBN_ProjectDeliverable" sidebar="false" tabStyle="Project__c">
<apex:form >
    <apex:pageBlock title="Project Deliverable" id="thePageBlock">
        <apex:pageBlockSection >
            <apex:selectList label="Active Projects:" value="{!selectedProject}" size="1">
                <apex:selectOptions value="{!ActiveProjects}"/>
            </apex:selectList>
            <apex:commandButton action="{!btnGo}" value="Go"/>
        </apex:pageBlockSection>

        <apex:pageBlockSection title="Create deliverable record" rendered="{!showInputForm}">
            <apex:inputfield value="{!getDelivarable.Name}"/>
            <apex:inputfield value="{!getDelivarable.Status__c}"/>
            <apex:inputfield value="{!getDelivarable.Start_Time__c}"/>
            <apex:inputfield value="{!getDelivarable.End_time__c}"/>
            <apex:commandButton action="{!addDataToTable}" value="Add" rerender="thePageBlockTable, thePageBlock"/>
        </apex:pageBlockSection>

        <apex:pageBlockSection title="Table of deliverable records" rendered="{!showTable}" columns="1" id="thePageBlockTable">
            <apex:pageBlockTable value="{!lstDeliverables}" var="objLstDeliverables" >
                <apex:column headervalue="Name">
                    <apex:outputText value="{!objLstDeliverables.Name}"/>
                </apex:column>
                <apex:column headervalue="Status">
                    <apex:outputText value="{!objLstDeliverables.Status__c}"/>
                </apex:column>
                <apex:column headervalue="Start Time">
                    <apex:outputText value="{!objLstDeliverables.Start_Time__c}"/>
                </apex:column>
                <apex:column headervalue="End Time">
                    <apex:outputText value="{!objLstDeliverables.End_time__c}"/>
                </apex:column>
                <apex:column headervalue="Edit">
                    <apex:commandLink value="Edit" action="{!}"/>
                </apex:column>
            </apex:pageBlockTable>
            <apex:commandButton action="{!btnSave}" value="Save" />
            <apex:commandButton action="{!btnCancel}" value="Cancel"/> 
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>       
</apex:page>

I am getting the input form data in the table. But on re-entering the form data with new values, the previous data is getting overriden in the table. Please help to to solve this problem.
Hargobind_SinghHargobind_Singh
You can do 2 things:

1. When adding to list, don't add your objDelivarable directly, as you are using the same object to enter data again, so old one would be over-written, try this:

public pageReference addDataToTable() {

	    showTable = true;
		//create new instance 
	    Delivarable__c tempObj = new Delivarable__c();
	    tempObj.Name = objDeliverable.name; 
	    tempObj.Status__c = objDeliverable.Status__c; 
	    tempObj.Start_Time__c = objDeliverable.Start_Time__c; 
	    tempObj.End_time__c = objDeliverable.End_time__c;
	    //add new instance to list  
	    lstDeliverables.add(tempObj);
	
	    return null;
	}

2. You can try using the String variables in InputFields, and before adding to list, create a new instance of object and add to list.

See if this works for you.


Srilakshmi B SSrilakshmi B S
I changed the function by instantiating  Delivarable__c  after after adding objDelivarable into the list. Now it is working fine for me.                                                   public pageReference addDataToTable() {

    showTable = true;
    System.debug('=====objDelivarable======'+objDelivarable);
    lstDeliverables.add(objDelivarable);
    System.debug('=====lstDeliverables======'+lstDeliverables);
    System.debug('=====lstDeliverables.Name======'+lstDeliverables[0].Name);

    // create a new instance
    objDelivarable = new Delivarable__c();

    return null;
}


Srilakshmi B SSrilakshmi B S
public void btnEdit(){
           
            System.debug('=====rowNumber===='+rowNumber);
            objDelivarable = lstDeliverables.get(rowNumber);
            System.debug('=====lstDeliverables.get(rowNumber)===='+objDelivarable);
            lstDeliverables.remove(rowNumber);
}
I wrote this function for edit button. When i click on edit, the particular row data has to be put back to the input form. In this code i am getting the data in objDelivarable. But i am not getting how to place it back to the form. Please help me. 
Srilakshmi B SSrilakshmi B S
I got the solution for edit button.

public void btnEdit(){
		
		System.debug('=====rowNumber===='+rowNumber);
		objDelivarable = lstDeliverables.get(rowNumber);
		System.debug('=====lstDeliverables.get(rowNumber)===='+objDelivarable);
		lstDeliverables.remove(rowNumber);
		getDelivarable = objDelivarable;
		System.debug('=====getDelivarable.Name===='+getDelivarable.Name);
		//return objDelivarable;
}

rerender the form on click of edit.