8

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

所以这会动态增长:

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

至:

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

或这个:

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

ETC....

取决于屏幕宽度。

我正在使用 jQuery 在 Rail 3 应用程序中执行此操作,因此我更喜欢 jQuery 解决方案,但对任何想法都持开放态度。

4

3 回答 3

1

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

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

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

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

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

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>';
}
于 2012-06-28T06:35:58.843 回答
1

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

/*
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)});
于 2012-06-28T05:41:13.063 回答
0

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

于 2012-06-28T08:10:18.517 回答