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
pixel1024pixel1024 

apex:repeat to build table. Need to show/hide html tag.

I'm trying to show or hide a <tr> HTML tag inside an apex:repeat, but can't find a way where SFDC will save the code without error. Basically I'm building a table and want to start a new <tr> if a certain condition is met (item number mod 3 = 0, so create a new row when the table row has three cells). Attached is a simplifed example, but it shows what I'm trying to do.

 

Note: I can't use an apex:panelGroup because I have nested repeaters in each "column".

 

Thanks!

 

Controller:

public with sharing class TestBuildTable {
	public List<Integer> Ints {get;set;}
	
	public TestBuildTable() {
		this.Ints = new List<Integer>();
		for (Integer i=0; i<20; i++) {
			this.Ints.add(i);
		}
	}
}

 

VF page first try (fail):

<apex:page id="TestBuildTable" Controller="TestBuildTable">
  <table>
    <apex:repeat value="{!Ints}" var="int">
	  {!IF(MOD(int,3)==0,'<tr>',' ')}
	    <td>{!int} nested repeat here</td>
	  {!IF(MOD(int,3)==0,'</tr>',' ')}
	</apex:repeat>
  </table>
</apex:page>

 

VF page second try (fail):

<apex:page id="TestBuildTable" Controller="TestBuildTable">
  <table>
    <apex:repeat value="{!Ints}" var="int">
	  <apex:outputPanel rendered="{!MOD(int,3)==0}" layout="none"><tr></apex:outputPanel>
	    <td>{!int}</td>
	  <apex:outputPanel rendered="{!MOD(int,3)==0}" layout="none"></tr></apex:outputPanel>
	</apex:repeat>
  </table>
</apex:page>

 

The desired result is a table that looks like this:

1    2    3

4    5    6

7    8    9

10 11 12

13 14 15

FippyFippy

It looks like you just want to wrap your list of data into 3 columns? If so, have you looked at panelGrid? It automatically flows the data into rows (see the example on the VisualForce docs page, e.g.:

 

<apex:panelGrid columns="3" id="theGrid">
pixel1024pixel1024

I called it a panelGroup, but I meant panelGrid when I said that it wasn't an option. I'm using nested repeats, and they don't work with panelGroups.

 

From SFDC documentation: "Note that if an<apex:repeat>component is used within an <apex:panelGrid>component, all content generated by the<apex:repeat>component is placed in a single <apex:panelGrid>cell. The<apex:panelGrid>component differs from<apex:dataTable>because it does not process a set of data with an iteration variable."

FippyFippy

Can you reformat your data differently in your controller so that you CAN use a panelGrid? I think the whole point of apex is that you aren't supposed to write table-based HTML directly.

hemantgarghemantgarg

If it is possible to go with API version 18 or below in your case, then this code can be saved under 18 or below api versions. you can try it.

Andy BoettcherAndy Boettcher

If you put your data in a wrapper class you could do it with a combination of <apex:outputText> tags.

 

Keep in mind this is pseudo-code...if you haven't already solved it, let me know and I can spiff it up.

 

*Controller*

(define wrapper class)
data
rowflipper

(populate wrapper class)
int count = 0
wrapper.data= your value;
if(count = 3) { 
	rowflipper = true;
	count = 0;
} else {
	rowflipper = false; 
	count += 1;
}

* VF Page*
<apex:repeat> logic you have
<apex:outputText value="<tr>" rendered="{!rowflipper}" escape="false" />
<td nested stuff here>
<apex:outputText value="</tr>" rendered="{!rowflipper}" escape="false"/>

 ** EDIT:  Forgot the escape="false" needs to be on the outputText tag.

 

Let me know if you need help.

 

-Andy

 

 

gautigauti

i am having a issue in writing tr and td inside the output text values for example:

<apex:outputText escape="false"

rendered="{!IF((cLife.Display_name__c == ansList.C_Life_Questions__r.Country_Life_section__r.Display_name__c),true, false)}"
value=" <tr  > <td style=text-align:right>  "

 how to resolve it??

 

Thanks in advance

 

MohaMoha

'm having the same issue, did u find any solution ?

gowthamgowtham

"

<apex:repeat value="{!clifeAnsList}" var="ansList" id="id2"> 
                            <apex:outputText escape="false" rendered="{!IF((cLife.Display_name__c == ansList.C_Life_Questions__r.Country_Life_section__r.Display_name__c),true, false)}"
                            value=" <tr  > <td style=text-align:right>  " />
                            <apex:outputText rendered="{!IF((cLife.Display_name__c == ansList.C_Life_Questions__r.Country_Life_section__r.Display_name__c),true, false)}"
                            value="{0, number, 0}.     " >
                            <apex:param value="{!ansList.C_Life_Questions__r.Question_Order__c}"/>
                            </apex:outputText>
                            <apex:outputText escape="false" rendered="{!IF((cLife.Display_name__c == ansList.C_Life_Questions__r.Country_Life_section__r.Display_name__c),true, false)}"
                            value="</td> <td style=color:purple >" />
                            <apex:outputField value="{!ansList.Question__c}" rendered="{!IF((cLife.Display_name__c == ansList.C_Life_Questions__r.Country_Life_section__r.Display_name__c),true, false)}" 
                            > </apex:outputField>   
                            <apex:outputText escape="false" rendered="{!IF((cLife.Display_name__c == ansList.C_Life_Questions__r.Country_Life_section__r.Display_name__c),true, false)}"
                            value=" </td>
                            <td style=text-align:center;>" />
                            <apex:inputfield value="{!ansList.Answer__c}" rendered="{!IF((cLife.Display_name__c == ansList.C_Life_Questions__r.Country_Life_section__r.Display_name__c),true, false)}"  ></apex:inputfield> 
                            <apex:outputText escape="false" rendered="{!IF((cLife.Display_name__c == ansList.C_Life_Questions__r.Country_Life_section__r.Display_name__c),true, false)}"
                            value=" </td>
                            <td >" /> 
                            <apex:inputfield styleClass="commentStyle" value="{!ansList.Comments__c}" rendered="{!IF((cLife.Display_name__c == ansList.C_Life_Questions__r.Country_Life_section__r.Display_name__c),true, false)}"></apex:inputfield>           
                            <apex:outputText escape="false" rendered="{!IF((cLife.Display_name__c == ansList.C_Life_Questions__r.Country_Life_section__r.Display_name__c),true, false)}"
                            value=" </td>
                            <tr>" />
                        </apex:repeat>

gowthamgowtham
Try this
ReeceReece
        <apex:variable var="cols" value="{!1}" />
        <table>
            <tr>
                <apex:repeat var="int" value="{!Ints}" >
                     <td>
                         <apex:outputText escape="false" value="</tr><tr>" rendered="{!if(mod(cols,4)=0,true,false)}" />
                         <apex:variable var="cols" value="{!cols+1}" />
                    </td>
                </apex:repeat>
            </tr>
         </table>