【问题标题】:Table column group widths表格列组宽度
【发布时间】:2013-11-26 04:07:11
【问题描述】:

假设我有一个如下表:

|<--                        Fixed width                          ->|
|<- group width     ->|<- group width     ->|
--------------------------------------------------------------------
| Label 1 | Input 1   | Label 2 | Input 2   | Filler (auto-expand) |
| Label 3 | Input 3   | Label 4 | Input 4   |                      |
| Section header (colspan=5)                                       |
--------------------------------------------------------------------
| Label 5 | Input 5   | Label 6 | Input 6   |                      |
| Label 7 | Input 7   | Label 8 | Input 8   |                      |
--------------------------------------------------------------------

我将如何设置它的样式,以便每个列组具有固定的宽度,但每个组下的各个列会自动调整,如下所示:

  1. 标签列调整为在不换行的情况下显示该列文本所需的最小宽度
  2. 输入列扩展以填充组宽度减去标签列宽度的剩余空间。

例子:

|<--                        Fixed width                          ->|
|<- group width     ->|<- group width     ->|
--------------------------------------------------------------------
| Label 1   | Input 1 | Label 2 | Input 2   | Filler (auto-expand) |
| Label 3.. | Input 3 | Label 4 | Input 4   |                      |
| Section header (colspan=5)                                       |
--------------------------------------------------------------------
| Label 5   | Input 5 | Label 6 | Input 6   |                      |

在上面,包含标签 1、3 和 5 的标签列已调整为所有文本不被截断显示所需的最小宽度(标签 3 的宽度)。同时,相应的输入列也进行了调整,使组宽保持不变。

我有一个结构和初始 css here 的 jsfiddle。

如果需要,请随意调整结构,但我希望对其进行最小的更改。我希望只需更改 html 结构和 css 就可以做到这一点。

我也欢迎 jquery 解决方案。虽然我还应该提到标签/输入行可以动态添加,并且列需要适当调整(所以我不能只在初始文档加载时设置固定宽度)。

如果这也可以更改为不再需要表格,那就更好了。

【问题讨论】:

  • 在每个组中嵌套表不会做你想要的吗?然后在父表列上设置固定宽度
  • 它会,除了每隔一段时间我会有跨越一整行的节标题(如上)。这意味着我需要将嵌套表切割成两行(一节标题之前,另一行之后)。如果我这样做了,除了为两者定义固定宽度之外,我不能保证两个嵌套表的列宽保持一致。
  • jsfiddle.net/vin_g/Lvy7G/1 显示了这个问题。在小提琴中,标签 1 和 3 的宽度正确,但在节标题之后,标签 5 的宽度不同。
  • @Vin-G 请看看我的回答。我想这会有所帮助。

标签: javascript jquery html css


【解决方案1】:

this 是您要找的吗?如果是,请查看下面的代码

    // Assume you want widths 60, 90, 120 widths to each header

    var widths = [60, 90, 120];

    $('table td[colspan]').each(function (i, v) {
        var th = $(this),
            width = Math.floor(widths[i] / th.attr('colspan') * 1),
            indStrt = 0;
        th.width(widths[i]);
        th.prevAll().each(function () {
            var ind = $(this).attr('colspan');
            indStrt += ind ? ind * 1 : 1;
        });
        var nextTr = th.closest('tr').next(),
            tds = nextTr.find('td');
        var $siblings = tds.eq(indStrt).nextUntil(tds.eq(indStrt + th.attr('colspan') * 1)).andSelf();
        $siblings.each(function () {
            $(this).width(width);
        });
    });       

根据 cmets 代码是

    var COLUMN_GROUP_WIDTH=100;
    var TABLE_WIDTH=$("#thetable").width();

    // reset width so we can get proper column widths
    $("#thetable").width("auto");

    $('td.label').each(function (e, v) {
        var labelWidth = $(v).width();
        $(v).width(labelWidth);
        console.log(labelWidth);
        $(v).next().width(COLUMN_GROUP_WIDTH-labelWidth);

    });

    // so we can check total widths
    //$(".header").css("display", "table-row");

    $("#thetable").width(TABLE_WIDTH);

【讨论】:

  • 标签列(第 1 列、第 3 列等)需要具有最小宽度以适应这些列中的所有文本。
  • @Vin-G 然后调整header column widths数组的宽度。
  • @Vin-G 希望你学会欣赏那些在 stackoverflow.com 上努力解决你的问题的人
  • *在小提琴中,所有列将具有相同的宽度(相对于列组)。满足列组宽度相同的部分。但是,标签列(第 1 列、第 3 列等)需要具有最小宽度以适应这些列中的所有文本,并且输入列(2、4 等)是可变的。第 1 列和第 2 列将具有不同的宽度,但会增加总列宽。我想您可以为此稍微调整一下代码...
  • 嗯,让我上一张图,让你看看我指的是什么
【解决方案2】:

将此demo 使用table-layout: fixed;

.col1{
    table-layout: fixed;
}
.col2{
    table-layout: fixed;
}
.col1 td{
    width: 200px;
}
.col2 td{
    width: 200px;
}

根据您评论的预期图片查看此updated demo

【讨论】:

  • 嗯,标签列需要最小宽度,同时,标签 5 并没有真正正确排列。见i.imgur.com/K06idhY.png
  • 嗨 C-Link,差不多,但标签 5 的宽度仍然与标签 1 和标签 3 不同(或者是我只看到这个?)。这就是我在回复 Simon Trewhella 时所指的问题。如果您认为需要修改 html 结构以使其正常工作,请随意这样做,只要显示相同的内容(两个列组、每个子组两列、跨越所有列的标题行等) .
  • 如果第 2 列有一个高度不同的单元格,这将不起作用:高度不会对齐
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
  • 1970-01-01
  • 1970-01-01
  • 2018-02-25
  • 1970-01-01
  • 2012-07-15
相关资源
最近更新 更多