简体   繁体   English

如何为模型中的每个元素生成编辑模态?

[英]How to generate edit modals for each element in the model?

I'm trying to use thymeleaf to generate edit and delete modals for each element in the model in my ModelAndView using th:each . 我正在尝试使用百万美元使用th:each为我的ModelAndView中的模型中的每个元素生成编辑和删除模态。

The modals are indeed created and have unique ids based on the id field of the elements. 确实创建了模态,并且基于元素的id字段具有唯一ID。 The problem I have is none of the values from elements are parsed into the input s to enable the user to see the current values. 我遇到的问题是没有将元素的值解析为input以使用户能够看到当前值。

They are obviously there because the view also has a table which displays each element's values along with the anchors which toggle the modals. 它们显然是存在的,因为视图还有一个表格,可以显示每个元素的值以及切换模态的锚点。

Here's some example code of how I'm doing it: 这是我如何做的一些示例代码:

<div th:each="f : ${foos}" th:id="um- + ${f.id}" class="modal fade"
    tabindex="-1" role="dialog">
...
    <form role="form" th:action="@{/foo/update}" th:object="${foo}" th:method="post">
        <input type="hidden" th:field="*{id}" th:value="${f.id}"/>
        <fieldset class="form-group">
            <label for="bar">Bar</label>
            <input th:field="*{bar}" th:value="${f.bar}" class="form-control" 
                id="bar" type="text" placeholder="Bar"/>
        </fieldset>  
        ...             
    </form>
...          
</div> 

How to generate edit modals for each element in the model? 如何为模型中的每个元素生成编辑模态? I'm not sure why thymeleaf is unable to get the values of the fields from the model elements. 我不确定为什么百万美元无法从模型元素中获取字段的值。

That's not a great approach actually. 实际上这不是一个好方法。 In addition to it not working, doing using a loop obviously creates n modals for the collection. 除了它不工作之外,使用循环显然会为集合创建n模态。

The solution that worked best was to provide a single modal that would be populated and submitted with Ajax calls. 最有效的解决方案是提供一个将使用Ajax调用填充和提交的模式。

This no-frills Spring Boot app has all the relavant code. 这个简洁的Spring Boot应用程序具有所有相关代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM