如何将值从动态 table 传递到 bootstrap 模态

how to pass value from dynamic table to bootstrap modal

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

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

这是我的标记: 模态

<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>
                                        <?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

您正在每个循环内的输入框中设置类别值,这是设置最后一个值的原因。相反,您可以在编辑按钮上编写 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>

要实现您的要求,您可以挂钩 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>