gpt4 book ai didi

javascript - array.length AngularJS 的未定义错误

转载 作者:行者123 更新时间:2023-12-03 08:02:39 25 4
gpt4 key购买 nike

我正在将 xml 转换为 html 表,但在内部 for 循环中,尽管有 Row[i].cell.length ,但我收到 undefined 错误该单元格中的SPACER

查找演示 here

需要的输出表 - Table

注意:您将收到的警报是该行的长度

最佳答案

节点内部有单个元素时,会引发错误。

我已经修改了将字符串元素转换为数组的代码,但在更正错误后出现了其他错误,我试图更正,但我认为应该对它们进行审查。

JSFiddle

这是新代码

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

关于javascript - array.length AngularJS 的未定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34526898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com