我正在将xml转换为html表,但在内部for循环中,undefined
出现Row[i].cell.length
错误,尽管该单元格中有SPACER
查找演示 here
注意:您将收到的警告是该行的length
答案 0 :(得分:1)
行节点内部有一个元素时会引发错误。
我修改了将字符串元素转换为数组的代码,但在纠正错误之后,其他人已经尝试纠正,但我认为应该对其进行审核。
这里是新代码
var myApp = angular.module('myApp', []);
myApp.controller("MyCtrl", function($scope) {
$scope.allText = '<Table rowSize="22"><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpan="true">Cat. No. </Cell><Cell cellWidth="2795" realIndex="1">Suitable Copper Conductor Range </Cell><Cell cellWidth="3150" realIndex="2">Limiter Fuse </Cell><Cell cellWidth="1800" realIndex="3" rowSpan="true">SCCR, A </Cell></Row><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpanIndex="0"></Cell><Cell cellWidth="1470" realIndex="1" columnSplitIndex="1">Line </Cell><Cell cellWidth="1325" realIndex="1" columnSplitIndex="1">Load </Cell><Cell cellWidth="1530" realIndex="2" columnSplitIndex="2">Ampere Max </Cell><Cell cellWidth="1620" realIndex="2" columnSplitIndex="2">Fuse Class </Cell><Cell cellWidth="1800" realIndex="3" rowSpanIndex="3"></Cell></Row><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpan="true">DK35H </Cell><Cell cellWidth="1470" realIndex="1" rowSpan="true" columnSplitIndex="1">1/0 - 12 </Cell><Cell cellWidth="1325" realIndex="1" rowSpan="true" columnSplitIndex="1">1/0 - 12 </Cell><Cell cellWidth="1530" realIndex="2" columnSplitIndex="2">225 </Cell><Cell cellWidth="1620" realIndex="2" columnSplitIndex="2">J </Cell><Cell cellWidth="1800" realIndex="3" rowSpan="true">100,000 </Cell></Row><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpanIndex="0"></Cell><Cell cellWidth="1470" realIndex="1" rowSpanIndex="1" columnSplitIndex="1"></Cell><Cell cellWidth="1325" realIndex="1" rowSpanIndex="2" columnSplitIndex="1"></Cell><Cell cellWidth="1530" realIndex="2" columnSplitIndex="2">225 </Cell><Cell cellWidth="1620" realIndex="2" columnSplitIndex="2">T </Cell><Cell cellWidth="1800" realIndex="3" rowSpanIndex="5"></Cell></Row><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpanIndex="0"></Cell><Cell cellWidth="1470" realIndex="1" rowSpanIndex="1" columnSplitIndex="1"></Cell><Cell cellWidth="1325" realIndex="1" rowSpanIndex="2" columnSplitIndex="1"></Cell><Cell cellWidth="1530" realIndex="2" columnSplitIndex="2">100 </Cell><Cell cellWidth="1620" realIndex="2" columnSplitIndex="2">RK1 </Cell><Cell cellWidth="1800" realIndex="3" rowSpanIndex="5"></Cell></Row><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpanIndex="0"></Cell><Cell cellWidth="1470" realIndex="1" rowSpanIndex="1" columnSplitIndex="1"></Cell><Cell cellWidth="1325" realIndex="1" rowSpanIndex="2" columnSplitIndex="1"></Cell><Cell cellWidth="1530" realIndex="2" columnSplitIndex="2">30 </Cell><Cell cellWidth="1620" realIndex="2" columnSplitIndex="2">RK5 </Cell><Cell cellWidth="1800" realIndex="3" rowSpanIndex="5"></Cell></Row><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpanIndex="0"></Cell><Cell cellWidth="1470" realIndex="1" rowSpanIndex="1" columnSplitIndex="1"></Cell><Cell cellWidth="1325" realIndex="1" rowSpanIndex="2" columnSplitIndex="1"></Cell><Cell cellWidth="1530" realIndex="2" columnSplitIndex="2">60 </Cell><Cell cellWidth="1620" realIndex="2" columnSplitIndex="2">G </Cell><Cell cellWidth="1800" realIndex="3" rowSpanIndex="5"></Cell></Row><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpanIndex="0"></Cell><Cell cellWidth="1470" realIndex="1" rowSpanIndex="1" columnSplitIndex="1"></Cell><Cell cellWidth="1325" realIndex="1" rowSpanIndex="2" columnSplitIndex="1"></Cell><Cell cellWidth="1530" realIndex="2" columnSplitIndex="2">30 </Cell><Cell cellWidth="1620" realIndex="2" columnSplitIndex="2">CC </Cell><Cell cellWidth="1800" realIndex="3" rowSpanIndex="5"></Cell></Row><Row cellSize="1"><Cell cellWidth="9245" realIndex="0" colSpanSize="4"> SPACER </Cell></Row><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpan="true">DKK4-TF (5X20) UPPER </Cell><Cell cellWidth="1470" realIndex="1" rowSpan="true" columnSplitIndex="1">10 - 18 </Cell><Cell cellWidth="1325" realIndex="1" rowSpan="true" columnSplitIndex="1">10 18 </Cell><Cell cellWidth="1530" realIndex="2" columnSplitIndex="2">60 </Cell><Cell cellWidth="1620" realIndex="2" columnSplitIndex="2">J </Cell><Cell cellWidth="1800" realIndex="3" rowSpan="true">100,000 </Cell></Row><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpanIndex="0"></Cell><Cell cellWidth="1470" realIndex="1" rowSpanIndex="1" columnSplitIndex="1"></Cell><Cell cellWidth="1325" realIndex="1" rowSpanIndex="2" columnSplitIndex="1"></Cell><Cell cellWidth="1530" realIndex="2" columnSplitIndex="2">60 </Cell><Cell cellWidth="1620" realIndex="2" columnSplitIndex="2">T </Cell><Cell cellWidth="1800" realIndex="3" rowSpanIndex="5"></Cell></Row><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpanIndex="0"></Cell><Cell cellWidth="1470" realIndex="1" rowSpanIndex="1" columnSplitIndex="1"></Cell><Cell cellWidth="1325" realIndex="1" rowSpanIndex="2" columnSplitIndex="1"></Cell><Cell cellWidth="1530" realIndex="2" columnSplitIndex="2">30 </Cell><Cell cellWidth="1620" realIndex="2" columnSplitIndex="2">RK1 </Cell><Cell cellWidth="1800" realIndex="3" rowSpanIndex="5"></Cell></Row><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpanIndex="0"></Cell><Cell cellWidth="1470" realIndex="1" rowSpanIndex="1" columnSplitIndex="1"></Cell><Cell cellWidth="1325" realIndex="1" rowSpanIndex="2" columnSplitIndex="1"></Cell><Cell cellWidth="1530" realIndex="2" columnSplitIndex="2">- </Cell><Cell cellWidth="1620" realIndex="2" columnSplitIndex="2">RK5 </Cell><Cell cellWidth="1800" realIndex="3" rowSpanIndex="5"></Cell></Row><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpanIndex="0"></Cell><Cell cellWidth="1470" realIndex="1" rowSpanIndex="1" columnSplitIndex="1"></Cell><Cell cellWidth="1325" realIndex="1" rowSpanIndex="2" columnSplitIndex="1"></Cell><Cell cellWidth="1530" realIndex="2" columnSplitIndex="2">50 </Cell><Cell cellWidth="1620" realIndex="2" columnSplitIndex="2">G </Cell><Cell cellWidth="1800" realIndex="3" rowSpanIndex="5"></Cell></Row><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpanIndex="0"></Cell><Cell cellWidth="1470" realIndex="1" rowSpanIndex="1" columnSplitIndex="1"></Cell><Cell cellWidth="1325" realIndex="1" rowSpanIndex="2" columnSplitIndex="1"></Cell><Cell cellWidth="1530" realIndex="2" columnSplitIndex="2">30 </Cell><Cell cellWidth="1620" realIndex="2" columnSplitIndex="2">CC </Cell><Cell cellWidth="1800" realIndex="3" rowSpanIndex="5"></Cell></Row><Row cellSize="1"><Cell cellWidth="9245" realIndex="0" colSpanSize="4"> SPACER </Cell></Row><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpan="true">DKK4-TF (5X20) LOWER </Cell><Cell cellWidth="1470" realIndex="1" rowSpan="true" columnSplitIndex="1">10 18 </Cell><Cell cellWidth="1325" realIndex="1" rowSpan="true" columnSplitIndex="1">10 18 </Cell><Cell cellWidth="1530" realIndex="2" columnSplitIndex="2">60 </Cell><Cell cellWidth="1620" realIndex="2" columnSplitIndex="2">J </Cell><Cell cellWidth="1800" realIndex="3" rowSpan="true">100,000 </Cell></Row><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpanIndex="0"></Cell><Cell cellWidth="1470" realIndex="1" rowSpanIndex="1" columnSplitIndex="1"></Cell><Cell cellWidth="1325" realIndex="1" rowSpanIndex="2" columnSplitIndex="1"></Cell><Cell cellWidth="1530" realIndex="2" columnSplitIndex="2">60 </Cell><Cell cellWidth="1620" realIndex="2" columnSplitIndex="2">T </Cell><Cell cellWidth="1800" realIndex="3" rowSpanIndex="5"></Cell></Row><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpanIndex="0"></Cell><Cell cellWidth="1470" realIndex="1" rowSpanIndex="1" columnSplitIndex="1"></Cell><Cell cellWidth="1325" realIndex="1" rowSpanIndex="2" columnSplitIndex="1"></Cell><Cell cellWidth="1530" realIndex="2" columnSplitIndex="2">30 </Cell><Cell cellWidth="1620" realIndex="2" columnSplitIndex="2">RK1 </Cell><Cell cellWidth="1800" realIndex="3" rowSpanIndex="5"></Cell></Row><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpanIndex="0"></Cell><Cell cellWidth="1470" realIndex="1" rowSpanIndex="1" columnSplitIndex="1"></Cell><Cell cellWidth="1325" realIndex="1" rowSpanIndex="2" columnSplitIndex="1"></Cell><Cell cellWidth="1530" realIndex="2" columnSplitIndex="2">- </Cell><Cell cellWidth="1620" realIndex="2" columnSplitIndex="2">RK5 </Cell><Cell cellWidth="1800" realIndex="3" rowSpanIndex="5"></Cell></Row><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpanIndex="0"></Cell><Cell cellWidth="1470" realIndex="1" rowSpanIndex="1" columnSplitIndex="1"></Cell><Cell cellWidth="1325" realIndex="1" rowSpanIndex="2" columnSplitIndex="1"></Cell><Cell cellWidth="1530" realIndex="2" columnSplitIndex="2">50 </Cell><Cell cellWidth="1620" realIndex="2" columnSplitIndex="2">G </Cell><Cell cellWidth="1800" realIndex="3" rowSpanIndex="5"></Cell></Row><Row cellSize="4"><Cell cellWidth="1500" realIndex="0" rowSpanIndex="0"></Cell><Cell cellWidth="1470" realIndex="1" rowSpanIndex="1" columnSplitIndex="1"></Cell><Cell cellWidth="1325" realIndex="1" rowSpanIndex="2" columnSplitIndex="1"></Cell><Cell cellWidth="1530" realIndex="2" columnSplitIndex="2">30 </Cell><Cell cellWidth="1620" realIndex="2" columnSplitIndex="2">CC </Cell><Cell cellWidth="1800" realIndex="3" rowSpanIndex="5"></Cell></Row></Table>';
$scope.processData = function(allText) {
var xmlText = allText.replace(/\n/, '')
var x2js = new X2JS();
var json = x2js.xml_str2json(xmlText);
var str = JSON.stringify(json);
$scope.json = str;
$scope.data = json.Table.Row;
for (var i = 0; i < json.Table.Row.length; i++) {
//var cells = json.Table.Row[i].Cell.length;
var header = 0;
// Transform the object into array
if (json.Table.Row[i].Cell.constructor !== Array) {
var asArray = [];
asArray.push(json.Table.Row[i].Cell);
json.Table.Row[i].Cell = asArray;
}
//alert( json.Table.Row[i].Cell.length)
for (var j = 0; j < json.Table.Row[i].Cell.length; j++) {
//alert(json.Table.Row[i].Cell[j].__text);
//Row Span initialization
if (json.Table.Row[i].Cell[j]._rowSpan) {
json.Table.Row[i].Cell[j]['rSpan'] = '1';
}
//Column Span
if (json.Table.Row[i].Cell[j]._colSpanSize != null) {
var limit = json.Table.Row[i].Cell[j]._colSpanSize;
var count = 0;
while (limit > 0) {
if (json.Table.Row[0].Cell[header].cSpan != null) {
count += json.Table.Row[0].Cell[header].cSpan;
} else {
count++;
}
header++;
limit--;
}
json.Table.Row[i].Cell[j]['cSpan'] = count;
}
//Row Span
if (json.Table.Row[i].Cell[j]._rowSpanIndex != null) {
var col = json.Table.Row[i].Cell[j]._rowSpanIndex;
var row = i;
while (!json.Table.Row[row].Cell[col]._rowSpan) {
row--;
}
json.Table.Row[row].Cell[col].rSpan++;
}
//Column Split
if (json.Table.Row[i].Cell[j]._columnSplitIndex != null) {
var count = 1,
col = j,
k = 0;
var index = json.Table.Row[i].Cell[j]._columnSplitIndex;
if (json.Table.Row[i - 1].Cell[k] == null) {
continue;
}
// here check for undefined
while (
json.Table.Row[i - 1].Cell[k] &&
json.Table.Row[i - 1].Cell[k]._realIndex != index) {
k++;
}
// here check for undefined
if (json.Table.Row[i - 1].Cell[k] &&
json.Table.Row[i - 1].Cell[k]._columnSplitIndex != null) {
continue;
}
if (json.Table.Row[0].Cell[index].cSpan != null) {
continue;
}
while (json.Table.Row[i].Cell[col]._columnSplitIndex == json.Table.Row[i].Cell[col + 1]._columnSplitIndex) {
count++;
col++;
}
json.Table.Row[i - 1].Cell[k]['cSpan'] = count;
}
}
}
$scope.data = json.Table.Row;
if (!$scope.$$phase) $scope.$apply();
}
});