2012-07-26 72 views
0

我實現一個功能結合,這將使我的動態添加列到JavaScript表:創建動態在JavaScript

for(var i = 0; i < info.length; i++){ 

     var temp = []; 

     temp.push(parseInt(info[i].site_id)); 
     temp.push(info[i].site); 
     temp.push(info[i].site_code); 
     temp.push(processArray(info[i].tc10)); 
     temp.push(processArray(info[i].tc9x_test)); 
     temp.push(processArray(info[i].tc9x_build)); 
     temp.push(processArray(info[i].oracle_dev)); 
     temp.push(processArray(info[i].database)); 
     temp.push(processArray(info[i].baseline)); 
     temp.push(processArray(info[i].push_windows)); 
     temp.push(processArray(info[i].push_unix)); 
     temp.push(processArray(info[i].license)); 
     temp.push(processArray(info[i].tcx)); 
     temp.push(processArray(info[i].eng)); 
     temp.push(processArray(info[i].perforce_proxy)); 
     temp.push(processArray(info[i].volume_server)); 
     temp.push(info[i].windows_ref_unit_location); 
     temp.push(info[i].unix_ref_unit_location); 
     temp.push(info[i].windows_rte_location); 
     temp.push(info[i].unix_rte_location); 
     temp.push(info[i].windows_toolbox_location); 
     temp.push(info[i].unix_toolbox_location); 
     temp.push(info[i].UGII_LICENSE_FILE); 
     temp.push(info[i].UGS_LICENSE_SERVER); 
     temp.push(info[i].unix_dev_units); 
     temp.push(info[i].unix_devop_path); 
     temp.push(info[i].perforce_proxy_path); 
     temp.push(info[i].primary_contact); 
     temp.push(info[i].secondary_contact); 
     temp.push(info[i].num_users); 
     temp.push(info[i].TC_12); 

      // check if new columns got added: 
     if(len > 29){ 
      for(var j = 30; j < len; j++){ 
       var col = columns[j]; 
       temp.push(info[i].col); 
      } 
     } 
      rows.push(temp); 
    } 
    return rows; 
} 

var rows = [[]]持有該表的數據... info[[]]包含查詢的對象JSON數據庫。 在這段代碼的問題:

for(var j = 30; j < len; j++){ 
    var col = columns[j]; 
    temp.push(info[i].col); 
} 

我試圖動態綁定col的一些info屬性。但我不知道是否可能......我怎麼能這樣做? 假設用戶添加了一個新列,TC_12。因此,我不知道TC_12是否存在,所以我想動態地將col標爲info[i],這樣可以以某種方式讓我產生info[i].TC_12。 任何想法?

+1

您可以通過清除該大系列'push'es了只是把它們放在文字中:https://gist.github.com/3185075 – Ryan 2012-07-26 22:53:30

回答

1

使用方括號表示法將變量的值或某個其他表達式的結果用作對象屬性。

temp.push(info[i][col]); 

僅供參考,你可以通過多個參數做以.push()單一調用所有這些推...

temp.push(parseInt(info[i].site_id), 
       info[i].site, 
       info[i].site_code, 
       processArray(info[i].tc10), 
       processArray(info[i].tc9x_test), 
       // etc... 
      ); 
+0

聽起來不錯,但它沒有給出'2-D'矩陣......所以Google Visualization API在抱怨... – cybertextron 2012-07-26 22:53:53

+0

@ philippe:不知道爲什麼會這樣。 – 2012-07-26 22:55:23