【问题标题】:Edit Table data with Spring-Boot & Thymeleaf使用 Spring-Boot 和 Thymeleaf 编辑表数据
【发布时间】:2014-09-29 17:27:20
【问题描述】:

我还在学习 Thymeleaf、Bootstrap 和 JQuery,所以请原谅我的无知。我有一个以 Thymeleaf 和 Bootstrap 作为 UI 的 spring-boot 应用程序。我有一个用户列表,我在表格中显示并希望对其进行编辑。我目前的想法是,用户可以选择行上的图标,并将该记录填充到模式对话框表单中,可以通过调用对其进行更新。不确定这是否是最好的,但这是我现在的位置。

这是我的html:

 <div class="container">
        <div class="well well-lg">
            <div class="container-fluid">
                <h5>CNET OFAC Users</h5>

                <div th:if="${userList != null and not #lists.isEmpty(userList)}">
                    Found <span th:text="${#lists.size(userList)}"></span> Users
                </div>
                <div th:if="${userList == null or #lists.isEmpty(userList)}">
                    <div>"No Users were found"</div>
                </div>
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>UserName</th>
                        <th>Company</th>
                        <th>Role</th>
                        <th>Active</th>
                        <th></th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr th:each="user : ${userList}">
                        <td th:text="${user.username}"></td>
                        <td th:text="${user.companyName}"></td>
                        <td th:text="${user.roles[0].authorities}"></td>
                        <td th:text="${user.enabled}"></td>
                        <td><a data-toggle="modal" data-target="#editModal"><span class="glyphicon glyphicon-edit"></span></a></td>

                    </tr>
                    </tbody>
                </table>
                <button data-target="#loginModal" data-toggle="modal" class="btn btn-default">Add User</button>

                <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
            </div>
        </div>
    </div>

其中包括更新表格: × 编辑用户

        <div class="modal-body">
            <form class="form-horizontal bv-form" action="addUser" th:action="@{/addUser}" th:object="${user}" method="put" id="loginForm">
                <div class="form-group">
                    <label class="col-md-3 control-label">Username</label>
                    <div class="col-md-5">
                        <input type="text" name="username" class="form-control" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">Password</label>
                    <div class="col-md-5">
                        <input type="password" name="password" class="form-control" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">Confirm Password</label>
                    <div class="col-md-5">
                        <input type="password" name="cpassword" class="form-control" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">Company</label>
                    <div class="col-md-5">
                        <input type="text" name="companyName" class="form-control" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">Status</label>
                    <div class="col-md-5">
                        <select class="form-control" name="enabled" th:field="*{enabled}">
                            <option value="true">Active</option>
                            <option value="false">Inactive</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-5 control-label">
                        <button class="btn btn-default" type="submit">Update</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div

我被卡住的地方是如何使用单击编辑按钮的行上的记录填充此模式。使用该记录值填充模式的最佳方法是什么,以便我可以将其提交给我的 spring-mvc 控制器进行更新?

【问题讨论】:

    标签: twitter-bootstrap-3 thymeleaf


    【解决方案1】:

    我对你的代码不是很清楚。我假设您尝试为表中的每个数据集打开一个模式。如果我是对的,那么您需要首先注意一件事。当您为动态生成的表格的每一行填充模态时,您不能简单地添加具有单个 ID 的模态。您需要为每个表格行生成模态 ID。因此,您为什么不像这样更改您的代码-

        <td>
    
        <a data-toggle="modal" th:href="'#'+${user.id}"><span class="glyphicon glyphicon-edit"></span></a>
             
        <div class="modal fade" id="view-news" 
    th:id="${news.id}" tabindex="-1" role="dialog" aria-labelledby="edit" aria-hidden="true">
          
          <!-- Every other codes for you modal -->
          
          
        </div>
    
        </td>

    我在这里所做的基本上是为每一行动态创建模态,并为它们分配一个 id,然后用来找到它。

    【讨论】:

      【解决方案2】:

      这是用于管理界面吗?听起来很像,因为那将是一个您可以查看用户列表并对其进行编辑的地方。

      实现这一点的更简单方法是将他们带到编辑用户页面,而不是在模式中进行。在这种情况下,一切都是直接的 HTML/CSS,您可以使用 Spring/Hibernate 验证来验证表单。

      【讨论】:

      • 是的,这是一个管理页面。我有一个用户列表,我考虑过内联编辑,但也不知道该怎么做——这就是我最终得到模态的方式。我可以使表格中的行可编辑,然后保存(PUT)吗?
      • 你肯定可以内联。我只是以为你让自己变得更加艰难(更多的工作)。具体来说,我在考虑如何处理错误情况。您最终可能不得不编写一些自定义的东西。因为 1)您将通过 ajax 执行 PUT,并且您需要为适当的字段正确呈现错误或 2)您将发布浏览器帖子,在这种情况下,如果出现错误,您将需要在其编辑中重绘页面有错误的状态。如果您要定义此页面的要求,只需将它们带到另一个页面进行编辑就应该花费更少的时间。
      猜你喜欢
      • 2021-04-04
      • 2018-02-08
      • 2021-12-20
      • 1970-01-01
      • 2018-10-02
      • 2019-08-29
      • 2020-11-26
      • 2020-11-08
      • 2021-09-03
      相关资源
      最近更新 更多