我有一些数据如下
var data = [ [21, [1,2] ], [22, [1,2] ] ];
var const = [-1 , 0 , 1];
我的要求是生成一个结构如下的表
21, 22 are main column headers
1, 2 are nested columns under 21, 22
对于21,22中的每1,2个,我需要将const值添加为嵌套列标题 所以在上面的案例中 21,22是主列标题, 1,2将是每个21,22下的嵌套列 const值-1,0,1将是每个1,2,22,22下的嵌套列标题 我写了下面的代码。但这没有按预期工作
function simplifyCols(obj) {
var reform = {
table : {}
};
for (var x = 0; x < obj.length; x++) {
obj[x].push( const );
}
for(var i = 0, l = obj.length; i < l; i++){
var key = obj[i];
for (var j = 0, m = key.length; j < m; j++) {
var colspan = 1;
for (var k = j + 1; k < m; k++) {
colspan *= key[k].length;
}
reform.table[j] = reform.table[j] || [];
reform.table[j].push({
span : colspan,
data : key[j]
});
}
}
return reform;
}
var cols = simplifyCols( data ).table;
var keys = Object.keys(cols).sort(function(a, b){
return a - b;
});
var table = document.createElement('table');
document.body.appendChild(table);
for (var i = 0, l = keys.length; i < l; i++) {
var keyData = cols[keys[i]], tr = document.createElement('tr');
table.appendChild(tr);
for (var j = 0, m = keyData.length; j < m; j++) {
var eleData = keyData[j].data;
if(eleData instanceof Array){
for (var k = 0, n = eleData.length; k < n; k++) {
var td = document.createElement('td');
td.innerHTML = eleData[k];
td.colSpan = keyData[j].span;
tr.appendChild(td);
}
}else{
var td = document.createElement('td');
td.innerHTML = keyData[j].data;
td.colSpan = keyData[j].span;
tr.appendChild(td);
}
}
table.appendChild(tr);
}
document.body.appendChild(table);
请帮助改进逻辑并使其正常工作。我在中间被击中,无法继续进一步
以下是我期待的HTML
在这里摆弄http://jsfiddle.net/4NS8d/
<table>
<tr> <td colspan="6">21</td> <td colspan="6">21</td> </tr>
<tr> <td colspan="3">1</td> <td colspan="3">2</td> <td colspan="3">1</td> <td colspan="3">2</td> </tr>
<tr> <td>-1</td> <td>0</td> <td>1</td><td>-1</td> <td>0</td> <td>1</td><td>-1</td> <td>0</td> <td>1</td><td>-1</td> <td>0</td> <td>1</td></tr>
</table>
答案 0 :(得分:0)
首先,你不能命名var const。
试试这个:
var const2 = [-1 , 0 , 1];
function simplifyCols(obj) {
var reform = {
table : {}
};
for (var x = 0; x < obj.length; x++) {
obj[x].push( const2 );
}
for(var i = 0, l = obj.length; i < l; i++){
var key = obj[i];
for (var j = 0, m = key.length; j < m; j++) {
var colspan = 1;
for (var k = j + 1; k < m; k++) {
colspan *= key[k].length;
}
reform.table[j] = reform.table[j] || [];
reform.table[j].push({
span : colspan,
data : key[j]
});
}
}
return reform;
}
var data = [ [21, [1,2] ], [22, [1,2] ] ];
var cols = simplifyCols(data).table;
var keys = Object.keys(cols).sort(function(a, b){
return a - b;
});
var table = document.createElement('table');
document.body.appendChild(table);
for (var i = 0, l = keys.length; i < l; i++) {
var keyData = cols[keys[i]], tr = document.createElement('tr');
table.appendChild(tr);
for (var j = 0, m = keyData.length; j < m; j++) {
var eleData = keyData[j].data;
if(eleData instanceof Array){
for (var k = 0, n = eleData.length; k < n; k++) {
var td = document.createElement('td');
td.innerHTML = eleData[k];
td.colSpan = keyData[j].span;
tr.appendChild(td);
}
}else{
var td = document.createElement('td');
td.innerHTML = keyData[j].data;
td.colSpan = keyData[j].span;
tr.appendChild(td);
}
}
table.appendChild(tr);
}
document.body.appendChild(table);