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
MellowYellowMellowYellow 

How to create HTML table containing apex fields in Visualforce page

I'm trying to create a VF page that displays a table of Opportunity and Contract values.  I need to use an IF statement so that blank rows don't get created if the test condition is false.  I'm not able to escape the HTML tags so that the browser treats them as HTML instead of text.

Here is an example of what I'm trying to do;

<apex:page standardController="Opportunity">
   
             <table border="1" cellPadding="4">       
          <TR> <TH> Name of Fee </TH> <TH>Current Fee</TH> <TH> Updated Fee </TH> </TR>
          <TR><TD>
        {!IF((Opportunity.Contract__r.FieldA = Opportunity.FieldB),
         'Standard Fee:' & ' </TD> <TD> ' & TEXT(Opportunity.Contract__r.FieldA) &  ' </TD><TD> ' &     TEXT(Opportunity.Field)  , "" )}                                                                                
          </TD></TR>                                              
          </table>
       </apex:page>

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

I had a very long html page with 24 tables populated from data across multiple objects....

 

What I did was constructed the string in the controller, here is a snippet and not complete:

 

//*********************************************          TABLE DATA      *************************************************************

For (integer i=1;i<13;i++){  //Month loop

    //Table Header

    tmp =     '<table border="1" bordercolor="#FFCC00" style="background-color:#FFFFFF; display: inline; width:25%"  cellpadding="3" cellspacing="3" >' +    
    
              '<tr class="headerRow " >' + 
        '<td colspan="4" align="center" style="background-color: #6699CC;" ><b><font size="3">' + mMonth.get(i) + ' ' + paramYear + '</font></b></td></tr>' +
        '<tr class="headerRow" style="background-color: #99CCFF; font-weight: bold;">' +
        '<td align="center">Region</td>' +
        '<td align="center">Live Dollars</td>' +
        '<td align="center">' + paramYear + ' Impact</td>' +
        '<td align="center">Next FY Impact</td>' +
        '</tr>';


    q += tmp;
    tmp=''; //Reset
        For (Integer r=1;r<6;r++){ //Regional Loop **Need to break up for East vs West, Right Now Only does EAST
        
            tmp = '<tr>' +
                    '<td style="font-size: 11px">' +
                        mRegion.get((a * 5) + r) +  '</td>';
                    


                        // Block to get the Live Data for each Region by month
                         
                        tmp += '<td align="right" style="font-size:11px">' +  toCurrency(finalData[(a * AREAOFFSET) + ((i-1)* OFFSET)+r-1].regLive) + '</td>';
                        tmp += '<td align="right" style="font-size:11px">' + toCurrency(finalData[(a * AREAOFFSET) + ((i-1)* OFFSET)+r-1].totThisFYImpact) + '</td>';
                        tmp += '<td align="right" style="font-size:11px">' + toCurrency(finalData[(a * AREAOFFSET) + ((i-1)* OFFSET)+r-1].totNextFYImpact) + '</td>';
        
    q +=  tmp + '</tr>';
    tmp = '';
        }

                        tmp = '<tr ><td style="font-weight: bold;">Total</td>' +
                              '<td align="right" style="font-size:11px; font-weight: bold; background-color: #999999;">'+ 
                              toCurrency(finalData[(a * AREAOFFSET) + (i* TOTALROW) + i-1].totLive) + '</td>';
                           
                        tmp += '<td align="right" style="font-size:11px; font-weight: bold; background-color: #999999;">' + 
                                toCurrency(finalData[(a * AREAOFFSET) + (i* TOTALROW) + i-1].totThisFYImpact) + '</td>';


                        tmp += '<td align="right" style="font-size:11px; font-weight: bold; background-color: #999999;">' + 
                                toCurrency(finalData[(a * AREAOFFSET) + (i* TOTALROW) + i-1].totNextFYImpact) + '</td></tr></table>';

    q += tmp;
    }
    
    
  } //Area Loop          

 Then in the page all I had to do was put this to display it:

 

<apex:outputtext value="{!buildTable}" escape="false" id="tblHTML"/>



I can manipulate and dynamically create the string on the fly with the controller without having to update the vf page.

The key is the escape="false" to allow the html code to not be stripped.

All Answers

Starz26Starz26

I had a very long html page with 24 tables populated from data across multiple objects....

 

What I did was constructed the string in the controller, here is a snippet and not complete:

 

//*********************************************          TABLE DATA      *************************************************************

For (integer i=1;i<13;i++){  //Month loop

    //Table Header

    tmp =     '<table border="1" bordercolor="#FFCC00" style="background-color:#FFFFFF; display: inline; width:25%"  cellpadding="3" cellspacing="3" >' +    
    
              '<tr class="headerRow " >' + 
        '<td colspan="4" align="center" style="background-color: #6699CC;" ><b><font size="3">' + mMonth.get(i) + ' ' + paramYear + '</font></b></td></tr>' +
        '<tr class="headerRow" style="background-color: #99CCFF; font-weight: bold;">' +
        '<td align="center">Region</td>' +
        '<td align="center">Live Dollars</td>' +
        '<td align="center">' + paramYear + ' Impact</td>' +
        '<td align="center">Next FY Impact</td>' +
        '</tr>';


    q += tmp;
    tmp=''; //Reset
        For (Integer r=1;r<6;r++){ //Regional Loop **Need to break up for East vs West, Right Now Only does EAST
        
            tmp = '<tr>' +
                    '<td style="font-size: 11px">' +
                        mRegion.get((a * 5) + r) +  '</td>';
                    


                        // Block to get the Live Data for each Region by month
                         
                        tmp += '<td align="right" style="font-size:11px">' +  toCurrency(finalData[(a * AREAOFFSET) + ((i-1)* OFFSET)+r-1].regLive) + '</td>';
                        tmp += '<td align="right" style="font-size:11px">' + toCurrency(finalData[(a * AREAOFFSET) + ((i-1)* OFFSET)+r-1].totThisFYImpact) + '</td>';
                        tmp += '<td align="right" style="font-size:11px">' + toCurrency(finalData[(a * AREAOFFSET) + ((i-1)* OFFSET)+r-1].totNextFYImpact) + '</td>';
        
    q +=  tmp + '</tr>';
    tmp = '';
        }

                        tmp = '<tr ><td style="font-weight: bold;">Total</td>' +
                              '<td align="right" style="font-size:11px; font-weight: bold; background-color: #999999;">'+ 
                              toCurrency(finalData[(a * AREAOFFSET) + (i* TOTALROW) + i-1].totLive) + '</td>';
                           
                        tmp += '<td align="right" style="font-size:11px; font-weight: bold; background-color: #999999;">' + 
                                toCurrency(finalData[(a * AREAOFFSET) + (i* TOTALROW) + i-1].totThisFYImpact) + '</td>';


                        tmp += '<td align="right" style="font-size:11px; font-weight: bold; background-color: #999999;">' + 
                                toCurrency(finalData[(a * AREAOFFSET) + (i* TOTALROW) + i-1].totNextFYImpact) + '</td></tr></table>';

    q += tmp;
    }
    
    
  } //Area Loop          

 Then in the page all I had to do was put this to display it:

 

<apex:outputtext value="{!buildTable}" escape="false" id="tblHTML"/>



I can manipulate and dynamically create the string on the fly with the controller without having to update the vf page.

The key is the escape="false" to allow the html code to not be stripped.

This was selected as the best answer
morganeCRMCLOUDERmorganeCRMCLOUDER

Hello,

 

I am very interested in your example to do an html table from the controller. 

Just a single question : what is the return of your function "buildTable" ? 

 

I did something like this  : 

 

public String getbuildTable(){

For (integer i = 0; i <line.getSize(); i++){ 
     tmp = '<table bordercolor="#FFCC00" width:300" cellpadding="3" cellspacing="3" >' + '<tr>' + 
        '  <td colspan="4" align="center">' + someObject.get(i) +'</td></tr>' ;
}

tmp += '</table>';

return tmp;
}

 

 

Problem : I don't know how to display the WHOLE iteration, and not only the last value at index i 

 

any advise ? 

 

 

Starz26Starz26

You are overwriting tmp with each loop

 

Change = to += and initially set tmp to ''

 

public String getbuildTable(){

tmp = '';

For (integer i = 0; i <line.getSize(); i++){ 
     tmp += '<table bordercolor="#FFCC00" width:300" cellpadding="3" cellspacing="3" >' + '<tr>' + 
        '  <td colspan="4" align="center">' + someObject.get(i) +'</td></tr>' ;
}

tmp += '</table>';

return tmp;
}

 

morganeCRMCLOUDERmorganeCRMCLOUDER

Thank you, it works :)