By Rijul Chaturvedi on July 7, 2021
Intermediate

Jinja templating can be used to reference any field on any DocType in ERPNext. This can simply be done by calling {{doc.field_name}} on a print format, where 'doc.name' is the variable name for a certain field.


However this approach does not work for Child Tables inside a DocType. This article will help you traverse and display all rows pertaining to a child table inside any DocType.


Pre Requisites


We would require the variable name of the child table on the corresponding DocType. This can be viewed from the 'Customize Form' section for the required DocType. The same is illustrated below:-



We will also require the variable names of all the fields inside the child table which need to be referenced. This can be obtained from the 'Customize Form' section of the corresponding child table as shown below:-




Method 1. Displaying rows of a Child Table on an unordered list


<ul>
{% for row in doc.items %}
<li>
Item Code: {{ row.get_formatted("item_code", doc) }}
Quantity: {{ row.get_formatted("qty", doc) }}
Rate: {{ row.get_formatted("rate", doc) }}
Amount: {{ row.get_formatted("amount", doc) }}
</li>
{% endfor %}
</ul>


The output on a print format would be as follows:-




Method 2. Displaying rows of a Child Table as a table


<table border="1" cellspacing="0" cellpadding="4">
<tr align="center">
  <th>Item Code</th>
  <th>Quantity</th> 
  <th>Rate</th>
  <th>Amount</th>
 </tr>
 <br>
{% for item in doc.items %}
<tr align="center">
    <td>{{item.item_code }}</td>
    <td>{{item.qty}}</td>
    <td>{{item.rate}}</td>
    <td>{{item.amount}}</td>
  </tr>
{% endfor %}
</table>


The output on a print format would be as follows:-



This template can be used for reference. Any additional fields on the child table field can be fetched in a similar manner, by amending the Jinja template.


More articles on Customization



More articles on Customization
Comments

No comments yet. Start a new discussion.

Add Comment