下面是無表解決方案的演示。目前只設置爲在頁面加載時安排列,並且需要一些小的添加來調整大小。
DEMO: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>';
}
相同類型的問題已經在SO上得到了解答[see](http://stackoverflow.com/questions/9562580/make-a-1-columns-per- row-table-into-2-columns-per-row-table-using-jquery)在nnnnnn用戶給出的答案中,你需要再添加一列:) –