Correct: how to get an instance by a container?

I need to create a function that does some data updates and re-rendering on a rendered instance of handsontalbe ( var ht = new Handsontable(el,options)

). I can easily get in my case el

(an element that is used as an instance container). Can you get ht

just knowing el

? Or do I need to "remember" ht

somewhere to access it later?

(i tried Handsontable(el)

and creates a new table, it doesn't return an already created instance)

+3


source to share


2 answers


You cannot get a portable object from the DOM element it was created from. A competitive instance is simply a wrapper that controls a DOM element for viewing, and that element has no reference to its wrapper.

This means that you really need to store your reference to ht

somewhere, just like in another variable.

If your problem is with scope, make the table a property of the window object and it will be accessible from anywhere in your page. This can be done simply by using:



window.ht = new Handsontable(el,options)

      

However, if possible, avoid making such globals and keep them in proper scope.

+1


source


If you are using jQuery you can do this:

  // Instead of creating a new Handsontable instance
  // with the container element passed as an argument,
  // you can simply call .handsontable method on a jQuery DOM object.
  var $container = $("#example1");

  $container.handsontable({
    data: getData(),
    rowHeaders: true,
    colHeaders: true,
    contextMenu: true
  });

  // This way, you can access Handsontable api methods by passing their names as an argument, e.g.:
  var hotInstance = $("#example1").handsontable('getInstance');

      



Here is a link to the documentation: https://docs.handsontable.com/pro/1.13.0/demo-jquery.html

+1


source







All Articles