【问题标题】:Creating a html table with dynamically expanding number of columns to fit screen size创建一个具有动态扩展列数以适应屏幕大小的 html 表
【发布时间】:2012-06-29 14:39:23
【问题描述】:

我想在 HTML 表格中显示一长串数据(例如说州及其缩写)。而不是在 2 x 50 表中显示数据,我希望它更紧凑以适应更大的屏幕(因此用户不必上下滚动,但仍然不必水平滚动)

所以这会动态增长:

状态 |抗体 ------------------ 阿拉巴马州 |铝 阿拉斯加 | AK 亚利桑那 | AZ 阿肯色州 |增强现实 加利福尼亚 |加州 科罗拉多 |一氧化碳 康涅狄格 |电脑断层扫描

到:

状态 |抗体 |状态 |抗体 | -------------------------------------- 阿拉巴马州 |铝 |加利福尼亚 |加利福尼亚州 | 阿拉斯加 | AK |科罗拉多 |一氧化碳 | 亚利桑那 |亚利桑那州 |康涅狄格 |计算机断层扫描 | 阿肯色州 |增强现实 |

或者这个:

状态 |抗体 |状态 |抗体 |状态 |抗体 | -------------------------------------------------- -------- 阿拉巴马州 |铝 |阿肯色州 |增强现实 |科罗拉多 |一氧化碳 | 阿拉斯加 | AK |加利福尼亚 |加利福尼亚州 |康涅狄格 |计算机断层扫描 | 亚利桑那 |亚利桑那州 |

等等……

取决于屏幕宽度。

我在 Rail 3 应用程序中使用 jQuery 执行此操作,因此我更喜欢 jQuery 解决方案,但欢迎任何想法。

【问题讨论】:

  • SO see 已经回答了相同类型的问题在 nnnnnn 用户给出的答案中,您需要再添加一列:)

标签: javascript jquery html


【解决方案1】:

这是一个无表格解决方案的演示。它目前仅设置为在页面加载时排列列,并且需要一些小的添加来调整大小。

演示:http://jsfiddle.net/DPRsj/

它基于我拥有的一个 json 数组,数组看起来像:

   [{"state":"AK","name":"Alaska"}........]

html 可以发送到页面而不需要解析 json,需要获取状态列表长度的细微差别

HTML:

<div id="state_wrap">
    <div id="state_header"></div>
    <div id="state_list"></div>   
</div>

CSS:(非常基础)

.state{  width:150px;  border:1px solid grey; border-top:none}
.st_name{ width: 120px;float:left}
.st_ab{ width:30px; margin-left: 120px;}
#state_header .state{
    font-weight:bold;
    color:blue;
    margin-right:10px;
    width: 150px;
    float:left;
}
.column{ width: 152px; float:left;margin-right:10px;}

JS:

$(function() {
    /* parse json to html*/
    var html = [];    
    $.each(states, function() {
        html.push(stateContainer(this.name, this.state));
    });    
    html.push('<div style="clear:both"></div>');    
    $('#state_list').html(html.join(''))
     /* END parse json to html*/

    var listW = 160, /* column width, including margin, set by css*/
        contW = $('#state_wrap').width();
    var num_cols = Math.floor(contW / listW)
    /* create headings*/
    var topHtml = [];
    for (i = 0; i < num_cols; i++) {
        topHtml.push(stateContainer('State', 'Abr'))
    }

    $('#state_header').html(topHtml.join(''));
    /*END create headings, START: create columns*/
    var start = 0,
        end = 0;
    var state_per_col = Math.floor(states.length / num_cols);
    var $states = $('#state_list .state')
    for (i = 0; i < num_cols; i++) {
        start = end;
        end = start + state_per_col + 1;
        $states.slice(start, end).wrapAll('<div class="column"></div>')
    }
});

function stateContainer(name, abr) {
    return '<div class="state"><div class="st_name">' + name + '</div><div class="st_ab">' + abr + '</div></div>';
}

【讨论】:

  • 为什么不想使用表格来表示数据?
【解决方案2】:

找不到现成的解决方案,但这个功能应该可以解决问题!

/*
This function will create a table with fixed `COL_WIDTH' (default=150 px) 
that uses as many columns as can be fit into the HTML element with id `table_id'.

A one-dimensional array of values to fill the table with should be passed in as 
`data'.
*/
function fillScreenWithData(table_id, data, COL_WIDTH){
    if (!COL_WIDTH) COL_WIDTH = 150;

    TABLE = $(table_id);
    alert(TABLE);
    TABLE_STRING = "<table>"
    var doc_width = TABLE.width();
    var num_cols = Math.floor(doc_width / COL_WIDTH);
    var num_rows = Math.ceil(data.length / num_cols); 

    for (var i = 0; i < num_rows; i++){
        TABLE_STRING += "<tr>";
        for (var j = 0; j < num_cols; j++){
            TABLE_STRING += "<td>";
            index = i + j*num_rows;
            if (index < data.length) {
                TABLE_STRING += data[index];
            }
            TABLE_STRING += "</td>"
        }
        TABLE_STRING += "</tr>"
    }
    TABLE_STRING += "</table>"
    TABLE.html(TABLE_STRING);
}

// probably fetch this JSON array of data with an AJAX request
var data = ["Alabama", 
        "Alaska",
        "Arkansas",
        "California",
        "Colorado",
        "Connecticut",
        "Extra Item",
        ]

// run the function on page load
$(function(){fillScreenWithData("#table", data)});

【讨论】:

    【解决方案3】:

    根据浏览器的要求以及列标题对您的重要性,css3 有一个您可以设置的 column-width css 属性。 (目前需要 -webkit 和 -moz 前缀。)

    【讨论】:

      猜你喜欢
      • 2019-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-08
      相关资源
      最近更新 更多