javascript - table td cell generates tr problem
漂亮男人
漂亮男人 2017-05-16 13:44:37
0
2
613

Suppose there are 10 json objects of td. The colspan attribute value of td is 1-3. Now we need to turn them into td and put them in tr. When the colspan of td is 1, put 3 td and colspan in tr. When colspan is 2, put two tds in tr. When colspan is 3, put 3 tds in tr. How to do it?

漂亮男人
漂亮男人

reply all(2)
PHPzhong
<tr ng-if="$scope.colspan === 1">
    <td></td>
    <td></td>
    <td></td>
</tr>
<tr ng-if="$scope.colspan === 2">
    <td></td>
    <td></td>
</tr>
<tr ng-if="$scope.colspan === 3">
    <td></td>
    <td></td>
    <td></td>
</tr>

Solved with angular>ng-if

仅有的幸福

Just guess what you think. .

var json = [{
    colspan: 1,
    title: 1
}, {
    colspan: 2,
    title: 2
}, {
    colspan: 3,
    title: 3
}]

document.body.innerHTML = '<table>'+ generateTr(json) +'</table>'

function generateTr(json) {
    var tr = []
    for (var i = 0, l = json.length; i < l; i++) {
        var item = json[i],
            tds = []
        for (var j = 0, k = Math.ceil(3 / item.colspan); j < k; j++) {
            tds.push('<td' + (j == 0 ? ' colspan="' + item.colspan+'"' : '') + '>' + item.title + '</td>')
        }
        tr.push('<tr>' + tds.join('') + '</tr>')
    }
    return tr.join('')
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template