Paul Taylor
Paul Taylor

Reputation: 13210

How to use j2html when have a method that needs to return multiple tags?

How to use j2html when have a method that needs to return multiple tags?

So here I have a reusable method that constructs a label and inputfield for a particular option. The label is put inside a td and the input field in another td field, all within a tr, and the tr is returned.

Logically this is just one thing (user input) so it makes sense to have within a single method rather than a separate method for label and input. This works fine for my webpages where i have a series of rows, each one containing label and input field all lines up nicely within a table. (Coming from a Java background I see use of table as simply a layout manager, and a more sensible approach then trying to use entirely CSS)

public Tag addInputTextTr(UserOption userOption, String value, String className)
{
    return tr(
            td(label(userOption.getLabelMsg())
                    .withTitle(userOption.getTooltipMsg())),
            td(input()
                    .withType(Html.TEXT)
                    .withName(userOption.getOption()).withValue(value)
                    .withClass(className)
            )
    );
}

Calling Code

addInputTextTr(UserOption.LICENSE_GUID1, licenseGuid1, LICENSE_INPUTFIELD_CLASS)

My problem is that sometimes I need three table cells so I can add a button at the end of the row. So to account for this I should just return the tds from this function and wrap into tr myself in calling method, but I cant just return two tds unless I wrap into a list, then this means i have to use each() in calling code which complicates it. I cannot return them in a span() since span() is not allowed within tr().

public List<Tag> addInputTextTds(UserOption userOption, String value, String className)
    {
        List<Tag> tds = new ArrayList<>();
        tds.add(
            td(label(userOption.getLabelMsg())
                    .withTitle(userOption.getTooltipMsg())));
        tds.add(
            td(input()
                    .withType(Html.TEXT)
                    .withName(userOption.getOption()).withValue(value)
                    .withClass(className)
            )
        );
        return tds;
    }

Calling code now

tr(
   each(addInputTextTds(UserOption.LICENSE_EMAIL, licenseEmail, LICENSE_INPUTFIELD_CLASS),
           next -> next
   )
),

I was looking for a j2html return type that could be used to store multiple elements ?

Upvotes: 3

Views: 1296

Answers (1)

Stormcloud
Stormcloud

Reputation: 2297

I spent a while playing different solutions for this. The simplest one I was able to come up with is:

public static void main(String[] args) {
    String html =
        html(
          body(
            ul(
              makeSomeChildren()
            )
          )
        ).renderFormatted();
}

private static DomContent makeSomeChildren() {
    return join(b("Child 1"), b("Child 2"));
}

which produces:

<html>
    <body>
        <ul>
            <b>Child 1</b><b>Child 2</b>
        </ul>
    </body>
</html>

Upvotes: 3

Related Questions