Search code examples
javascriptangularjshandsontable

instance.rootElement.getAttribute is not a function


I am trying to use handsontable in application and doing some experiment with the code. I am now facing the error-"instance.rootElement.getAttribute is not a function". When I check it occurs at line of code

hot = new Handsontable(container, {

My index.html

<!DOCTYPE html>
<html ng-app="myApp">
<head>

<script type="text/javascript" src="https://docs.handsontable.com/0.16.1/scripts/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.js"></script>
<script type="text/javascript"src="https://docs.handsontable.com/0.16.1/bower_components/handsontable/dist/handsontable.full.js"></script>
<script type="text/javascript" src="js/dragresize.js"></script>
<link type="text/css" rel="stylesheet" href="https://docs.handsontable.com/0.16.1/bower_components/handsontable/dist/handsontable.full.min.css">
</head>

<body >

<div id="example1" class="hot handsontable htRowHeaders htColumnHeaders" data-drag-resize></div>
</body>

</html>

My dragresize.js

angular.module('myApp',[]).directive('dragResize',function(){
    return{
        restrict: 'AEC',
        replace:true,
        controller: function($scope, $timeout){
            //document.addEventListener("DOMContentLoaded", function() {

  var container = angular.element(document.getElementById('example1')),
    hot;

  hot = new Handsontable(container, {
    data: Handsontable.helper.createSpreadsheetData(200, 10),
    rowHeaders: true,
    colHeaders: true,
    colWidths: [55, 80, 80, 80, 80, 80, 80],
    rowHeights: [50, 40, 100],
    manualColumnResize: true,
    manualRowResize: true
  });

  function bindDumpButton() {
      if (typeof Handsontable === "undefined") {
        return;
      }

      Handsontable.Dom.addEvent(document.body, 'click', function (e) {

        var element = e.target || e.srcElement;

        if (element.nodeName == "BUTTON" && element.name == 'dump') {
          var name = element.getAttribute('data-dump');
          var instance = element.getAttribute('data-instance');
          var hot = window[instance];
          console.log('data of ' + name, hot.getData());
        }
      });
    }
  bindDumpButton();
    $timeout(dragResize,0);
//});
        }

    };
});

/* document.addEventListener("DOMContentLoaded", function() {

  var
    container = document.getElementById('example1'),
    hot;

  hot = new Handsontable(container, {
    data: Handsontable.helper.createSpreadsheetData(200, 10),
    rowHeaders: true,
    colHeaders: true,
    colWidths: [55, 80, 80, 80, 80, 80, 80],
    rowHeights: [50, 40, 100],
    manualColumnResize: true,
    manualRowResize: true
  });

  function bindDumpButton() {
      if (typeof Handsontable === "undefined") {
        return;
      }

      Handsontable.Dom.addEvent(document.body, 'click', function (e) {

        var element = e.target || e.srcElement;

        if (element.nodeName == "BUTTON" && element.name == 'dump') {
          var name = element.getAttribute('data-dump');
          var instance = element.getAttribute('data-instance');
          var hot = window[instance];
          console.log('data of ' + name, hot.getData());
        }
      });
    }
  bindDumpButton();
$timeout(container,0);
}); */

I am not sure what does this error exactly means. I am trying to integrate the existing javascript file in my angularjs file. Any information/help would be much appreciated

thanks.


Solution

  • Replace

    container = angular.element(document.getElementById('example1'))
    

    to

    container = $document[0].getElementById('example1')
    

    And add $document to your function param just like

    directive('dragResize', function($document) { ... }
    

    Hope this helps.

    Ask me for more queries.

    Thanks.