2015-12-30 5 views
0

Я преобразования XML в HTML таблицу, но во внутреннем для цикла I'am получения undefined ошибки для Row[i].cell.length Inspite того SPACER в этой ячейкенеопределенная ошибка для array.length AngularJS

Поиск демо here

Необходимый выходной стол - Table

Примечание: предупреждение вы будете получать это length из этой строки

+4

Для стартера при отладке JS вы должны использовать 'console.log' вместо предупреждения, как тревога очень ограничено, и останавливает выполнение JS, который может приводят к ложноположительным сценариям – maurycy

+0

@maurycy в порядке, но любая идея, почему его высказывание 'undefined ', когда есть объект там? – insomniac

ответ

1

ошибка раис ed, когда Строка узел имеет один элемент внутри.

Я изменил код для преобразования элемента строки в массив, но после исправления ошибки вышли другие, которые я пытался исправить, но я думаю, что они должны быть пересмотрены.

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