我正在尝试将表字符串(如csv)转换为html表。我的代码在简单表上工作正常,但在合并行和列时失败。因此,如何在脚本中使用行跨度和列跨度?

<!DOCTYPE html>
<html ng-app="" ng-controller="myCtrl">
<style>
table, th, td {
border: 1px solid black;
padding:5px;
}
table {
   border-collapse: collapse;
   margin:10px;
}
</style>
<body>
<button ng-click="processData(allText)">
    Display CSV as Data Table
</button>
<div id="divID">
    <table>
        <tr ng-repeat="x in data">
            <td ng-repeat="y in x">{{ y }}</td>
        </tr>
    </table>
</div>
<div>
    <table>
    </table>
</div>

<script>
function myCtrl($scope, $http) {

$scope.allText=" |Through Air |Over Surface |\nRS(0)|in. CM(1)|mm CM(1)|in. CM(2)|mm CM(2)|\nB |3/32\n (a) CM(1)|2.4 \n (a) CM(1)|3/32 \n (a) CM(2)|2.4 \n (a) CM(2)|\nD |1/16\n (a) CM(1)|1.6 \n (a) CM(1)|1/8 \n (a) CM(2)|3.2 \n (a) CM(2)|\nLAST ROW";
$scope.processData = function(allText) {
    // split content based on new line
    var allTextLines = allText.split(/\|\n|\r\n/);
    var headers = allTextLines[0].split('|');
    var lines = [];

    for ( var i = 0; i < allTextLines.length; i++) {
        // split content based on comma
        var data = allTextLines[i].split('|');
        if (data.length == headers.length) {
            var temp = [];
            for ( var j = 0; j < headers.length; j++) {
                temp.push(data[j]);
            }
            lines.push(temp);
        }
    };
    $scope.data = lines;
};
}
</script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
</body>
</html>


| ---是细胞的分界符

| \ n ---用于换行

RS(#)-行跨度列号

CM(#)---列拆分w.r.t列标题

$ scope.allText中的值是CSV表字符串

所以本质上我应该将此表作为输出
javascript - 如何将我的表字符串解析为有角JS中的html表?-LMLPHP

最佳答案

processData函数中,将表示表格单元格的基准设为具有rowSpancolumnSplit属性的对象:

[[{value: 10, rowSpan: 1, columnSplit: 0}, ... ] ... ]


您的示例中的数据似乎有问题,因为它似乎将跨越两列的标题定义为一个“列”,而其他列实际上是一个拆分列。因此,我想在您的情况下,您必须在每一rowSpan中添加1:

<td ng-repeat="y in x" rowSpan="{{y.rowSpan + 1 }}">{{ y.value }}</td>

07-24 18:19