【问题标题】:how to pass value from dynamic table to bootstrap modal如何将值从动态表传递到引导模式
【发布时间】:2021-03-23 15:15:39
【问题描述】:

我通过使用 PHP 从 SQL 获取数据创建了一个动态表。每行都有一个链接到模态的编辑按钮。我想将值从表传递到模态,以便我可以编辑它。

我尝试循环遍历表行并能够获取不同列的值。但是,每次我单击任何编辑按钮时,只有最后一行被传递到模态输入。

这是我的标记: 模态

<div class="modal fade" id="modalCategory" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                <div class="modal-dialog modal-sm" role="document">
                                    <form role="form" method="POST" action="php/add_category.php">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <h5 class="modal-title" id="exampleModalLabel">Category</h5>
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                <span aria-hidden="true">&times;</span>
                                                </button>
                                            </div>
                                            <div class="modal-body">
                                                <div class="form-group">
                                                    <input type="hidden" class="form-control" name="categoryID" id="categoryID">
                                                    <label for="category">Category</label>
                                                    <input type="text" class="form-control" name="category" required id="category">
                                                </div>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                                <button type="submit" class="btn btn-primary" name="submitCategory" id="submitCategory">Save</button>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>

表格

<table id="main-table" class="footable table table-stripped toggle-arrow-tiny default breakpoint footable-loaded" data-page-size="15">
                                    <thead>
                                        <tr>
                                            <th data-toggle="true" class="footable-visible footable-first-column footable-sortable">ID<span class="footable-sort-indicator"></span></th>
                                            <th data-hide="phone" class="footable-visible footable-sortable">Category Name<span class="footable-sort-indicator"></span></th>
                                            <th class="text-right footable-visible footable-last-column" data-sort-ignore="true">Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach($categories as $category){ ?>  
                                        <tr class="footable-even" style="">
                                            <td class="footable-visible footable-first-column" id="tdCategoryID"><span class="footable-toggle"></span>
                                                <?php echo $category['categoryID']; ?>
                                            </td>
                                            <td class="footable-visible" id="tdCategory">
                                            <?php echo $cakeOrdering->escape($category['category']); ?>
                                            </td> 
                                            <td class="text-right footable-visible footable-last-column">
                                                <div class="btn-group">
                                                    <button class="btn-white btn btn-xs">Delete</button>
                                                    <button class="btn-white btn btn-xs" data-toggle="modal" data-target="#modalCategory" id="editCategory">Edit</button>
                                                </div>
                                            </td>
                                        </tr>
                                    <?php } ?>
                                    </tbody>
                                </table>

脚本

<script  type="text/javascript">
   $(document).ready(function () {
        var table = document.getElementById("main-table");
        $('#main-table tr').each(function(i, row){
            var $row = $(row);

            var category = $row.find('td:nth-child(2)').text().trim();
            console.log(category);
            $('#category').val(category);
        });   
    });
</script>

这是输出 Output

当我尝试将值打印到控制台时。 Console.log

【问题讨论】:

    标签: javascript jquery bootstrap-modal


    【解决方案1】:

    要实现您的要求,您可以挂钩show.bs.modal 事件。在事件处理程序中,您可以获得对被单击按钮的引用。您可以使用该引用遍历 DOM 以找到包含类别名称的相关 td。最后,您可以在具有该类别名称的模态中设置input 的值。

    顺便说一句,我强烈建议您从您在 PHP 循环中创建的 HTML 内容中删除 id 属性,因为 id 在 DOM 中必须是唯一的。同样,删除内联 style 属性,因为样式应该放在外部样式表中。

    说了这么多,试试这个:

    $('#modalCategory').on('show.bs.modal', e => {
      var $button = $(e.relatedTarget);
      $('#category').val($button.closest('td').prev().text().trim());
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    
    <div class="modal fade" id="modalCategory" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog modal-sm" role="document">
        <form role="form" method="POST" action="php/add_category.php">
          <div class="modal-content">
            <div class="modal-header">
              <h5 class="modal-title" id="exampleModalLabel">Category</h5>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body">
              <div class="form-group">
                <input type="hidden" class="form-control" name="categoryID" id="categoryID">
                <label for="category">Category</label>
                <input type="text" class="form-control" name="category" required id="category">
              </div>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
              <button type="submit" class="btn btn-primary" name="submitCategory" id="submitCategory">Save</button>
            </div>
          </div>
        </form>
      </div>
    </div>
    
    <table id="main-table" class="footable table table-stripped toggle-arrow-tiny default breakpoint footable-loaded" data-page-size="15">
      <thead>
        <tr>
          <th data-toggle="true" class="footable-visible footable-first-column footable-sortable">ID<span class="footable-sort-indicator"></span></th>
          <th data-hide="phone" class="footable-visible footable-sortable">Category Name<span class="footable-sort-indicator"></span></th>
          <th class="text-right footable-visible footable-last-column" data-sort-ignore="true">Action</th>
        </tr>
      </thead>
      <tbody>
        <tr class="footable-even">
          <td class="footable-visible footable-first-column">
            <span class="footable-toggle"></span>
            CategoryID_1
          </td>
          <td class="footable-visible">
            Category 1
          </td>
          <td class="text-right footable-visible footable-last-column">
            <div class="btn-group">
              <button class="btn-white btn btn-xs">Delete</button>
              <button class="btn-white btn btn-xs" data-toggle="modal" data-target="#modalCategory" id="editCategory">Edit</button>
            </div>
          </td>
        </tr>
        <tr class="footable-even">
          <td class="footable-visible footable-first-column">
            <span class="footable-toggle"></span>
            CategoryID_2
          </td>
          <td class="footable-visible">
            Category 2
          </td>
          <td class="text-right footable-visible footable-last-column">
            <div class="btn-group">
              <button class="btn-white btn btn-xs">Delete</button>
              <button class="btn-white btn btn-xs" data-toggle="modal" data-target="#modalCategory" id="editCategory">Edit</button>
            </div>
          </td>
        </tr>
      </tbody>
    </table>

    【讨论】:

    • 这成功了!谢谢@Rory。我已经尝试了 2 天来循环遍历表格行。原来我需要的只是show.bs.modal,我目前不知道。感谢您提供更清晰的解释,也感谢其他 cmets。
    • 另外,我还输入了隐藏的类别 ID。如果我像这样$('#categoryID').val($button.closest('td').prev().prev().text().trim()); 做两个.prev() 来访问categoryID 是否正确?
    【解决方案2】:

    您正在每个循环内的输入框中设置类别的值,这是设置最后一个值的原因。相反,您可以在编辑按钮上编写click 事件,以便单击此按钮获取类别名称并将其放入模态输入框。

    演示代码

    $(document).ready(function() {
      //on click modal buton
      $(".editCategory").on("click", function() {
        var category = $(this).closest("tr").find('td:nth-child(2)').text().trim(); //get cat name
        $('#category').val(category); //set value
    
      })
    });
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <div class="modal fade" id="modalCategory" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog modal-sm" role="document">
        <form role="form" method="POST" action="php/add_category.php">
          <div class="modal-content">
            <div class="modal-header">
              <h5 class="modal-title" id="exampleModalLabel">Category</h5>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                    <span aria-hidden="true">&times;</span>
                                                    </button>
            </div>
            <div class="modal-body">
              <div class="form-group">
                <input type="hidden" class="form-control" name="categoryID" id="categoryID">
                <label for="category">Category</label>
                <input type="text" class="form-control" name="category" required id="category">
              </div>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
              <button type="submit" class="btn btn-primary" name="submitCategory" id="submitCategory">Save</button>
            </div>
          </div>
        </form>
      </div>
    </div>
    Table
    
    <table id="main-table" class="footable table table-stripped toggle-arrow-tiny default breakpoint footable-loaded" data-page-size="15">
      <thead>
        <tr>
          <th data-toggle="true" class="footable-visible footable-first-column footable-sortable">ID<span class="footable-sort-indicator"></span></th>
          <th data-hide="phone" class="footable-visible footable-sortable">Category Name<span class="footable-sort-indicator"></span></th>
          <th class="text-right footable-visible footable-last-column" data-sort-ignore="true">Action</th>
        </tr>
      </thead>
      <tbody>
    
        <tr class="footable-even" style="">
          <td class="footable-visible footable-first-column"><span class="footable-toggle"></span> 1
          </td>
          <td class="footable-visible">
            abc
          </td>
          <td class="text-right footable-visible footable-last-column">
            <div class="btn-group">
              <button class="btn-white btn btn-xs">Delete</button>
              <!--use class here-->
              <button class="btn-white btn btn-xs editCategory" data-toggle="modal" data-target="#modalCategory">Edit</button>
            </div>
          </td>
        </tr>
    
        <tr class="footable-even" style="">
          <td class="footable-visible footable-first-column"><span class="footable-toggle"></span> 2
          </td>
          <td class="footable-visible">
            abcd
          </td>
          <td class="text-right footable-visible footable-last-column">
            <div class="btn-group">
              <button class="btn-white btn btn-xs">Delete</button>
              <!--use class here-->
              <button class="btn-white btn btn-xs editCategory" data-toggle="modal" data-target="#modalCategory">Edit</button>
            </div>
          </td>
        </tr>
      </tbody>
    </table>

    【讨论】:

    • 谢谢。这解释了为什么我只得到该类别的最后一行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 2019-03-15
    • 2014-11-13
    • 2020-02-24
    • 1970-01-01
    相关资源
    最近更新 更多