简体   繁体   English

在tbody上使用empty()可以调整整个表的宽度

[英]Using empty() on tbody keeps resizing width of entire table

I am using jQuery to reload data inside the tbody . 我正在使用jQuery重新加载tbody数据。 However, the header column width keeps resizing event though I put the fixed width for the columns. 但是,尽管我为列设置了固定的宽度,但标题列的宽度仍会继续调整大小。 I am using Firefox. 我正在使用Firefox。

Simple HTML table: 简单的HTML表:

<table style="width:95%;" border="1" align="center" >
    <thead>
        <tr>
        <td class="header" style="width:20%">ProductFamily
            <select onchange="selectCategoryChange()">
                <option "1">1</option>
                <option "2">2</option>
            </select>
        </td>
        <td class="header" style="width:20%">ProductNum</td>
        <td class="header" style="width:30%">ProductDesc</td>
        <td class="header" style="width:5%">DT Rate</td>
        <td class="header" style="width:5%">List Rate</td>
        <td class="header" style="width:5%">Man Hour Unit</td>
        <td class="header" style="width:5%">Precall</td>
        <td class="header" style="width:5%">SOW</td>
        <td class="header" style="width:5%">Box</td>
         </tr>
    </thead>
    <tbody  id="tbodyDataRow">
        <!-- Data Rows Here -->
    </tbody>
</table>

When the tbody.empty(), it automatically resizes the width of the header columns, which make the page looks ugly. 当使用tbody.empty()时,它会自动调整页眉列的宽度,从而使页面看起来难看。 and then when I reload the new rows, it resize it back to original width. 然后当我重新加载新行时,它将其大小重新设置为原始宽度。 Here is my Jquery code. 这是我的Jquery代码。

$(document).ready(function() {
    selectCategoryChange = function()
    {
        var tbody = $("#tbodyDataRow");
        tbody.empty();

    };
});

This works, but I've not tested it in all browsers. 这可行,但是我没有在所有浏览器中都对其进行过测试。 Change HTML to: 将HTML更改为:

<table border="1" align="center">
    <thead>
        <tr>
            <th>ProductNum</th>
            <th>Product</th>
            <th>DT Rate</th>
            <th>List Rate</th>
            <th>Man Hour Unit</th>
            <th>Precall</th>
            <th>SOW</th>
            <th></th>
        </tr>
    </thead>
    <tbody id="tbodyDataRow">
        <!-- tbody Content -->
    </tbody>
</table>

You should avoid inline styles anyway. 无论如何,您都应避免使用内联样式。 Add this CSS file: 添加此CSS文件:

table {
    border: 1px solid black;
}
.header {
    background-color: #ccc;
    font-weight: bold;
    min-width: 200px;
}

That'll fix your problem, but again it's not cross-browser tested. 这样可以解决您的问题,但是再次,它没有经过跨浏览器的测试。 You might consider allowing the resize. 您可以考虑允许调整大小。

Final result Fiddle 最终结果小提琴

Your table style="width:95%;" 您的表格style="width:95%;" is the problem. 是问题。 You are expecting a table with 8 X 200px = 1600px width, but your table should has 95% as width, which can be more or less than 1600px. 您期望表格的宽度为8 X 200px = 1600px ,但是表格的宽度应为95%,可以大于或小于1600px。 If it be deferent, the headers will be ajusted automatic. 如果不同,则标题将自动调整。 I'm sorry about my english. 我的英语不好意思。

I had a similar problem with empty() function applied on ul element. 我对ul元素应用了empty()函数时遇到了类似的问题。 HTML structure : HTML结构:

<table>
  <tr>
    <td>
        <ul id="list"></ul>
    </td>
    <td>
     ...
    </td> 
  </tr>

I have a JS timer which call an ajax function responsible to refresh the content of the list (ul). 我有一个JS计时器,它调用ajax函数来刷新列表(ul)的内容。 To update the list (ul) I started to empty it with the following code: 要更新列表(ul),我开始使用以下代码将其清空:

$("#list").empty();

At each iteration, the column width was incremented even if content was the same. 在每次迭代中,即使内容相同,列宽也会增加。 I finally replaced the content by the initial content by using replaceWith function and this time it was ok: 最后,我通过使用replaceWith函数将内容替换为初始内容,这次还可以:

$("#list").replaceWith('<ul id="list"></ul>');

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

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