سؤال

So I'm trying to simply open a popup and populate handsontable with an array of arrays. The data passed from the parent to the child looks like:

[
  ['one', 'two', 'three'],
  ['four', 'fix', 'six'],
  ['seven', 'eight', 'nine']
]

If I call the table setup method from the parent window and pass in the data above, handsontable gives me an error.

var w = window.open('...');
// wait for load
w.editor.loadTable([['one','two'],['three','four']]);

The above fails with the following error:

Uncaught Error: Cannot create new column. When data source in an object, 
you can only have as much columns as defined in first data row, data 
schema or in the 'columns' setting.If you want to be able to add new 
columns, you have to use array datasource.

However, if I don't pass in an array, like...

w.editor.loadTable();

...the child popup generates the array and all works as expected. My best guess is there is some misunderstanding I have with sending data from a parent to a child window, or it's finally happened...I've lost my mind.

I've put together a simple test to show the problem that can be found here: http://scottdover.com/ht_test/

Any help would be greatly appreciated!

هل كانت مفيدة؟

المحلول 2

Decided to take another approach to remedy the issue. Since the data was uploaded via a CSV, I simply pulled in the info within the popup window via ajax.

نصائح أخرى

The code that returns the error is:

if (this.instance.dataType === 'object' || this.instance.getSettings().columns) {
  throw new Error("Cannot create new column. When data source in an object, " +
    "you can only have as much columns as defined in first data row, data schema or in the 'columns' setting." +
    "If you want to be able to add new columns, you have to use array datasource.");

It does two checks, and if you debug, you'll see it's actually the first check that generates the error.

Although you set data with an array of arrays, when you pass it from your parent window, it becomes an object with objects (weird, but that's what the debugger says). You can check it with your debugger (try data instanceof Array)

I suggest you use JSON.stringify to pass your data to the popup, and then use JSON.parse to convert it back to an array:

openWindowWith(JSON.stringify([
                ['one','two','three'],
                ['four','five','six'],
                ['seven','eight','nine']
            ]));

...

if (!data) {
              data = [
                ['one', 'two', 'three'],
                ['four', 'fix', 'six'],
                ['seven', 'eight', 'nine']
              ];
          } else data = JSON.parse(data);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top