简体   繁体   English

隐藏表格行时 HTML 列宽会发生变化

[英]HTML column width changes when table row is hidden

I am a relative newcomer to web programming, so probably I am making some obvious mistake here.我是网络编程的新手,所以我可能在这里犯了一些明显的错误。

When I am trying to hide a row in a table from javascript like rows[i].style.display = 'none', the table layout is getting completely broken.当我试图从 javascript 中隐藏表格中的一行时,如rows[i].style.display = 'none',表格布局完全被破坏了。 Originally, the cell content was getting wrapped, and the table width was getting shrunk.最初,单元格内容被包裹,表格宽度正在缩小。 I then added style="table-layout: fixed" in the table tag and style="white-space:nowrap" in each td.然后我在表格标签中添加了 style="table-layout: fixed" 并在每个 td 中添加了 style="white-space:nowrap"。 This stopped the line wrapping, but still the content was not aligned on a line.这停止了​​换行,但内容仍然没有对齐。 Cells started moving left if there is space and column width varied from one row to another.如果从一行到另一行有空间和列宽变化,则单元格开始向左移动。 I then added a fixed width to each of the th and td element and also to the div containing the table.然后,我为每个 th 和 td 元素以及包含表格的 div 添加了一个固定宽度。 Still the problem remained.问题仍然存在。

My current HTML is something like the following.我当前的 HTML 类似于以下内容。


<div style="width: 300px">
  <table id="errorTable" width="100%" cellpadding="5" cellspacing="2" style="table-layout: fixed"> 
    <tr id="HeaderRow">
      <th style="width: 100px;">Header 1</th>
      <th style="width: 50px;">Header 2</th>
      <th style="width: 150px;">Header 3</th>
    </tr>
    <tr id="DetailRow1">                                        
      <td style="white-space:nowrap; width: 100px;">Data 1_1 in Row 1</td>
      <td style="white-space:nowrap; width: 50px;">Data 1_2  in Row 1</td>
      <td style="white-space:nowrap; width: 150px;">Data 1_3 in Row 1</td>
    </tr>
    <tr id="DetailRow2">                                        
      <td style="white-space:nowrap; width: 100px;">Data 2</td>
      <td style="white-space:nowrap; width: 50px;">Data 2</td>
      <td style="white-space:nowrap; width: 150px;">Data 2</td>
    </tr>
    <tr id="DetailRow3">                                        
      <td style="white-space:nowrap; width: 100px;">Data 3_1</td>
      <td style="white-space:nowrap; width: 50px;">Data 3_2</td>
      <td style="white-space:nowrap; width: 150px;">Data 3_3</td>
    </tr>
  </table>
</div>

When the table is displayed first time, the columns are aligned properly, with width 100, 50 and 150 px respectively.第一次显示表格时,列对齐正确,宽度分别为 100、50 和 150 像素。 But if a row, say the second one is hidden, the cell width of the remaining two displayed rows are no longer fixed at 100, 50 and 150 and data is no longer aligned vertically.但是如果一行,比如说第二行被隐藏,剩下的两个显示行的单元格宽度不再固定为 100、50 和 150,并且数据不再垂直对齐。 Please note that the overall table width remains 300 px.请注意,整个表格宽度仍为 300 像素。 Data in each cell moves left if there is available space and the additional space is used by the last, in this case, third column.如果有可用空间,则每个单元格中的数据将向左移动,并且额外的空间被最后一列(在本例中为第三列)使用。

The following post was helpful but did not solve my problem fully, as you can see.如您所见,以下帖子很有帮助,但并没有完全解决我的问题。 Hiding table rows without resizing overall width 隐藏表格行而不调整整体宽度

Any help will be most welcome.任何帮助将是最受欢迎的。

Hope this could help who are struggling with the same problem.希望这可以帮助那些正在努力解决同样问题的人。 Instead of using display: none;而不是使用display: none; I used visibility: collapse;我使用了visibility: collapse; for the hidden rows.对于隐藏的行。 This still keeps the width of the columns and the whole layout.这仍然保持列的宽度和整个布局。

The problem is the display type that you use to make the table-row visible.问题在于您用来使表格行可见的显示类型。
To hide a table-row use display="none"要隐藏表格行,请使用display="none"
To show a table-row use display="table-row"要显示表格行,请使用display="table-row"
I did a sample so you can see these in action.我做了一个样本,所以你可以看到这些在行动。

 function show(){ document.getElementById('trB').style.display='table-row'; } function hide(){ document.getElementById('trB').style.display='none'; }
 @import url("https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css"); table{ border: 1px solid #ccc; }
 <table id="myTable" class="table table-striped table-hover"> <thead> <tr> <td>#</td> <td>Letter</td> </tr> </thead> <tbody> <tr id="trA"> <td>1</td> <td>A</td> </tr> <tr id="trB"> <td>2</td> <td>B</td> </tr> <tr id="trC"> <td>3</td> <td>C</td> </tr> </tbody> </table> <button id="showB" onclick="show()">show B</button> <button id="hideB" onclick="hide()">hide B</button>

I had the same problem: I tried to hide a column by elem.style.display="none" but when showing again the layout was broken.我遇到了同样的问题:我试图通过elem.style.display="none"隐藏一列,但是当再次显示时,布局被破坏了。 This display-style worked for me:这种显示风格对我有用:

columns.item(i).style.display = "table-cell";

Always fix the width of your columns.始终固定列的宽度。 Would that sole your problem?这会是你的唯一问题吗?

.myTable td{ width:33% !important; }

Ideally, you should also enclose header and body sections using thead and tbody理想情况下,您还应该使用theadtbody将 header 和 body 部分括起来

<table>
  <thead>
    <tr> ... </tr>
  </thead>

  <tbody> 
    .  
    .      
    .
  </tbody>
</table>

I'm having the same problem.我有同样的问题。 I threw a span inside of each table to see if I could force the width and it seems to work.我在每张桌子里面扔了一个跨度,看看我是否可以强制宽度,它似乎有效。 It's ugly though.虽然很丑。

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

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