I have sorted the item lines by job and grouped th...
# advancedpdf
l
I have sorted the item lines by job and grouped them by job. My problem is that currently the column headers have an if statement where it says print it only for index = 0 which makes sense in a regular item table, so the header columns won't be repeated. But the first job isn't always in the first line or index 0. How can I tell NS to print the columns for the first line with job assigned? Code in the reply section.
<#assign previousJob = "">
<#list record.item?sort_by('job') as item>
<#if item.job != previousJob>
<#assign currentJob = item.job>
<#if item.custcol_abc?has_content && item.amount gt 0><p>${currentJob}</p></#if>
<table class="itemtable" style="width: 100%; margin-top: 10px;"><!-- start items --><#if item_index==0>
<thead>
    
<tr>
    
<th colspan="2">Date</th>
    
<th colspan="5">Memo</th>
    
<th colspan="3">Item</th>
<th colspan ="2" align="center">Employee<br />Rate</th>
    
<th align="center" colspan="2">Rate</th>
    
<th colspan="2">Quantity</th>
    
<th align="right" colspan="2">Amount<br />(ex GST)</th>
    
</tr>
</thead>
</#if>
    
<#list record.item?sort_by('job') as item>
    
<#if item.job == currentJob>
<#if item.custcol_abc?has_content && item.amount gt 0><#assign totalTime = totalTime + item.amount><tr>
<td colspan="2"><#if item.billeddate?has_content>${item.billeddate}<#else>${item.custcol1}</#if></td>
    
<td colspan="5">${item.description}</td>
    
<td colspan="3">${item.custcol_display_name}</td>
<td colspan="2">${item.custcol_pr_pcst}</td>
    
<td align="right" colspan="2">${item.rate}</td>
    
<td colspan="2">${item.quantity}</td>
    
<td align="right" colspan="2">${item.amount}</td>
    
</tr>
    
</#if></#if></#list><!-- end items --></table>
    
</#if>
    
<#assign previousJob = item.job>
</#list>
m
Move the
<table>...</thead>
to before your
<#list>
. I generally do that anyways because it makes more sense to me than checking if it's the first line in every loop iteration
d
Confirming what you're after, do you want to not print anything until
item.job?has_content
? If so, you could conditionally skip these lines (
<#if !item.job?has_content><#continue></#if>
), or you could use the
?drop_while
sequence built-in
from my understanding you would currently be getting a table per unique job (and potentially a table for lines without a job). Each table has a header row I've added a formatted version of your code as a text snippet below:
I'd also like to point out you have nested
<#list>
directives which are not necessary. You could use something like this instead (untested):
l
Thank you both. I will try these tomorrow.
@David B, I copied your code, and I got this error. I don’t quite understand the error message. Just to clarify, what I was trying to do is to group the invoice lines by project but not all of the invoice lines will be printed (only the ones the amount > 0 and the custcol_abc is not blank).
Error on line 561, column 3 in template. Detail... String index out of range: The index was 1 (0-based), but the length of the string is only 0. ---- FTL stack trace (“~” means nesting-related): - Failed at: #if item?is_last || item.job != recor... [in template “template” at line 561, column 3] ---- Error on line 513, column 3 in template. Detail... String index out of range: The index was 0 (0-based), but the length of the string is only 0. ---- FTL stack trace (“~” means nesting-related): - Failed at: #if item?is_first || item.job != reco... [in template “template” at line 513, column 3] ---- Error on line 561, column 3 in template. Detail... String index out of range: The index was 2 (0-based), but the length of the string is only 0. ---- FTL stack trace (“~” means nesting-related): - Failed at: #if item?is_last || item.job != recor... [in template “template” at line 561, column 3]