Adding Line Numbers in Odoo QWeb Report

Abid Patel
06-Nov-2024 Updated : 06-Nov-2024

Learn how to add line numbers in Odoo QWeb reports for better readability. This guide provides a step-by-step approach for customizing reports in Odoo 16

Adding Line Numbers in Odoo QWeb Report

Manipulating reports in Odoo often requires adding line numbers. Line numbers are especially relevant in documents such as invoices and sales orders. They boost clarity and readability. Users find it easier to identify each item. This guide focuses on adding line numbers to an Odoo QWeb report. It aims for simplicity and effectiveness in the process.

What's QWeb in Odoo?

QWeb is a templating engine utilized by Odoo. It's used to create XML-based reports. Reports like PDF invoices and quotations. Developers have control over the HTML structure of their reports. They can also add necessary logic. This helps them to format data correctly.

To Add Line Numbers to a QWeb Report

To include line numbers in your QWeb report you'll need to iterate over report items. Apply a counter to each line. These are the steps.

Code Example

Consider the addition of line numbers to the order lines in a sales order report. A QWeb XML file can be utilized. This file incorporates the t-esc attribute. This attribute executes Python expressions directly in the XML. It helps to create a simple counter.

xml

<template id="report_saleorder_document">
    <t t-call="web.html_container">
        <t t-foreach="docs" t-as="o">
            <div class="page">
                <h2>Sales Order</h2>
                <table class="table table-condensed">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Product</th>
                            <th>Description</th>
                            <th>Quantity</th>
                            <th>Unit Price</th>
                            <th>Subtotal</th>
                        </tr>
                    </thead>
                    <tbody>
                        <!-- Initialize the counter -->
                        <t t-set="line_no" t-value="0"/>
                        
                        <!-- Iterate over each order line and increment the counter -->
                        <t t-foreach="o.order_line" t-as="line">
                            <tr>
                                <td>
                                    <t t-set="line_no" t-value="line_no + 1"/>
                                    <span t-esc="line_no"/>
                                </td>
                                <td><span t-esc="line.product_id.display_name"/></td>
                                <td><span t-esc="line.name"/></td>
                                <td><span t-esc="line.product_uom_qty"/></td>
                                <td><span t-esc="line.price_unit"/></td>
                                <td><span t-esc="line.price_subtotal"/></td>
                            </tr>
                        </t>
                    </tbody>
                </table>
            </div>
        </t>
    </t>
</template>

Explanation of the Code

  • ▹ Initialize Counter: Below code sets line_no to zero before iteration. The code is <t t-set="line_no" t-value="0"/>.
  • ▹ Increment Counter: In the loop (t-foreach="o.order_line") the counter is incremented. We increment line_no by one for each line with this code: <t t-set="line_no" t-value="line_no + 1"/>.
  • ▹ Show Line Number: Below is the tag <span t-esc="line_no"/>. It shows the current value of line_no. It effectively displays the line number for each row.
  • ▹ Display of Data: Data is displayed for every column (Product Description, Quantity, etc.). We use t-esc for this purpose. It retrieves and displays fields. For product name, we use line.product_id.display_name. For quantity, it is line.product_uom_qty. And so forth.

Adding Line Numbers Benefits

The addition of line numbers boosts readability. It allows users to quickly reference specific items on a report. This setup proves useful for documents with lots of line items. Examples are invoices and purchase orders.

Method Conclusion

Method gives a simple way to add line numbers. Users can do this to any list in QWeb report in Odoo. The key is initializing a counter variable outside loop and incrementing it inside loop. This will display a line number for each record. These steps can help create more organized and user-friendly reports in Odoo.

Make a Comment

Your email address will not be published. Required fields are marked *