How to add values in Thymeleaf variable in the loop and after complete loop display final value

11,276

In general you can't change a variable once it's been defined with th:with. They just aren't designed to be used that way. Rather, they are simple temporary variables.

Thymeleaf also doesn't have the concept of global variables. The closest you get to that are attributes you've placed on the model.

You can use collection projection for this:

<table>
    <tr th:each="count,iterator: ${product.paidService}">
        <td th:text="${iterator.index+1}" />
        <td th:text="${count.name}" />
        <td>Paid</td>
        <td th:text="${count.price}" />
    </tr>

    <tr>
        <td colspan="3" />
        <td><b th:text="${#aggregates.sum(product.paidService.![price])}" /></td>
    </tr>
</table>

(General style comments. If you want to do thymeleaf stuff, but don't want to output anything you should be using <th:block /> -- rather than placing <p /> or <span /> tags directly in tables rows.)

Share:
11,276
Ketan Navadiya
Author by

Ketan Navadiya

Hi everyone, I'm a client-focused full-stack developer with more than 4 years of experience in various types of software projects. Particularly I'm interested in code quality and best practices in the development process with Node.js, Angular, Express.js, MySQL, MongoDB and AWS, and try to stay up-to-date on current technology and trends. I really like to think through the architecture from scratch and the challenge of expanding and developing large-scale applications. You'll get robust, secure, modular, and maintainable code. Expertise in delivery on time with quality of work. Technologies I use in my work are as follows: ✅ Angular, Node.js, Express, JQuery ✅ TypeScript ✅ MySQL, MongoDB, Redis, Azure Cosmos DB, and PostgreSQL ✅ RESTful interfaces (JSON, XML) ✅ HTML, CSS and CSS Preprocessors ✅ Linux Command-line Interface ✅ Windows, Linux, macOS ✅ Git, Jira ✅ Amazon Web Services If you are looking for someone to bring your ideas to life, or for someone to be a part of your development team feel free to contact me. Let’s create something awesome together and If you need a problem solver rather than just a coder, you are a right stop here and contact me. Thanks!

Updated on July 03, 2022

Comments

  • Ketan Navadiya
    Ketan Navadiya almost 2 years

    I am design bill for products, I want to display all products in thymeleaf template using table and at last, out of loop I want to display sum of price of all products in thymeleaf. how can I define global variable and do it?

    <table th:with="totalPrice=0">
        <tr th:each="count,iterator: ${product.paidService}">
            <td th:text="${iterator.index+1}"></td>
            <td th:text="${count.name}"></td>
            <td>Paid</td>
            <td th:text="${count.price}"></td>
            <p th:with="totalPrice=${totalPrice + count.price}"> </p>
        </tr>
        <span th:text="${totalPrice}"></span>
    </table>
    

    I am geeting 0 as output but I want sum of all products price as output.

    How to make variable global and solve my problem?