2014-03-28 27 views
0

所以我想簡單地打開一個彈出並填充handsontable與數組的數組。從父到子傳遞的數據是這樣的:HandsonTable,將數據傳遞到子窗口,並window.open

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

如果我把從父窗口的桌子設置方法,並通過上面的數據,handsontable給我一個錯誤。

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

上述失敗,出現以下錯誤:

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. 

但是,如果我沒有在數組中,喜歡傳球...

w.editor.loadTable(); 

...孩子彈出窗口生成數組,並按預期工作。我最好的猜測是,我將數據從父母發送到子窗口時存在一些誤解,或者終於發生了......我失去了理智。

我已經把一個簡單的測試表明,可以在這裏發現了問題:http://scottdover.com/ht_test/

任何幫助將不勝感激!

回答

0

決定採取另一種方法來解決問題。由於數據是通過CSV上傳的,我只需通過ajax將彈出窗口中的信息拖入。

0

返回錯誤代碼是:

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."); 

它做兩次檢查,如果你調試,你會看到它實際上是產生錯誤的第一次檢查。

雖然你有一個數組的數組,當您從父窗口傳遞數據集,它成爲與對象的對象(奇怪,但是這是調試器說的話)。您可以使用調試器檢查(試行data instanceof Array

我建議你使用JSON.stringify將數據傳遞給彈出,然後用JSON.parse將其轉換回一個數組:

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); 
+0

感謝您的幫助。如果我是正確的,'typeof []'在javascript中總是'object'。我早先發現了這一行代碼,它似乎仍不能解決這個問題。醜陋的解決方法是簡單地將數據保存到磁盤並在彈出窗口中讀取文件。不是最好的解決方案,而是完成工作。 – sdover102

+0

感謝您的幫助,但! – sdover102

+0

我發佈之前試過我的解決方案,它與IE11一起工作(無法使用其他瀏覽器進行檢查)。確實'typeof []'返回'object',這就是爲什麼你使用'instanceof'。無論如何,如果你正在尋找答案的話,你能否將你的問題標記爲答案? –

相關問題