【问题标题】:Bootstrap Collapse Panel with dynamic ID not working具有动态 ID 的引导折叠面板不起作用
【发布时间】:2017-08-21 05:55:18
【问题描述】:

我有一个带有面板的页面,这些面板是用 PHP 动态制作的。每个制作的项目都会制作一个新面板。但是当我想打开其中一个面板时,只打开了第一个面板。因此,我从数据库中检索 ID,并希望将其用作面板的 ID。

当我检查其中一个面板时,它显示 #5,它是数据库中的 ID 之一。

但不幸的是,这种方式行不通,我不知道为什么。有人可以看看它并帮助我朝好的方向发展吗?这是我的代码:

<?php

$sql = "SELECT * FROM project";
$result = mysqli_query($db,$sql);
while($row = mysqli_fetch_array($result)){
    $id = $row['id'];
    $panelid = "#$id";?>
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
    <div class="panel panel-default">
        <div class="panel-heading" role="tab" id="faqhead">
            <h4 class="panel-title">
                <?php
                echo '<a role="button" data-toggle="collapse" data-parent="#accordion" href="'. $panelid .'" aria-expanded="false" aria-controls="collapseOne">';
                echo $row['projectname'];
                echo '</a>';
                ?>
            </h4>
        </div>
        <?php echo '<div id="'. $panelid.'" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">'; ?>
            <div class="panel-body" id="faqpanel">
                <h1 class="">Taken
                    <small>
                        <a role="button" data-toggle="modal" data-target="#newtask"><span class="glyphicon glyphicon-plus project" style="color:#000;"></span></a>
                    </small>
                </h1>
                <table class="table table-hover">
                    <thead>
                    <tr>
                        <th style="width: 5%;"></th>
                        <th style="width: 5%;">Taak ID</th>
                        <th style="width: 10%;">Taak naam</th>
                        <th style="width: 20%;">Omschrijving</th>
                        <th style="width: 10%;">Start datum</th>
                        <th style="width: 10%;">Einddatum</th>
                        <th style="width: 30%;">Notities</th>
                        <th style="width: 10%">Duur</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<?php } ?>  

更新:

【问题讨论】:

  • 您的数据库中有多少个 id?
  • 此时为 3 @PhpDev
  • 如我所见:您根据用户 ID 更改的唯一内容是面板 div 的 id 属性。你到底愿意做什么?
  • 我猜您正在尝试填充 div 中的表格。也许?
  • div中的表格不是问题。就是当创建一个新项目时,会显示另一个面板。但是当我点击面板时,只有第一个会打开。因为面板的所有 ID 都是相同的。所以我试图从我的数据库中填充它们,所以每个面板 ID 都是唯一的。

标签: php twitter-bootstrap dynamic panel


【解决方案1】:

不要只使用数字作为 id 这将不起作用。使用一些文本和连接 ID,例如 colapse_1、colapse_2

<?php

$i = 1;
while($i < 5){
    $id = $i;
    ?>
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
    <div class="panel panel-default">
        <div class="panel-heading" role="tab" id="faqhead">
            <h4 class="panel-title">
                <?php
                echo '<a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse_'. $id .'" aria-expanded="false" aria-controls="collapseOne">';
                echo $id;
                echo '</a>';
                ?>
            </h4>
        </div>
        <?php echo '<div id="collapse_'. $id.'" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">'; ?>
            <div class="panel-body" id="faqpanel">
                <h1 class="">Taken
                    <small>
                        <a role="button" data-toggle="modal" data-target="#newtask"><span class="glyphicon glyphicon-plus project" style="color:#000;"></span></a>
                    </small>
                </h1>
                <table class="table table-hover">
                    <thead>
                    <tr>
                        <th style="width: 5%;"></th>
                        <th style="width: 5%;">Taak ID</th>
                        <th style="width: 10%;">Taak naam</th>
                        <th style="width: 20%;">Omschrijving</th>
                        <th style="width: 10%;">Start datum</th>
                        <th style="width: 10%;">Einddatum</th>
                        <th style="width: 30%;">Notities</th>
                        <th style="width: 10%">Duur</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?php $i++; } ?> 

【讨论】:

  • 我将 '$panelid = "#$id";?>' 更改为 '$panelid = "#collapse_$id";'但这仍然不起作用。
  • 也适用于 div
  • 我还需要 2 个代表才能做到这一点@lakhvir
猜你喜欢
  • 2020-07-27
  • 2018-07-17
  • 2014-06-22
  • 2018-03-16
  • 1970-01-01
  • 2013-09-20
  • 1970-01-01
  • 2015-03-29
相关资源
最近更新 更多