+ Start a Discussion
go_bear_2001go_bear_2001 

Insert a header while loop through apex:repeat

I'm trying to insert a header in every 10 lines while iterating and display data from a list of objects. I thought of using a temp javascript variable as a counter during iteration and insert a header when necessary but it did not work.  My VF is below:

--------------

<script>

var counter =0; 

</script> 

 <table>

  <tr align="left">

<apex:repeat value="{!headers}" var="h">

<td colspan="1">{!h}</td>

</apex:repeat>

</tr>

<apex:repeat value="{!list_orders}" var="tlist">

<tr>

<td><apex:outputText value="{!tlist.productname}" /></td>

<td><apex:outputText value="{!tlist.productdesc}" /></td>

<td colspan="1"><apex:inputText size="3" value="{!tlist.Qty_M1}" /></td>

<td colspan="1"><apex:inputText size="3" value="{!tlist.Qty_M2}" /></td>

<td colspan="1"><apex:inputText size="3" value="{!tlist.Qty_M3}" /></td> 

</tr>

<script>

counter++;

if(counter % 10 ==0){ 

</script>

   <tr align="left">

<apex:repeat value="{!headers}" var="h">

<td colspan="1">{!h}</td>

</apex:repeat>

</tr>

<script>

  }

</script> 

</apex:repeat>

</table> 

-------------- 

Basically, headers is just a collection of string:

"Product    Product Desc    Quantity_Month1    Quantity_Month2    Quantity_Month3"

and list_orders is a list of a custom object (Orders) that I built from an apex controller and it returns right values.  In IE, when I running this VF page, it notifies an error on the close bracket on the script section, but could not figure out why.

 

anybody came across something like this and had a solution as if I'm not sure my above solution would work? 

thanks ,

go_bear_2001 

SteveBowerSteveBower

I think you can do this easily if you change the way you're thinking about it.  Caveat, I'm just reading your message late at night and whipping out what I think will work.  I don't have this coded up in front of me.  :-)   First, Skip Javascript.  Instead, think about including your Header loop *inside* the loop for the list-orders, but only rendering the headers conditionally.

 

So, in pseudo code:

 

<page>

<table>

<apex:repeat value="{!list_Orders}" var="tlist">

 

<apex:outputPanel rendered="{!doIRenderHeadersThisTime}">

<tr>

<apex:repeat value="{!headers}" var="h">

<td colspan="1">{!h}</td>

</apex:repeat>

</tr>

</apex:outputPanel>

 

<tr>

<td> etc. for the list order stuff

<td> etc.

</tr>

</repeat>

</table>

 

 

Then, in your controller:

 

private Integer headerCounter = 0;

public Boolean getdoIRenderHeadersThisTime() {

if (headerCounter == 0) return true;

if (++headerCounter == 10) headerCounter = 0;

return false;

}

 

 

Hope this helps, (and I hope it works) and it's time for me to go to bed.  :-)   Best, Steve.

go_bear_2001go_bear_2001
Thanks Steve for your extended help.  I've so used to think solving this problem as if it's on JSP and Javascript pages.  thanks for your suggestions.  I think your solution should work.
SteveBowerSteveBower

Yeah, it takes a little practice to get used to the concept that Salesforce pre-processes the generated HTML.  It leads to some easy to use advantages, but can also cause problems when they have a built-in behavior that you don't want but can't stop.

 

Either way, happy to help.

 

BTW, my counter is wrong, it'll never progress beyone zero so you'll have to fix it.  :-)  (the joys of late night...)

 

Steve.