简体   繁体   English

具有自动布局的表格的最大宽度

[英]Max width for table with auto layout

I have a table with multiple columns and this table should not exceed a certain width y . 我有一个包含多列的表格,此表格不应超过某个宽度y

The columns of this table are automatically sized by the table-layout: auto. 此表的列由table-layout:auto自动调整大小。

But sometimes the columns get too big (the text should not be wrapped, it should be clipped) and sum up to a width bigger than y . 但有时列太大(文本不应该被包裹,应该被剪裁)并且总和大于y的宽度。

In this case i want the 'biggest' columns to become smaller until the widths of the columns don't exceed y anymore. 在这种情况下,我希望“最大”列变小,直到列的宽度不再超过y

This means that i do not want to shrink alls columns by some factor but rather make the bigger columns "as small as neccessary". 这意味着我不想通过某种因素缩小所有列,而是使更大的列“尽可能小”。

Is this possible through some simple JS or even CSS? 这是通过一些简单的JS甚至CSS来实现的吗?

Here an example: 这是一个例子:

I have 3 columns and a max total width y of 1000px. 我有3列,最大总宽度y为1000px。
Here the sizes of the content of the columns: 这里列的内容大小:

1: 100px 2: 1100px 3: 1300px 1:100px 2:1100px 3:1300px

This should result in a table with those widths: 这应该导致具有这些宽度的表:

1: 100px 2: 450px 3: 450px; 1:100px 2:450px 3:450px;

Here some code: 这里有一些代码:

HTML: HTML:

<div>
    <table>
        <tr>
            <td>SomeText</td>
            <td>SomeOtherTextSomeOtherTextSomeOtherText</td>
            <td>SomeLongerTextSomeLongerTextSomeLongerTextSomeLongerTextSomeLongerText</td>
        </tr>
    </table>
</div>

CSS: CSS:

div {
    border: 1px solid red; 
    width: 500px;
}

table {
    table-layout: auto;
    border: 1px solid grey;
}

td {
    border: 1px solid black;
}

fiddle: http://jsfiddle.net/6zJR4/2/ 小提琴: http//jsfiddle.net/6zJR4/2/

In this example the table is bigger than the containing div. 在此示例中,表格大于包含div。 What i would want is that the table respects the width of the containing div and the columns shrink. 我想要的是该表尊重包含div的宽度和列缩小。 But not all columns should shrink with the same factor eg 30% smaller. 但并非所有色谱柱都应以相同因子收缩,例如缩小30%。 But the bigger columns (maybe all columns would need to shrink in the end) shrink. 但是较大的列(可能所有列最终都需要缩小)会缩小。

My approach: I want to let the browser calculate the 'ideal' sizes of the columns and then adjust the sizes by hand (=JS) to match the maximum width of the containing div. 我的方法:我想让浏览器计算列的“理想”大小,然后手动调整大小(= JS)以匹配包含div的最大宽度。

How i am going to calculate the width of the different columns is not yet clear to me, i opened a mathoverflow question for that: https://math.stackexchange.com/questions/702067/how-to-find-a-set-of-ascending-natural-numbers-which-when-added-to-another-set-o 我将如何计算不同列的宽度尚不清楚,我打开了一个mathoverflow问题: https ://math.stackexchange.com/questions/702067/how-to-find-a-set -of-升序-自然数-这-当添加到另一个设定邻

i hope there is a simpler solution. 我希望有一个更简单的解决方案。

Just use CSS to set TD (table data) 只需使用CSS设置TD(表数据)

td {
    max-width: 800px;
}

Setting a max on table data will guarantee that no column exceeds that width (but may grow in height if overlap isnt specified) 设置表数据的最大值将保证没有列超过该宽度(但如果未指定重叠,则可能会增加高度)

If you want to have different max widths for differnt columns, make multiple classes: 如果要为不同的列设置不同的最大宽度,请创建多个类:

 .col1 {
     max-width: 500px;
 }

 .col2 {
      max-width: 600px;
 }

etc. then just add a class to TD tag. 然后只需在TD标签中添加一个类。 If the table is dynamically created use JQuery to add the class: 如果表是动态创建的,请使用JQuery添加类:

 $( "td" ).addClass( "myClass yourClass" );

When the text is too long we can break them by this CSS property. 当文本太长时,我们可以通过这个CSS属性来破坏它们。

-ms-word-break: break-all;
word-break: break-all;

// Non standard for webkit
word-break: break-word;

in table cell td you can add this CSS. 在表格单元格td您可以添加这个CSS。

td {
    border: 1px solid black;
    -ms-word-break: break-all;
     word-break: break-all;

     // Non standard for webkit
     word-break: break-word;
}

Here is the Demo http://jsfiddle.net/kheema/6zJR4/1/ 这是演示http://jsfiddle.net/kheema/6zJR4/1/

After understanding the actual requirement it would be good to use a very tiny jquery plugin called Succinct can help you. 在了解了实际需求之后,最好使用一个名为Succinct的非常小的jquery插件来帮助你。 here is the link. 链接在这里。 http://micjamking.github.io/succinct/ http://micjamking.github.io/succinct/

add width to your table and it would wrap contents inside table equal to div width, but it wont show excess text table添加width ,它会将表中的内容包装为div宽度,但它不会显示多余的文本

Demo of what u need exactly 演示你需要什么

table.tbl{
    width:100%; /* this will make table equal to div width and clip excess contents*/
    table-layout: fixed;
}

and, to show excess text, word-break them 而且,为了显示多余的文字,请word-break它们

table.tbl{
    width:100%;
    table-layout: fixed;
    word-break:break-all;
}

demo here 在这里演示

I implemented my own solution: 我实现了自己的解决方案:

Here is the code showed in an example: 以下是示例中显示的代码:

JS: JS:

function findOptimalDistribution(Z, z) {
    // Make a working copy
    var Y = Z.slice(0);

    // Add dummy element so the 'lowest' value is 1, increment z accordingly
    Y.unshift(1);
    z++;

    // calculate sum of Y
    var R = 0;
    for(var i = 0; i < Y.length; i++) {
        R += Y[i];
    }

    // R contains the remaining 'overlap' over z
    R -= z;

    // R <= 0 means the condition is already fullfiled
    if (R <= 0) {
        return Y.slice(1);
    }

    for(var i = Y.length - 1, u = 1; i > 0; i--, u++) {
        var c = (Y[i] - Y[i - 1]);

        var b = (R <= c*u);
        if (b) {
            c = Math.floor(R / u);
        }

        for(var j = Y.length - 1; j >= i; j--) {
            Y[j] -= c;
        }
        R -= c * u;

        // Subtract the remainder of R, that got 'lost' due to rounding (Math.floor)
        if (b) {
            for (var j = i; j < i + R; j++) {
                Y[j]--;
            }

            R = 0;
        }

        if (R == 0) {
            break;
        }
    }

    return Y.slice(1);
}

function findOptimalWidths(columns, max_width) {    
    columns.sort(function (a, b) {
        return a.width - b.width;
    });

    var X = columns.map(function(e) {           
        return e.width;
    });

    var optimal = findOptimalDistribution(X, max_width);

    for(var i = 0; i < columns.length; i++) {
        columns[i].width = optimal[i];
    }

    return columns; 
}

function resizeColumns(){
    var columns = [];
    $('table tr:first-child td').each(function(i, e) {
        var $e = $(e);

        $e.css('max-width', 'auto');
        columns.push({elem: $e, width: $e.outerWidth()});
    });



    columns = findOptimalWidths(columns, $('div').innerWidth());

    for(var i = 0; i < columns.length; i++) {
        columns[i].elem.css('max-width', columns[i].width);
    }
}

$(window).resize(function() {
    resizeColumns();
});

resizeColumns();

HTML: HTML:

<div>
    <table>
        <tr>
            <td style="background-color: violet;">
                Line
            </td>
            <td style="background-color: green;">
                LongerLineLongerLineLongerLine
            </td>
            <td style="background-color: yellow;">
               VeryLongLineVeryLongLineVeryLongLineVeryLongLineVeryLongLineVeryLongLineVeryLongLineVeryLongLine
            </td>
        </tr>
    </table>
</div>

CSS: CSS:

table {
    table-layout:fixed;
    border-collapse: collapse;
}

table td {
    overflow: hidden;
    box-sizing: border-box;
    text-overflow: ellipsis;
}

div {
    width: 100%;
    border: 1px solid red;
}

Link to the fiddle: http://jsfiddle.net/ujQh7/2/ 链接到小提琴: http//jsfiddle.net/ujQh7/2/

I believe this is a good and intuitiv behaviour of a table with columns that get truncated. 我相信这是一个表格的良好和直观的行为,其中列被截断。

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

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