【问题标题】:Populating Bootstrap modal w/ sql from table row click从表行单击填充带有 sql 的 Bootstrap 模式
【发布时间】:2015-03-06 17:14:47
【问题描述】:

我整个星期都在努力解决这个问题,最后放弃了,决定直接问。我所拥有的是通过 mysql 查询填充的引导表中的打开作业列表。单击一行时,会出现一个模型,显示作业的详细信息。用户界面完美无缺,但现在我试图让正确的数据显示在模态中,我不知所措。

根据我的阅读,解决方案似乎是使用 AJAX,我对此的经验为零。如果我需要我的模态信息、sql 查询或两者都在单独的页面上,我似乎无法通过阅读其他解决方案和教程来弄清楚。这是我的初始代码:

jobs.php

<table class="table table-hover" data-link="row">
        <thead>
        <tr>
            <th>Job #</th>

            <th>Quote #</th>

            <th>Date Started</th>

            <th>Customer Name</th>

            <th>Description</th>

            <th>Amount</th>

            <th>Date Completed</th>

        </tr>
        </thead>
        <tbody>
            <?php
                while ($row = mysqli_fetch_array($jobs)) {
                    // Print out the contents of the entry
                    echo '<tr>';
                    echo '<td><a href="" data-toggle="modal" data-target="#jobModal" ></a>' . $row['id'] . '</td>';
                    echo '<td>' . $row['quote'] . '</td>';
                    echo '<td>' . $row['started'] . '</td>';
                    echo '<td>' . $row['customer'] . '</td>';
                    echo '<td>' . $row['description'] . '</td>';
                    echo '<td>$' . $row['amt'] . '</td>';
                    echo '<td>' . $row['status'] . '</td>';
                    echo '</tr>';
                }
            ?>
        </tbody>
    </table>

模态 - 同样在 jobs.php 中(忽略占位符,当我开始工作时,它们将被替换为 php 变量)

<div class="modal fade" id="jobModal" tabindex="-1" role="dialog" aria-labelledby="jobModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="jobModalLabel"><strong>Job <?php echo $row['id']; ?></strong></h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-lg-2">
                            Quote #
                        </div>
                        <div class="col-lg-2">
                            PO # 1234567
                        </div>
                        <div class="col-lg-4">
                            Status: In Progress
                        </div>
                        <div class="col-lg-4 text-right">
                            Customer Name
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-4">
                            <br/>
                            Started: 03/01/2015
                        </div>
                        <div class="col-lg-4">
                            <br/>
                            Completed:
                        </div>
                        <div class="col-lg-4 text-right">
                            Primary Contact
                        </div>
                    </div>
                    <hr>
                    <div class="row">
                        <div class="col-lg-6 text-center">
                            <strong>PUMP SPECS</strong><br/><br/>
                        </div>
                        <div class="col-lg-6 text-center">
                            <strong>MOTOR SPECS</strong>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3">
                            Mfg: Taco
                            <br/><br/>
                            Size: 6x4
                        </div>
                        <div class="col-lg-3">
                            Mod: 54654856daf5
                            <br/><br/>
                            S/N: 54654856daf5
                        </div>
                        <div class="col-lg-2 border-left">
                            Mfg: Baldor
                            <br/><br/>
                            Frame: 215T
                        </div>
                        <div class="col-lg-2">
                            HP: 50
                            <br/><br/>
                            Encl: TEFC
                        </div>
                        <div class="col-lg-2">
                            RPM: 1800
                            <br/><br/>
                            Volts: 100
                        </div>
                    </div>
                    <div class="row"></div>
                    <div class="col-lg-2 col-lg-offset-6 border-left">
                        <br/>
                        Amps: 240/360
                    </div>
                    <div class="col-lg-4">
                        <br/>
                        S/N: 54654856daf5
                    </div>
                    <hr>
                    <div class="row">
                        <div class="col-lg-12">
                            <strong>Job Description:</strong>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Edit Job</button>
                </div>
            </div>
        </div>
    </div>

从这一点来看,无论如何我可以让 id 在模态端通过,这样我就可以像使用原始表一样使用 php 填写其余信息?如果我确实需要使用 ajax,那么这样做的正确方法是什么?

【问题讨论】:

  • 所以当你点击 id 时,模态框应该只弹出那个 id 的数据?
  • 如果你想在模态中保存更改,你应该添加一个form,它在第一个input之前开始并在button之后结束,顺便说一下应该是type="submit"如果你想提交表格..
  • 正确,模态应该只弹出该 id 的数据。无需对模态中的数据进行任何更改。

标签: php mysql ajax twitter-bootstrap bootstrap-modal


【解决方案1】:

[编辑]

如果你试试这个呢?

打开模式的表格

<?php
    while ($row = mysqli_fetch_array($jobs)) {
        // Print out the contents of the entry
        echo '<tr>';
        echo '<td><a href="#" data-toggle="modal" data-target="#jobModal'.$row['id'].'" >Click here to edit</a>' . $row['id'] . '</td>';
        echo '<td>' . $row['quote'] . '</td>';
        echo '<td>' . $row['started'] . '</td>';
        echo '<td>' . $row['customer'] . '</td>';
        echo '<td>' . $row['description'] . '</td>';
        echo '<td>$' . $row['amt'] . '</td>';
        echo '<td>' . $row['status'] . '</td>';
        echo '</tr>';
    }
?>

模式

<?php while ($row = mysqli_fetch_array($jobs)) { ?>
    <div class="modal fade" id="jobModal<?php echo $row['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="jobModalLabel<?php echo $row['id']; ?>" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="jobModalLabel<?php echo $row['id']; ?>"><strong>Job <?php echo $row['id']; ?></strong></h4>
                </div>
                <div class="modal-body">
                    ...
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Edit Job</button>
                </div>
            </div>
        </div>
    </div>
<?php } ?>

【讨论】:

  • 模态搜索链接中的data-target ID(需要唯一),因此,您应该将模态ID(和数据目标)中的$row['id'] 设置为指定要打开的模式。最后,您将从数据库中获得一个模态/行。如果有很多行,页面将需要很长时间才能加载。在这种情况下,您应该使用AJAX。请记住,模态仍然存在,但被隐藏了。
  • 这对我来说很有意义,但我仍然不能完全正确。当我复制您的代码时,它告诉我您提供的表格设置行有错误。 id 周围的引号似乎引起了一些问题,我收到了错误:Parse error: syntax error, unexpected 'id' (T_STRING), expecting ',' or ';'
  • 我设法找到了一个解决方案,只需从单独的页面中提取模态数据,我通过将作业 ID 附加到 url 并使用 get 变量来填充该页面。但是,我真的认为您的答案会奏效,而这正是我发布时所寻找的。感谢您的所有帮助。
  • PS:也不是最优的,如果表有 1000 行,会生成 1000 个模态吗?没办法,我正在学习数据绑定——比如 knockoutjs——你也应该这样做
  • @Enrique,如果您有 1000 行,您可以使用 SQL limit 制作一个简单的页面功能,或者您可以在用户请求模式时获取数据。我通常更喜欢根据模式的使用频率以及在同一页面上显示所有行的重要性来限制行。如果您正在寻找特定的行,是否也将页面功能与使用 SQL 的简单搜索功能结合起来?更少的工作,可能更容易阅读代码。
猜你喜欢
  • 1970-01-01
  • 2010-10-08
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-08
  • 1970-01-01
  • 2020-10-09
相关资源
最近更新 更多