【问题标题】:how to load dynamically generated table data into form inside modal?如何将动态生成的表格数据加载到模态内的表单中?
【发布时间】:2018-11-15 07:46:02
【问题描述】:

我有一个动态生成的表。表格如下:-

    <table id="datatables" class="table table-striped table-no-bordered table-hover" 
    cellspacing="0" width="100%" style="width:100%">
    <thead>
    <tr>
        <th>S.N</th>
        <th>Branch Name</th>
        <th>Branch Location</th>
        <th>Status</th>
        <th class="disabled-sorting text-right">Actions</th>
    </tr>
    </thead>
    <tfoot>
    <tr>
        <th>S.N</th>
        <th>Branch Name</th>
        <th>Branch Location</th>
        <th>Status</th>
        <th class="disabled-sorting text-right">Actions</th>
    </tr>
    </tfoot>
    <tbody>
    <tr th:Each="b,iter : ${branches}">
        <td th:text="${iter.index}+1"></td>
        <td th:text="${b.branchName}"></td>
        <td th:text="${b.branchLocation}"></td>
        <td th:text="${b.status}"></td>
        <td style="text-align: right;">
            <button  class="btn btn-warning" data-toggle="modal" data-target="#branchModal">Edit</button>
            <button class="btn btn-warning">Deactivate</button>
        </td>
    </tr>
    </tbody>
</table>

我有引导模式。现在,当我单击表格的编辑按钮时,我想在模态表单中加载单击的表格行数据。

<div class="modal fade" id="branchModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">New message</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <form>
                            <div class="form-group">
                                <label for="branch-name" class="col-form-label">Branch Name:</label>
                                <input type="text" class="form-control" id="recipient-name">
                            </div>
                            <div class="form-group">
                                <label for="branch-location" class="col-form-label">Branch Location:</label>
                                <textarea class="form-control" id="branch-location"></textarea>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button style="margin-right:10px;" type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-primary">Update</button>
                    </div>
                </div>
            </div>
        </div>

我怎样才能实现它?

注意:- 我没有使用 jquery,所以使用纯 javascript 的解决方案将不胜感激。

【问题讨论】:

  • 您可以添加行的id(像这样:&lt;button data-id="10"&gt;Edit&lt;/button&gt;),然后通过使用ajax单击按钮获取数据并将其放入模态!
  • 请问有示例代码吗?

标签: javascript bootstrap-4 thymeleaf


【解决方案1】:

如果您有一个函数 loadDataOnModal 来处理显示您的模式,请将其添加到您的编辑按钮:

th:onclick="| loadDataOnModal('${b.branchName}', '${b.branchLocation}')|"

我看到你只需要在你的模态体中的分支名称和位置。

【讨论】:

  • 按钮是这样的:- 我不确定 th:onclick 在你的代码里面会发生什么。
  • 我更改了答案以匹配您的函数名称。我将 branchName 和 branchLocation 传递给 loadDataOnModal,因此您不必再次获取它。
  • 你提供的代码就像一个魅力。谢谢你。我从来没有在百里香上看到过这样的东西。这对我来说是一种新的优雅的解决方案。
【解决方案2】:

您好,只需在单击事件上运行此代码即可为您希望更改的每个值打开您的模式:

document.getElementById('recipient-name').value = 'Value you wish to set'

您还可以通过传入事件变量来访问您单击的元素的值,如下所示:

document.getElementById('clicked').addEventListener('click', event => {
  document.getElementById('recipient-name').value = event.target.value
})

这是基本概念,您可以从那里进一步了解...

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2020-11-21
    • 2013-02-19
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 2012-04-09
    • 1970-01-01
    相关资源
    最近更新 更多