1

我有一些如下数据

 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 of 21、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>
4

1 回答 1

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);

http://jsfiddle.net/zxvnp/

于 2013-06-19T07:18:18.807 回答