+ Start a Discussion
CRM JediCRM Jedi 

Weird VisualForce Behaviour

Hi all,

Recently I have a requirement that requires me to generate table dynamically on a VisualForce page. Although it is not the best way, I've decided to generate HTML Table codes in the controller itself and pass it back to the VisualForce page.

 

The controller has a method similiar to this:

 

    public List<String> getHTMLCode() {

List<String> htmlList = new List<String>();

String tempStr = '';
for(Integer i=0; i<10; i++) {
tempStr = '<tr><td>' + i + '</td><td>THIS IS ROW NUMBER ' + i + '</td></tr>';

htmlList.add(tempStr);
}

return htmlList;

}

 

The VisualForce page is written as followed:

 

<apex:page controller="TestPageController" showheader="false" sidebar="false" >

<table>
<apex:repeat value="{!htmlCode}" var="html">
<apex:outputText value="{!html}"></apex:outputText>

</apex:repeat>
</table>
</apex:page>

 

 This would not work as the page is shown like this:

 

<tr><td>0</td><td>THIS IS ROW NUMBER 0</td></tr><tr><td>1</td><td>THIS IS ROW NUMBER 1</td></tr><tr><td>2</td><td>THIS IS ROW NUMBER 2</td></tr><tr><td>3</td><td>THIS IS ROW NUMBER 3</td></tr><tr><td>4</td><td>THIS IS ROW NUMBER 4</td></tr><tr><td>5</td><td>THIS IS ROW NUMBER 5</td></tr><tr><td>6</td><td>THIS IS ROW NUMBER 6</td></tr><tr><td>7</td><td>THIS IS ROW NUMBER 7</td></tr><tr><td>8</td><td>THIS IS ROW NUMBER 8</td></tr><tr><td>9</td><td>THIS IS ROW NUMBER 9</td></tr>

 

however, tweaking the code by adding a " <script></script> "bfore the <apex:outputtext>, to this:

 

 

 

 

<apex:page controller="TestPageController" showheader="false" sidebar="false" >

<table>
<apex:repeat value="{!htmlCode}" var="html">
<script></script>
<apex:outputText value="{!html}"></apex:outputText>

</apex:repeat>
</table>
</apex:page>

 

 

The page will display correctly:

 

0 THIS IS ROW NUMBER 0
1 THIS IS ROW NUMBER 1
2 THIS IS ROW NUMBER 2
3 THIS IS ROW NUMBER 3
4 THIS IS ROW NUMBER 4
5 THIS IS ROW NUMBER 5
6 THIS IS ROW NUMBER 6
7 THIS IS ROW NUMBER 7
8 THIS IS ROW NUMBER 8
9 THIS IS ROW NUMBER 9

 

So my questions are:

1. Can I rely on this "workaround"? 

2. When <apex:outputtext>is used to displathtml code like <tr><td>, does it suppose to convert it into ascii code like "&lt;" or treat them as normal html tag? This is important for me in the way that if this is a bug, I will need to know what is the expected behaviour so I can cope with it.

 

Thanks in advance :)

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Edwin VijayEdwin Vijay
Try using the escape attribute of outputtext. I do not have a clear idea about this attribute but i guess it has something to do with your problem..