这是一个无表解决方案的演示。它目前仅设置为在页面加载时排列列,并且需要一些小的添加来调整大小处理。
演示: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>';
}