dongrao9454 2019-03-23 20:24 采纳率: 100%
浏览 122

有没有办法通过bootstrap获得多个下拉菜单?

I am trying to show, in a dropdown, the students beloging to the same class. But with my current code, the dropdown content for all classes are the same.

I started with these lines of code:

    // Get all the classnames
    $aAllClasses = $this->fetchAllClasses();
    // Create a menubutton foreach classname
    foreach($aAllClasses as $aClassname){
      echo("<a class='link dropdown-toggle' href='#' id='navbarDropdown' role='button' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>".$aClassname[0]." </a>"); 
      // Create the list with students for this class
      echo("<ul class='dropdown-menu' aria-labelledby='navbarDropdown'>");                    
      // Get all the students from this class
      $aClassStudents = $this->fetchClassStudents($aClassname[0]);
      // Create the dropdown list
      foreach($aClassStudents as $aStudentRecord){
          echo("<li><a class='dropdown-item' href='student.php' data-toggle='tooltip' title='Edit'>".$aStudentRecord['sStudentSurname']."</a></li>");                                                                                                                                            
      }
      echo("</ul><br>");     
    }

According to this answer Bootstrap Dropdown shows double? Weird One

I changed

    href='#' into href='javascript:void();'

nothing changed. Then I saw at W3schools this snippet https://www.w3schools.com/bootstrap4/bootstrap_dropdowns.asp

Also no joy

On basis of this site I changed the labeling ID's https://www.quackit.com/html/html_editors/scratchpad/?example=/bootstrap/bootstrap_4/tutorial/bootstrap_4_navbars_dropdown_menus

    id='#Dropdown".$aClassname[0]."'
    aria-labelledby='Dropdown".$aClassname[0]."'

New code:

    // Get all the classnames
    $aAllClasses = $this->fetchAllClasses();
    // Create a menubutton foreach classname
    foreach($aAllClasses as $aClassname){
      echo("<a class='link dropdown-toggle' href='#' id='#Dropdown".$aClassname[0]."' role='button' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>".$aClassname[0]." </a>"); 
      // Create the list with students for this class
      echo("<ul class='dropdown-menu' aria-labelledby='Dropdown".$aClassname[0]."'>");                    
      // Get all the students from this class
      $aClassStudents = $this->fetchClassStudents($aClassname[0]);
      // Create the dropdown list
      foreach($aClassStudents as $aStudentRecord){
          echo("<li><a class='dropdown-item' href='student.php' data-toggle='tooltip' title='Edit'>".$aStudentRecord['sStudentSurname']."</a></li>");                                                                                                                                            
      }
      echo("</ul><br>");     
    }

Also the same result.

if I remove the 'end unordend list' in the last line both classes and students are viewed, but in a nested way.

Result part 1

Result part 2

Result,part 3

So now I separated the buttons from the content

 $aAllClasses = $this->fetchAllClasses();
 foreach($aAllClasses as $aClassname){
      echo("<a class='link dropdown-toggle' href='#' id='#navbarDropdown".$aClassname[0]."' role='button' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>".$aClassname[0]."</a><br><br>"); 
 }
 foreach($aAllClasses as $aClassname){
     echo("<ul class='dropdown-menu' aria-labelledby='#navbarDropdown".$aClassname[0]."'>");                    
     $aClassStudents = $this->fetchClassStudents($aClassname[0]);
     foreach($aClassStudents as $aStudentRecord){
       echo("<li><a class='dropdown-item' href='exa_classes.php' data-toggle='tooltip' title='Edit'>".$aStudentRecord['sStudentSurname']."</a></li>");                                                                                                                                            
     }
     echo("</ul>");     
   }

Same result, the content of the second class is still the same as the first. And now I run out of idea's. Can someone tell me why I cant get those dropdowns filled with a different content for each class?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 wegame打不开英雄联盟
    • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
    • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
    • ¥30 eclipse开启服务后,网页无法打开
    • ¥30 雷达辐射源信号参考模型
    • ¥15 html+css+js如何实现这样子的效果?
    • ¥15 STM32单片机自主设计
    • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
    • ¥15 不小心不正规的开发公司导致不给我们y码,
    • ¥15 我的代码无法在vc++中运行呀,错误很多