Quantcast

Thymeleaf: Empty List doesn't show input fields

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Thymeleaf: Empty List doesn't show input fields

hmoa05
I'm trying to create a html "Edit/New" page to edit or add new a publisher. One of the details would be an ArrayList of subPublishers. Although the ArrayList might be empty if the publisher has no subPublishers or if i add new publisher.

My issue is with the input fields. I'm attempting to show the ArrayList as follows...

<div class="col-sm-6 b-r">
    <div class="form-group">
        <label class="col-sm-3 control-label">Publisher name: </label>
        <div class="col-sm-9">
        <input th:field="*{publisherName}" type="text" class="form-control" th:maxlength="45"/>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label">Description: </label>
        <div class="col-sm-9">
        <input th:field="*{description}" type="text" class="form-control" th:maxlength="200"/>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label">Enabled: </label>
        <div class="col-sm-9">
        <input th:field="*{status}" value="ENABLED" type="checkbox"/>  
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label">Website URL: </label>
        <div class="col-sm-9">
        <input th:field="*{websiteURL}" type="text" class="form-control" th:maxlength="50"/>
        </div>
    </div>
    <table>
        <thead>
            <tr>
                <th >SubPublishers</th>
            </tr>
        </thead>
        <tbody>
            <tr th:each="subPublisher,stat : *{subPublishers}">
                <td>
                    <input type="text" th:field="*{subPublishers[__${stat.index}__].name}" />
                </td>
                <td>
                    <button type="submit" name="addRow">+</button>
                    <button type="submit" name="removeRow" th:value="${stat.index}">-</button>
                </td>
            </tr>
        </tbody>
    </table>
</div>

And here is part from controller:

@PostMapping(value="/{publisherId}", params={"addRow"})
    public String addRow(@PathVariable int publisherId, final PublisherResource publisher, final BindingResult bindingResult, Model model) {
        publisher.getSubPublishers().add(new SubPublisherResource());
        return showPage(publisher, model);
    }

    @PostMapping(value="/{publisherId}", params={"removeRow"})
    public String removeRow(@PathVariable int publisherId,
            final PublisherResource publisher, final BindingResult bindingResult, 
            final HttpServletRequest req, Model model) {
        final Integer rowId = Integer.valueOf(req.getParameter("removeRow"));
        publisher.getSubPublishers().remove(rowId.intValue());
        return showPage(publisher, model);
    }

I use exampel from thymeleaf documentation http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#dynamic-fields

The problem is that when the ArrayList is empty, the input fields do not show on the page! Also when edit publisher that have subpublishers, if i remove all of them i lose input fields and I can't add any other subpublishers. Which pretty much makes it impossible to add an subpublishers.

How is this handled with Thymeleaf?
Loading...