简体   繁体   English

如何使 Bootstrap 下拉菜单显示在粘性表格标题的前面?

[英]How to make a Bootstrap dropdown show up in front of sticky table headers?

I followed a CSS Tricks tutorial to get a table with a sticky header and first column, and tried adding a Bootstrap dropdown inside of the first column.我按照CSS 技巧教程获得了一个带有粘性 header 和第一列的表格,并尝试在第一列内添加 Bootstrap 下拉列表。

Problem is, the dropdown shows up in front of the parent cell but behind all of the others of the first column:问题是,下拉列表显示在父单元格的前面,但在第一列的所有其他单元格后面: 在此处输入图像描述

I've tried changing the position of the cell to relative, switching the z-index of the dropdown menu to a higher value but the dropdown is still behind the first column.我尝试将单元格的 position 更改为相对值,将下拉菜单的 z-index 切换为更高的值,但下拉菜单仍位于第一列之后。

Here's a JSFiddle with a minimum, reproducible example: https://jsfiddle.net/maxencelav/hwsrcmL6/14/这是一个 JSFiddle,其中包含一个最小的、可重现的示例: https://jsfiddle.net/maxencelav/hwsrcmL6/14/

 @import url("https://fonts.googleapis.com/css2?family=Fraunces:ital,wght@0,700;1,200&display=swap"); table { font-family: "Fraunces", serif; font-size: 125%; white-space: nowrap; margin: 3px; border: none; border-collapse: separate; border-spacing: 0; table-layout: fixed; border: 1px solid black; } table td, table th { border: 1px solid black; padding: 0.5rem 1rem; } table thead th { padding: 3px; position: sticky; top: 0; z-index: 1; width: 25vw; background: white; } table td { background: #fff; padding: 4px 5px; text-align: center; } table tbody th { font-weight: 100; font-style: italic; text-align: left; position: relative; } table thead th:first-child { position: sticky; left: 0; z-index: 2; } table tbody th { position: sticky; left: 0; background: white; z-index: 1; } caption { text-align: left; padding: 0.25rem; position: sticky; left: 0; } [role="region"][aria-labelledby][tabindex] { width: 100%; max-height: 98vh; overflow: auto; } #team-cell-container { display: grid; grid-template-columns: 1fr auto; align-items: center; margin: 0; gap: 5px; }
 <script src="https://code.jquery.com/jquery-3.4.1.slim.js"></script> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script> <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet" /> <:-- Based off https.//css-tricks.com/a-table-with-both-a-sticky-header-and-a-sticky-first-column/ --> <table> <thead> <tr> <th>Teams</th> <th>1</th> <th>2</th> <th>3</th> <th>4</th> <th>5</th> <th>6</th> <th>7</th> <th>8</th> <th>9</th> <th>Runs</th> </tr> </thead> <tbody> <tr> <th> <div id="team-cell-container"> Milwaukee Brewers <div class="dropdown"> <i class="fa fa-cog" type="button" id="dropdown-item" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></i> <div class="dropdown-menu" aria-labelledby="dropdown-item"> <a class="dropdown-item" href="#">Action</a> <a class="dropdown-item" href="#">Another action</a> <a class="dropdown-item" href="#">Something else here</a> </div> </div> </div> </th> <td>2</td> <td>1</td> <td>4</td> <td>0</td> <td>3</td> <td>1</td> <td>1</td> <td>0</td> <td>1</td> <td>13</td> </tr> <tr> <th>Los Angles Dodgers</th> <td>4</td> <td>2</td> <td>0</td> <td>0</td> <td>3</td> <td>2</td> <td>4</td> <td>2</td> <td>3</td> <td>20</td> </tr> <tr> <th>New York Mets</th> <td>0</td> <td>1</td> <td>2</td> <td>0</td> <td>0</td> <td>2</td> <td>4</td> <td>0</td> <td>3</td> <td>12</td> </tr> <tr> <th>St. Louis Cardinals</th> <td>3</td> <td>4</td> <td>1</td> <td>3</td> <td>2</td> <td>4</td> <td>3</td> <td>2</td> <td>3</td> <td>25</td> </tr> </tbody> </table>

This is problem with CSS position and z-index .这是 CSS positionz-index的问题。 It was explained by this question .这是由这个问题解释的。 (Or see my testing ). (或查看我的测试)。

To make position: absolute with high z-index appears on top of other relative, you have to set z-index of the others to be lower than z-index of the parent dropdown.要使position: absolute具有高z-index绝对值出现在其他相关项之上,您必须将其他项的z-index设置为低于父下拉列表的z-index This confirmed work by this answer .这证实了这个答案的工作。

First, add some identity to your table.首先,向表中添加一些标识。

<!-- Based off https://css-tricks.com/a-table-with-both-a-sticky-header-and-a-sticky-first-column/ -->

<table id="my-table">
  <thead>
    <tr>
      <th>Teams</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
      <th>4</th>
      <th>5</th>
      <th>6</th>
      <th>7</th>
      <th>8</th>
      <th>9</th>
      <th>Runs</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>
        <div id="team-cell-container">
          Milwaukee Brewers
          <div class="dropdown">
            <i class="fa fa-cog" type="button" id="dropdown-item" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></i>
            <div class="dropdown-menu" aria-labelledby="dropdown-item">
              <a class="dropdown-item" href="#">Action</a>
              <a class="dropdown-item" href="#">Another action</a>
              <a class="dropdown-item" href="#">Something else here</a>
            </div>
          </div>
        </div>
      </th>
      <td>2</td>
      <td>1</td>
      <td>4</td>
      <td>0</td>
      <td>3</td>
      <td>1</td>
      <td>1</td>
      <td>0</td>
      <td>1</td>
      <td>13</td>
    </tr>
    <tr>
      <th>Los Angles Dodgers</th>
      <td>4</td>
      <td>2</td>
      <td>0</td>
      <td>0</td>
      <td>3</td>
      <td>2</td>
      <td>4</td>
      <td>2</td>
      <td>3</td>
      <td>20</td>
    </tr>
    <tr>
      <th>New York Mets</th>
      <td>0</td>
      <td>1</td>
      <td>2</td>
      <td>0</td>
      <td>0</td>
      <td>2</td>
      <td>4</td>
      <td>0</td>
      <td>3</td>
      <td>12</td>
    </tr>
    <tr>
      <th>St. Louis Cardinals</th>
      <td>3</td>
      <td>4</td>
      <td>1</td>
      <td>3</td>
      <td>2</td>
      <td>4</td>
      <td>3</td>
      <td>2</td>
      <td>3</td>
      <td>25</td>
    </tr>
    <tr>
      <th>Houston Astros</th>
      <td>3</td>
      <td>2</td>
      <td>1</td>
      <td>4</td>
      <td>2</td>
      <td>3</td>
      <td>1</td>
      <td>3</td>
      <td>1</td>
      <td>20</td>
    </tr>
    <tr>
      <th>Toronto Blue Jays</th>
      <td>2</td>
      <td>4</td>
      <td>4</td>
      <td>1</td>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
      <td>1</td>
      <td>22</td>
    </tr>
    <tr>
      <th>Boston Red Sox</th>
      <td>4</td>
      <td>4</td>
      <td>4</td>
      <td>0</td>
      <td>3</td>
      <td>0</td>
      <td>0</td>
      <td>1</td>
      <td>1</td>
      <td>17</td>
    </tr>
    <tr>
      <th>Chicago Cubs</th>
      <td>1</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
      <td>2</td>
      <td>2</td>
      <td>2</td>
      <td>0</td>
      <td>4</td>
      <td>12</td>
    </tr>
    <tr>
      <th>Philadelphia Phillies</th>
      <td>0</td>
      <td>4</td>
      <td>2</td>
      <td>0</td>
      <td>4</td>
      <td>1</td>
      <td>0</td>
      <td>4</td>
      <td>2</td>
      <td>17</td>
    </tr>
    <tr>
      <th>Chicago White Sox</th>
      <td>2</td>
      <td>0</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
      <td>2</td>
      <td>4</td>
      <td>2</td>
      <td>0</td>
      <td>19</td>
    </tr>
    <tr>
      <th>San Diego Padres</th>
      <td>2</td>
      <td>2</td>
      <td>3</td>
      <td>0</td>
      <td>4</td>
      <td>2</td>
      <td>2</td>
      <td>0</td>
      <td>2</td>
      <td>17</td>
    </tr>
    <tr>
      <th>Cleveland Indians</th>
      <td>1</td>
      <td>0</td>
      <td>0</td>
      <td>3</td>
      <td>2</td>
      <td>1</td>
      <td>3</td>
      <td>1</td>
      <td>0</td>
      <td>11</td>
    </tr>
    <tr>
      <th>San Francisco Giants</th>
      <td>1</td>
      <td>3</td>
      <td>1</td>
      <td>4</td>
      <td>1</td>
      <td>1</td>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>17</td>
    </tr>
    <tr>
      <th>Cincinatti Reds</th>
      <td>2</td>
      <td>2</td>
      <td>4</td>
      <td>1</td>
      <td>4</td>
      <td>2</td>
      <td>2</td>
      <td>3</td>
      <td>3</td>
      <td>23</td>
    </tr>
    <tr>
      <th>Minnesota Twins</th>
      <td>4</td>
      <td>4</td>
      <td>1</td>
      <td>0</td>
      <td>2</td>
      <td>0</td>
      <td>3</td>
      <td>1</td>
      <td>3</td>
      <td>18</td>
    </tr>
    <tr>
      <th>Tampa Bay Rays</th>
      <td>4</td>
      <td>1</td>
      <td>3</td>
      <td>0</td>
      <td>2</td>
      <td>1</td>
      <td>4</td>
      <td>0</td>
      <td>1</td>
      <td>16</td>
    </tr>
    <tr>
      <th>Miami Marlins</th>
      <td>0</td>
      <td>3</td>
      <td>0</td>
      <td>4</td>
      <td>3</td>
      <td>3</td>
      <td>0</td>
      <td>1</td>
      <td>4</td>
      <td>18</td>
    </tr>
    <tr>
      <th>Oakland Athletics</th>
      <td>3</td>
      <td>3</td>
      <td>2</td>
      <td>0</td>
      <td>4</td>
      <td>2</td>
      <td>0</td>
      <td>1</td>
      <td>0</td>
      <td>15</td>
    </tr>
    <tr>
      <th>Detroit Tigers</th>
      <td>3</td>
      <td>4</td>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>2</td>
      <td>0</td>
      <td>0</td>
      <td>4</td>
      <td>19</td>
    </tr>
    <tr>
      <th>Pittsburgh Pirates</th>
      <td>2</td>
      <td>2</td>
      <td>0</td>
      <td>3</td>
      <td>0</td>
      <td>2</td>
      <td>2</td>
      <td>2</td>
      <td>2</td>
      <td>15</td>
    </tr>
    <tr>
      <th>Seattle Mariners</th>
      <td>1</td>
      <td>3</td>
      <td>3</td>
      <td>1</td>
      <td>2</td>
      <td>2</td>
      <td>0</td>
      <td>4</td>
      <td>0</td>
      <td>16</td>
    </tr>
    <tr>
      <th>Atlanta Braves</th>
      <td>4</td>
      <td>3</td>
      <td>0</td>
      <td>3</td>
      <td>4</td>
      <td>4</td>
      <td>4</td>
      <td>4</td>
      <td>3</td>
      <td>29</td>
    </tr>
  </tbody>
</table>
</div>

Then use Bootstrap dropdown events to listen on show or hide the dropdown element.然后使用 Bootstrap 下拉事件来监听显示或隐藏下拉元素。

$('#my-table .dropdown').on('show.bs.dropdown', (event) => {
    let thisTable = event.target.closest('table');
    thisTable.classList.add('dropdowns-opened');
    // add class to all <tr>
    $(thisTable).find('tr').addClass('non-dropdown');
    // remove class of this <tr>
    event.target.closest('tr').classList.remove('non-dropdown');
});

$('#my-table .dropdown').on('hidden.bs.dropdown', (event) => {
    let thisTable = event.target.closest('table');
    thisTable.classList.remove('dropdowns-opened');
    $(thisTable).find('tr').removeClass('non-dropdown');
});

The JavaScript above will add and remove class when dropdown element show or hide.上面的 JavaScript 将在下拉元素显示或隐藏时添加和删除 class。

Now, add some CSS class to make dropdown appears on top of other.现在,添加一些 CSS class 使下拉菜单出现在其他菜单之上。

table.dropdowns-opened tbody tr.non-dropdown th {
    z-index: 0;
}

See it in action on JsFiddle .在 JsFiddle 上查看实际效果

You maybe change your current z-index: 1 to 2 and use the class above from z-index: 0 to 1 if the sticky functional is not work as expected.如果粘性函数没有按预期工作,您可以将当前z-index: 1更改为2并使用上面的 class 从z-index: 01

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM