希望能帮到你。
我在 NetSuite 上有一个自定义高级 PDF 模板,它需要循环使用 N/render 模块中的“addCustomDataSource”函数构建的 JSON 对象。目前,它仅返回 JSON 的第一行,而不是打印每一行。
下面是 JSON 编译后的样子:
{
lines: [
{
item: "BOM 89",
custcol_itemdisplayname: "White Cabinet (made to order)",
unit: "Ea",
quantity: "1",
amount: ".00"
},
{
item: "ABC 20019 N",
custcol_itemdisplayname: "231-670cm Natural",
unit: "Ea",
quantity: "1",
amount: ".00"
},
{
item: "ABC 10040 N",
custcol_itemdisplayname: "203cm Natural",
unit: "Ea",
quantity: "1",
amount: ".00"
},
{
item: "ABC 10020 N",
custcol_itemdisplayname: "115cm Natural",
unit: "Ea",
quantity: "1",
amount: ".00"
}
]
}
这是我在 PDF 模板的 <#list> 指令中使用的循环片段:
<#assign line_no = 0>
<table border="0" cellpadding="1" cellspacing="1" style="width:100%;"><!-- start items -->
<#list results.lines as result>
<#if result_index==0>
<tr>
<td style="height: 29px; width: 120px;"><strong>Part Number</strong></td>
<td style="height: 29px; width: 60px;"><strong> </strong></td>
<td align="left" style="height: 29px; width: 200px; text-align: right;"><strong>Description</strong></td>
<td align="center" style="height: 40px; width: 40px; text-align: center;"><strong>Cty of Mfg</strong></td>
<td align="center" style="height: 29px; text-align: left; width: 60px;"><strong>UOM</strong></td>
<td align="right" style="height: 29px; text-align: right;"><strong>Qty</strong></td>
<td align="right" style="height: 29px; text-align: right;"><strong>Value</strong></td>
</tr>
</#if>
<tr>
<td style="width: 120px;">${result[line_no].item}</td>
<td style="width: 60px;"><strong> </strong></td>
<td align="left" style="width: 200px; text-align: right;">${result[line_no].custcol_itemdisplayname}</td>
<td align="center" style="width: 40px; text-align: center;">UK</td>
<td align="center" style="text-align: right; width: 60px;">Pieces</td>
<td align="right" style="text-align: right;">${result[line_no].quantity}</td>
<td align="right" style="text-align: right;">${result[line_no].amount}</td>
</tr>
<#assign line_no = line_no + 1> <-----THIS IS USED TO INCREMENT TO NEXT POSITION
</#list>
</table>
以下是用于将数据源创建为 JSON 对象的 SuiteScript 代码段:
renderer.addCustomDataSource({
format: render.DataSource.OBJECT,
alias: "results",
data: searchData
});
我已使用“results”作为 <#list> 指令中的变量,如上面 SuiteScript 函数调用中所命名。因为返回了第一行,所以我相当肯定该问题与此无关。
据我所知,一切都构建正确,因为我还能够访问 PDF 中构造的特定位置。例如,PDF 可以输出以下数据:
${result[1].amount}
${result[2].amount}
${result[3].amount}
这让我相信问题与 PDF 中的 line_no 变量有关。也就是说,这是 PDF 本身的最终输出:
请注意,“line_no”变量成功打印每一行。那么为什么 ${result[line_no].amount} 不起作用呢? 谁能建议什么可能会阻止模板迭代对象?
希望尽快收到回复!
最佳答案
当你 <#list results.lines as result>
,每个result
是一行 - 您不需要创建自己的索引( line_no
)来迭代。因此${result[line_no].amount}
应该是${result.amount}
。我更困惑为什么它适用于第一行 - 但我希望这是因为在这一点 line_no
未定义,因此 Freemarker 会忽略它。
关于NetSuite Advanced PDF - 无法循环遍历从 addCustomDataSource N/render 函数构建的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75755378/