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
abi1.2911902284080376E12abi1.2911902284080376E12 

adding a dynamic row to a datatable

Based on a product quantity im displaying product name and price in a datatable. Now at the end of each product list i need a row that should display the sum of the product price. How do i generate this list?

Im using a datatable to display the product name and price. i want to insert  the "total" and "overall total" rows dynamically. please suggest how to do this..

 

Product Name

Total Price

Acer- Liquid Car Charger

7

Acer- Liquid Car Charger

7

Acer- Liquid Car Charger

7

Acer- Liquid Car Charger

7

Acer- Liquid Car Charger

7

Total

35

Acer Neo Touch Leather Case

10

Acer Neo Touch Leather Case

10

Acer Neo Touch Leather Case

10

Acer Neo Touch Leather Case

10

Acer Neo Touch Leather Case

10

total

50

Overall total

85

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You can create a wrapper class to generate the values. I'd imagine the class looks like this:

 

 

// controller
public class theController {
  public class wrapper {
    public wrapper(string rT, product2 p, decimal amt) {
      rowType = rT;
      rowProduct = p;
      rowAmount = amt;
    }
    public string rowType { get; set; }
    public Product2 rowProduct { get; set; }
    public decimal rowAmount { get; set; }
  }
  public list<wrapper> rows { get; set; }
  public opportunity record { get; set; }
  public theController(ApexPages.StandardController controller) {
    record = (Opportunity)controller.getRecord();
    rows = new list<wrapper>();
    decimal subTotal, grandTotal = 0;
    for(OpportunityLineItem oli:[select id,pricebookentry.product2.id,pricebookentry.product2.name,quantity,unitprice from opportunitylineitem where opportunityid = :record.id]) {
      subTotal = 0;
      for(Integer ctr = 0; ctr < oli.quantity; ctr++) {
        subTotal += oli.unitprice;
        rows.add(new wrapper('item',oli.pricebookentry.product2,oli.unitprice));
      }
      grandTotal += subTotal;
      rows.add(new wrapper('sub',null,subTotal));
    }
    rows.add(new wrapper('grand',null,grandTotal));
  }
}

<!-- thePage -->
<apex:page standardController="Opportunity" extensions="theController">
  <apex:datatable value="{!rows}" var="row">
    <apex:column headerValue="Product Name">
      <apex:outputLink value="/{!row.rowProduct.id}"  rendered="{!row.rowType='item'}">{!row.rowProduct.name}</apex:outputLink>
      <apex:outputText value="Total" rendered="{!row.rowType='sub'}"/>
      <apex:outputText value="Grand Total" rendered="{!row.rowType='grand'}"/>
    </apex:column>
    <apex:column headerValue="Total Price" value="{!row.rowAmount}"/>
  </apex:datatable>
</apex:page>

Formatting not included, I'll leave that to you.

 

All Answers

sfdcfoxsfdcfox

You can create a wrapper class to generate the values. I'd imagine the class looks like this:

 

 

// controller
public class theController {
  public class wrapper {
    public wrapper(string rT, product2 p, decimal amt) {
      rowType = rT;
      rowProduct = p;
      rowAmount = amt;
    }
    public string rowType { get; set; }
    public Product2 rowProduct { get; set; }
    public decimal rowAmount { get; set; }
  }
  public list<wrapper> rows { get; set; }
  public opportunity record { get; set; }
  public theController(ApexPages.StandardController controller) {
    record = (Opportunity)controller.getRecord();
    rows = new list<wrapper>();
    decimal subTotal, grandTotal = 0;
    for(OpportunityLineItem oli:[select id,pricebookentry.product2.id,pricebookentry.product2.name,quantity,unitprice from opportunitylineitem where opportunityid = :record.id]) {
      subTotal = 0;
      for(Integer ctr = 0; ctr < oli.quantity; ctr++) {
        subTotal += oli.unitprice;
        rows.add(new wrapper('item',oli.pricebookentry.product2,oli.unitprice));
      }
      grandTotal += subTotal;
      rows.add(new wrapper('sub',null,subTotal));
    }
    rows.add(new wrapper('grand',null,grandTotal));
  }
}

<!-- thePage -->
<apex:page standardController="Opportunity" extensions="theController">
  <apex:datatable value="{!rows}" var="row">
    <apex:column headerValue="Product Name">
      <apex:outputLink value="/{!row.rowProduct.id}"  rendered="{!row.rowType='item'}">{!row.rowProduct.name}</apex:outputLink>
      <apex:outputText value="Total" rendered="{!row.rowType='sub'}"/>
      <apex:outputText value="Grand Total" rendered="{!row.rowType='grand'}"/>
    </apex:column>
    <apex:column headerValue="Total Price" value="{!row.rowAmount}"/>
  </apex:datatable>
</apex:page>

Formatting not included, I'll leave that to you.

 

This was selected as the best answer
abi1.2911902284080376E12abi1.2911902284080376E12

Thanks a lot... :smileyhappy: Its working fine...

SFDC_WorksSFDC_Works

Hi SFDCFOX,

 

           can we group the same table with respect to product family. The solution is based on Quantity. Can we group under Famiy???

 

 

Please let me know.

 

sfdcfoxsfdcfox

You certainly could group by product family or any other means. The specific solution I provided was directed at this poster's specific scenario. The point of the post, however, is to illustrate the usage of wrapper classes, which allows you to generate any sort of arbitrary data representation that will ultimately be rendered in Visualforce.