Variable to concatenate and display later

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Variable to concatenate and display later

redfox26
This post was updated on .
I use spring boot 2 and thymeleaf

In a class name SamplesTestResult

private Double compression;
private Double flexion;
I have a list of SamplesTestResult

<div th:each="testResult: ${sampletTestResult}" th:remove="tag">         
    <table>
        <tr th:if="{testResult.compression!=null}">
            <td th:text=" 'Compression ' +  $ {}"></td><td th:text="'Requirement' + ${testResult.compression}"></td>
        </tr>
        <tr th:if="{testResult.flexion!=null}">
            <td th:text=" 'Flexion ' +  $ {}"></td><td th:text=" 'Requirement' + ${testResult.flexion}"></td>
         </tr>
     </table>
</div>

So the result is

<table>
    <tr>
        <td>Compression 12</td>
        <td>Flexion 14</td>
    </tr>
</table>
<table>
    <tr>
        <td>Compression 22</td>
        <td>Flexion 17</td>
    </tr>
</table>  

I search to get

<table>
    <tr>
        <td>Compression 12, 22</td>
        <td>Flexion 14, 17</td>
    </tr>
</table>

I think to get that, I would need to create a kind a variable to stock value?
Reply | Threaded
Open this post in threaded view
|

Re: Variable to concatenate and display later

Reiju
This post was updated on .
U can transform this collection to Map<String, List<Double>>. And print key + values.

Or something like this:

public class ViewUtils {

    public static String printCompressions(List<SamplesTestResult> list) {
        return list.stream()
                .filter(Objects::nonNull)
                .map(SamplesTestResult::getCompression) //getFlexion
                .filter(Objects::nonNull)
                .map(String::valueOf)
                .collect(Collectors.joining(", "));
    }

}
<table>
        <tr>
            <td>Compression</td>
            <td th:text="${T(project.utils.ViewUtils).printCompressions(results)}"></td>
        </tr>
        <tr>
            <td>Flexion</td>
            <td th:text="${T(project.utils.ViewUtils).printFlexions(results)}"></td>
        </tr>
</table>
Reply | Threaded
Open this post in threaded view
|

Re: Variable to concatenate and display later

redfox26
This post was updated on .
thanks, will try, look great solution

edit:

work fine