简体   繁体   English

表格行显示/隐藏*没有*列宽调整大小,带表格列表:自动

[英]Table Row SHOW / HIDE *Without* Column Width Resizing, w/ TableLayout: auto

I have table with multiple rows, showing items for sale. 我有多行表,显示待售物品。 When the user clicks on a row, a Javascript inserts / shows a new row right beneath it with details about the item. 当用户点击一行时,Javascript会在其下方插入/显示一个新行,其中包含有关该项目的详细信息。 The issue is when the description is long, it forces the column widths to readjust / resize. 问题是当描述很长时,它会强制列宽度重新调整/调整大小。 This shifts the columns positions and is really annoying, especially for the user. 这会改变列位置并且非常烦人,特别是对于用户而言。 Right now, I have my table.style.tableLayout: auto. 现在,我有我的table.style.tableLayout:auto。 I actually prefer it this way, because the columns are adjusted to the content. 我实际上更喜欢这种方式,因为列调整到内容。

My question is: how do I dynamically "lock" the widths of the columns in my table so that when I insert / show the new row, the columns do not readjust / resize? 我的问题是:如何动态“锁定”表格中列的宽度,以便在插入/显示新行时,列不会重新调整/调整大小?

I've tried: 我试过了:

  1. dynamically setting the table to temporarily "tableLayout: fixed" 动态地将表设置为临时“tableLayout:fixed”
  2. inserting / showing my new row 插入/显示我的新行
  3. changing the table back to "tableLayout: auto" 将表格更改回“tableLayout:auto”

Actions 1 & 2 works in in FireFox, but not in Safari and IE (6 & 7). 操作1和2适用于FireFox,但不适用于Safari和IE(6和7)。 However, doing all three seems to prevent the columns from shifting too much. 然而,做这三个似乎可以防止列移动太多。

The frustration is unbearable ... loosing lots of sleep ... please help! 沮丧是无法忍受的......失去了很多睡眠......请帮助!

Thanks. 谢谢。

For those looking for the code (this is done in jQuery). 对于那些寻找代码的人(这是在jQuery中完成的)。 This also assumes the first row has the proper widths for each cell. 这也假设第一行具有适合每个单元格的宽度。 Pretty easy changes if needed. 如果需要,很容易改变。

$('table.class_of_table_to_fix tr:first td').each(function() {
   $(this).css({'width': $(this).width()+"px"});
});

I would set a percent width on each column simply as a guide. 我会在每列上设置百分比宽度作为指导。 Set it just once on the TH of each column. 在每列的TH上只设置一次。 The browser will still adjust the columns to content if necessary, but the columns will stay in place more consistently. 如有必要,浏览器仍会将列调整为内容,但列将更加一致地保留在原位。

Next, I would never put css "white-space:nowrap" anywhere on that table. 接下来,我永远不会把css“white-space:nowrap”放在那张桌子的任何地方。 A long description should not break the table layout, it should wrap around properly on multiple lines, and be readable if you set the widths on each column to suit the type of data. 长描述不应该破坏表格布局,它应该在多行上正确环绕,并且如果您在每列上设置宽度以适合数据类型,则可读。 Similarly I would keep the use of (non breakable spaces) to dates, times and numbers and allow the text to wrap. 类似地,我会继续使用(不可破坏的空格)日期,时间和数字,并允许文本换行。

Other than that, I do this at my job on a dialy basis, and there's a time when you need to stop ulling hairs asking the browser to do something it's not designed to do. 除此之外,我在我的工作中以透明的方式做到这一点,并且有一段时间你需要停止向头发询问浏览器做一些它不打算做的事情。 Content should flow and adapt. 内容应该流动和适应。 Locking column widths to pixels is 99.99999% of the time a bad idea. 将列宽度锁定为像素是错误想法的99.99999%。

PS: If you really, reeally, REALLY need to lock columns, the only solution I'm aware of that works with CSS2 and accross all browsers is to use images. PS:如果你真的,真的,真的需要锁定列,我知道唯一适用于CSS2并且适用于所有浏览器的解决方案是使用图像。 You could insert a 1px high transparent gif image in each column, and counting in the padding of the cells (TD), set a pixel width on each image (IMG), instead of on the columns (TH/TD). 您可以在每列中插入1px高透明gif图像,并计算单元格的填充(TD),在每个图像(IMG)上设置像素宽度,而不是在列(TH / TD)上设置像素宽度。 You could hide those in the TH for example. 例如,您可以隐藏TH中的那些。 You can leave the images at 1 pixel wide and set percent widths on TDs, and when you open a new row, you would get each column width minus TD Padding, and set that to the corresponding IMG. 您可以将图像保留为1像素宽并在TD上设置百分比宽度,当您打开新行时,您将获得每个列宽减去TD填充,并将其设置为相应的IMG。 I haven't tried! 我没试过! I just know that in many projects I've worked on, I've used small gif images to lock a minimum vertical spacing between columns, for example. 我只知道在我参与的许多项目中,我使用了小的gif图像来锁定列之间的最小垂直间距。

I had a similar problem when I was implementing a table with groups that could be toggled. 当我实现一个可以切换的组的表时,我遇到了类似的问题。 I wanted the initial ratio between the columns to stay the same without fixing the widths of the columns. 我希望列之间的初始比率保持不变,而不固定列的宽度。 By default the browser would change the widths depending on the visibility of the table's rows, which was undesirable. 默认情况下,浏览器会根据表行的可见性更改宽度,这是不可取的。

I went ahead and followed @faB's suggestion of applying percentages, but doing so using a small script that would calculate the percentages of the th elements and apply them after the initial render. 我继续并遵循@ faB建议应用百分比,但使用一个小脚本来计算第三个元素的百分比并在初始渲染后应用它们。 This made my columns stay the same width, even with all rows hidden. 这使得我的列保持相同的宽度,即使隐藏了所有行。

Here's the script, which uses jQuery: 这是使用jQuery的脚本:

(function($){

    var lock_widths = function() {
        var total_width = $('table').innerWidth();
        var headers = $('table th');
        var leftover = 100;

        $.each(headers, function(ix, el) {
            var header = $(el), width;

            // on the last call use the leftover percentage
            if (ix == headers.length - 1) {
                width = leftover;
            } else {
                leftover -= width = header.outerWidth() / total_width * 100; 
            }

            header.css({'width': width + '%'});
        });
    };

    $(document).ready(lock_widths);

})(jQuery);

Tested in IE7+, Firefox and Chrome. 在IE7 +,Firefox和Chrome中测试过。 This works for my special case because I have header columns as a reference, but it could be rewritten to measure some other columns. 这适用于我的特殊情况,因为我有标题列作为参考,但它可以重写以测量其他一些列。

You can display the details of the row beneath the clicked one in DIV and set its 您可以在DIV中显示所单击的行下方的行的详细信息并进行设置

style="overflow:auto";

so that details will wrap and scrollbar will be available to display entire text. 这样细节就会换行,滚动条可以显示整个文本。

I don´t know if you´re familiar with jquery, but that´s what I would use - in combination with a separate class for the column that´s causing resizing in the new row - to: 我不知道你是否熟悉jquery,但这就是我将使用的 - 与一个单独的类组合在一起导致在新行中调整大小 - 来:

  1. Calculate / get the with of the column 计算/获取列的内容
  2. Set the with of the afore mentioned class 设置前面提到的类的
  3. Add the row 添加行

I haven´t tried it, but that should do it. 我没试过,但应该这样做。

By the way, there are probably other ways to do it, I´m just more familiar with jquery (for point 1. and 2.). 顺便说一下,可能有其他方法可以做到这一点,我对jquery更熟悉(对于第1点和第2点)。

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

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