2015-12-30 89 views
0

我将XML转换到HTML表格,但在内部进行循环我'得到undefined错误Row[i].cell.length inspite该小区有SPACER为array.length AngularJS未定义的错误

查找演示 here

所需输出表 - Table

注意:警惕你会得到是该行的length

+4

对于入门时调试JS,你应该使用'console.log'而不是警觉戒备是非常有限的,它停止执行JS其中可能导致假阳性情况 – maurycy

+0

@maurycy好吧,但任何想法为什么它有一个对象时说'undefined'? – insomniac

回答

1

的错误是RAIS当节点有单个元素里面。

我已经修改了字符串元素转化成数组的代码,但纠正错误后,已经出来了别人,我想正确的,但我觉得他们应该进行审查。

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