+ Start a Discussion
JessBJessB 

VisualForce Data Table: Add a Row

This is probably a simple question, and I keep searching for an answer, but I'm getting more complicated questions/answers than I need.

 

I know how to insert a new column in VF <apex:column> but how do you do it for adding a row? So far, I can't figure out how to do this without creating a new data table for every row.

GlynAGlynA

Jess,

 

The <apex:pageBlockTable>  component takes as it "value" a reference to an iterable collection (i.e. a list) of records, instances or primitives to display.  There will be one row in the table for each element in the list.  To add another row to the table, you need to add another element to the list that the table is iterating.

 

You can also add <apex:facet> components to a table to specify the header, footer and caption of the table.

 

The VF developers guide gives this example:

 

<apex:page standardController="Account">
    <apex:pageBlock title="My Content">
        <apex:pageBlockTable value="{!account.Contacts}" var="item">
            <apex:column value="{!item.name}"/> 
        </apex:pageBlockTable> 
    </apex:pageBlock> 
</apex:page>

In this example, "{!account.Contacts}" is a list of all the contacts associated with the account, so the table will have one row for each contact.

 

If you have a specific use case, I will be happy to help you with the controller logic to create the list of items to display in your table.

 

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!

 

-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator

Daniel Fuller017920529519823092Daniel Fuller017920529519823092
Glyn,

I just came across this post and could use your help if you wouldn't mind. On the VF page I am building, the page launches from the Opportunity Detail screen and automatically pulls in Opportunity Detail information with Product Detail information associated to that opportunity. On this page, all entities can be edited and updated in the database all from this page. Everyone on the page is working correctly except my being able to add a new row to the pageblocktable with the "Add Product" button I have added. I understand the concept behind adding a new row to the pageblocktable. I am just not sure what syntax to use. My VF markup and Apex code are below for what I have so far. Any help would be greatly appreciated!


VisualForce Markup:

<apex:page standardController="Opportunity" extensions="LineItem_Controller">
<apex:form >

<style type="text/css">
        .ProductNameField { width: 250px; }
</style>

<apex:sectionHeader title="Create/Edit Opportunity" />
<apex:pageblock id="OpportunityandProduct_PageBlock">
<apex:pageblocksection columns="1" title="Opportunity Detail">
   
    <apex:pageblocktable value="{!OpportunityInfo}" var="opp">
       
        <apex:column headerValue="Opportunity Name">
            <apex:inputField value="{!opp.name}"/>
        </apex:column>
       
        <apex:column headerValue="Opportunity Owner">
            <apex:inputfield value="{!opp.ownerID}" />
        </apex:column>
       
        <apex:column headerValue="Created Date">
           <apex:inputfield value="{!opp.createddate}" />
        </apex:column>
       
        <apex:column headerValue="Stage">
            <apex:inputField value="{!opp.stagename}"/>
        </apex:column>
        <apex:column value="{!opp.amount}" />
   </apex:pageblocktable>
</apex:pageblocksection>

<apex:pageblocksection columns="1" title="Product Detail">
    <apex:pageblocktable value="{!ProductInfo}" var="prod">
       
        <apex:column headerValue="Product Name">
            <apex:inputField value="{!prod.pricebookentry.product2.name}" styleClass="ProductNameField"/>
        </apex:column>
       
        <apex:column headerValue="Quantity">
            <apex:inputField value="{!prod.quantity}"/>
        </apex:column>
       
        <apex:column headerValue="Unit Price">
            <apex:inputField value="{!prod.unitprice}"/>
        </apex:column>
        <apex:column value="{!prod.TotalPrice}" headerValue="Total Price" />
   </apex:pageblocktable>
</apex:pageblocksection>


<apex:pageBlockButtons >
       <apex:commandButton value="Save" action="{!save}"/>
       <apex:commandButton value="Cancel" action="{!cancel}"/>
    </apex:pageBlockButtons>
</apex:pageblock>

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




Controller Extension Code:

public class LineItem_Controller {
   
    //*****Declare GLOBAL variables
    private List <Opportunity> oppList = new List <Opportunity>();
    private List <OpportunityLineItem> prodList = new List <OpportunityLineItem>();
    private List <OpportunityLineItem> addRowList = [SELECT PricebookEntry.Product2.Name, quantity, unitprice, totalprice
                                                    FROM OpportunityLineItem
                                                    WHERE OpportunityID='006i000000H8Abn'];
    public OpportunityLineItem newrecord{get;set;}
    //********
   
     public LineItem_Controller(ApexPages.StandardController controller)
        {
        }
   
    //Get the Opportunity Detail Inforamation from the Opportunity Detail Page and return a list of that information.
    public List <Opportunity> getOpportunityInfo()
    {
        List <Opportunity> opp_InfoList = [SELECT name, ownerID, createddate, stagename, amount
                               FROM Opportunity
                               WHERE id = '006i000000H8Abn'];
        oppList = opp_InfoList;
   
    return opp_infoList;
    }
   
    //Get the Product Detail Information associated with the Opportunity. The WHERE queries whatever Opportunity ID is currently in context.
    public List <OpportunityLineItem> getProductInfo()
    {
        List <OpportunityLineItem> prod_InfoList = [SELECT PricebookEntry.Product2.Name, quantity, unitprice, totalprice
                                                    FROM OpportunityLineItem
                                                    WHERE OpportunityID='006i000000H8Abn'];
        prodList = prod_InfoList;
       
        return prod_InfoList;
    }
  
   
    //****SAVE AND CANCEL METHODS
    public PageReference cancel() {
       PageReference cancelReference = new PageReference('https://na15.salesforce.com/006i000000H8Abn');
        cancelReference.setRedirect(true);
        return cancelReference;
    }

    public PageReference save() {
        update prodList;
        update oppList;
        PageReference reference = new PageReference('https://na15.salesforce.com/006i000000H8Abn');
        reference.setRedirect(true);
        return reference;
    }
   
    //******
   
  //****ADD NEW ROW
 
   public void addRow()
   {
       addRowList.add(newrecord);
   }



}