【问题标题】:Using Bootstrap modal with HTML clickable-row in table which is populated by foreach to return clicked row content在由 foreach 填充的表中使用带有 HTML 可点击行的 Bootstrap 模式以返回点击的行内容
【发布时间】:2021-07-30 10:53:10
【问题描述】:

我有一个 HTML 表,该表是通过使用来自 MVC MySql 查询的 foreach 的数组填充的。我在表格行中使用 class="clickable-row" 打开一个模式,目的是使用新信息更新该表格行中的任何单元格,然后通过表单更新 MySql 数据库。我的问题是,因为表格是由 foreach 填充的,所以当我单击一行时,模式会填充表格内容中的最后一行。如何让它返回点击的行内容? HTML -->

            <th>Update</th>
            <th class="numeric-sort">Waybill</th>
            <th>Date</th>
            <th>Client</th>
            <th>From</th>
            <th>To</th>
            <th>Supplier</th>
            <th>Supplier Wb</th>
            <th class="numeric-sort">Billable wght</th>
            <th class="numeric-sort">Service</th>
            <th>Pmnt method</th>
            <th>Insurance</th>
            <th class="numeric-sort">Desc</th>
            <th>Cost net</th>
            <th>Cost VAT</th>
            <th>Cost Total</th>
            <th>Sale</th>
            <th>GP</th>
            <th>Supplier Inv</th>
            <th>Inv No.</th>
            <th>POD.</th>
            <th>Comp.</th>
            
        </tr>
        </thead>
<?php 
    foreach ($orderbook_infos as $orderbook_info) { ?>
        <tr class='clickable-row' data-toggle="modal" data-target="#editModal" style="cursor:pointer">
                <td><button class="btn btn-primary btn-sm">Update</button></td>
                <td><?php echo $orderbook_info['order_id']; ?></td>
                <td><?php echo mb_strimwidth($orderbook_info['date_added'], 0, 10); ?></td>
                <td><?php echo $orderbook_info['payment_company']; ?></td>
                <td><?php echo mb_strimwidth($orderbook_info['collection_city'], 0, 12); ?></td>
                <td><?php echo mb_strimwidth($orderbook_info['delivery_city'], 0, 12); ?></td>
                <td><?php echo "Seabourne"; ?></td>
                <td><?php echo "400156784"; ?></td>
                <td contenteditable><?php echo $orderbook_info['billable_weight']; ?></td>
                <td><?php echo $orderbook_info['service_selected']; ?></td>
                <td><?php echo $orderbook_info['payment_method']; ?></td>
                <td><?php echo $orderbook_info['insured_value']; ?></td>
                <td><?php echo $orderbook_info['package']; ?></td>
                <td><?php echo "92.00"; ?></td>
                <td><?php echo "123.00"; ?></td>
                <td><?php echo "495.99"; ?></td>
                <td><?php echo $orderbook_info['total']; ?></td>
                <td><?php echo $orderbook_info['total'] - 92; ?></td>
                <td><?php echo "SB123456"; ?></td>
                <td><?php echo "425716"; ?></td>
                <td><input type="checkbox" name="pod" />&nbsp;</td>
                <td><input type="checkbox" name="comp" />&nbsp;</td>
                
                
          </tr>
      <?php } ?>
  </table>

MODAL

<!-- Edit  Job modal -->
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <a href="#" data-dismiss="modal"> <img src="images/arrow-back-512.png" width="30px" height="30px"> <small>Back</small>
            </a> <span id="myModalLabel" style="margin-left:20px;"><font size="+2"><b>Update entry</b></font></span> </div>
        <div class="modal-body">
            <form class="form-horizontal" method="post" action="InsertPassportReminderSrvlt?idEmployee=1">
                <fieldset id="modal_form">

                    <!-- Text input-->
                    <div class="form-group">
                        <!--<label class="col-md-4 control-label" for="textinput">Date Renewal Due</label>-->
                        <label class="col-md-4 control-label" for="textinput">Enter new number</label>
                        <div class="col-md-6">
                            <input name="dateRenewalDue" type="text" class="form-control input-md" value = "<?php echo $orderbook_info['order_id']; ?>">
                        </div>
                    </div>

                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">Save</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    </div>
                </fieldset>
            </form>

        </div>
    </div>
</div>

【问题讨论】:

    标签: php html jquery twitter-bootstrap-3


    【解决方案1】:

    你需要创建一个这样的 jQuery 代码:

    <script>
        // When the modal is show
        $('#editModal').on('show.bs.modal', function (event) {
            var target = $(event.relatedTarget);
    
            // Get the order_id from the attribute
            var order_id = target.data('order-id');
    
            var modal = $(this);
    
            // Find the input from modal and put a value in it
            modal.find('input[name=dateRenewalDue]').val(order_id);
        })
    </script>
    

    为了从我们的 jQuery 代码中获取order_id。我们需要在tr标签上添加一个数据属性

    <tr class='clickable-row' data-toggle="modal" data-target="#editModal" data-order-id ="<?php echo $orderbook_info['order_id']; ?>" style="cursor:pointer">
    

    因为我们已经将 data-order-id 添加到我们的 tr 标记中。通过 jQuery,我们可以在触发模式时获取订单 ID

    Bootstrap modal docs

    【讨论】:

    • 工作 100% thx。不幸的是,我无法投票,因为我没有足够的代币。
    • 您将如何填充 但该字段为空?
    • 嗨@AdrianB,我可以知道你想要实现什么。
    • 我在使用 html 选择下拉列表的表单中没有得到任何结果 但在
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多