How to give serial number in jinja for loop in print format templates

when i give row.index it give (risk_location_and_value)1 like that…when i try to iterate it gives error.
i attached the screenshots below.!


Not sure if either of these will work, but some things to try:
Try using the jinja keyword set, like this:

{% set index = index + 1 %}

Alternatively, you could try using python’s builtin enummerate(), but Frappe has very poor support for standard library functions inside safe_exec. I know that len() isn’t available for example.

1 Like

Thank you @tmatteson for your quick solution. enumerate() works :smile:
but the {% set index = index + 1 %} give one for all the rows.

@BalaV I was just about to reply with a tip about the using the start keyword argument. Glad it worked.

1 Like

trying to create print format for sales invoice
and sequence no for parent item

can you advice me
Thanks

<style>
    .invoice-table {
        font-family: Arial, sans-serif;
        font-size: 10pt;
        border-collapse: collapse;
        width: 100%;
    }
    .invoice-table th, .invoice-table td {
        border: 1px solid #ccc;
        padding: 5px;
        text-align: left;
    }
    .invoice-table th {
        background-color: #f0f0f0;
        font-weight: bold;
        text-align: center;
    }
    .parent-item {
        font-weight: bold;
    }
    .child-item {
        padding-left: 20px; /* Indent for child items */
    }
</style>

<table class="invoice-table">
    <thead>
        <tr>
            <th style="width: 5%;">SN</th>
            <th style="width: 60%;">Item Description</th>
            <th style="width: 7%;">Qty</th>
            <th style="width: 10%;">Rate</th>
            <th style="width: 10%;">Amount</th>
        </tr>
    </thead>
    <tbody>
        {% set parent_serial_no = 0 %} <!-- Initialize serial number -->
        {% for row in doc.items %}
        <tr>
            <!-- Serial Number -->
            <td>
                {% if row.custom_is_main_item == 1 %}
                    {% set parent_serial_no = parent_serial_no + 1 %} <!-- Increment serial number for parent items -->
                    {{ parent_serial_no }}
                {% endif %}
            </td>

            <!-- Item Description -->
            <td class="{{ 'parent-item' if row.custom_is_main_item == 1 else 'child-item' }}">
                {{ row.item_name }}
                {% if row.custom_is_main_item == 1 %}
                    ({{ row.item_code }})
                {% endif %}
            </td>

            <!-- Qty -->
            <td style="text-align: center;">
                {{ row.qty }}
            </td>

            <!-- Rate -->
            <td style="text-align: right;">
                {{ "{:,.2f}".format(row.rate) }}
            </td>

            <!-- Amount -->
            <td style="text-align: right;">
                {{ "{:,.2f}".format(row.amount) }}
            </td>
        </tr>
        {% endfor %}
        
        <!-- Total Row -->
        <tr>
            <td colspan="4" style="font-weight: bold; text-align: right;">Total:</td>
            <td style="font-weight: bold; text-align: right;">
                {{ "{:,.2f}".format(doc.total) }}
            </td>
        </tr>
    </tbody>
</table>

<!-- Grand Total and Total in Words -->
<div style="margin-top: 10px; font-size: 9pt; font-weight: bold;">
    Grand Total: {{ "{:,.2f}".format(doc.grand_total) }}<br>
    In Words: {{ frappe.utils.money_in_words(doc.grand_total) }}
</div>