2012-07-26 1 views
0

나는 기능을 구현하고있어 바인딩 동적 생성 :나 동적 <code>JavaScript</code> 테이블에 열을 추가 할 수 있도록 자바 스크립트

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); 
} 

내가 동적으로 info의 속성 중 일부와 col을 결합하기 위해 노력하고있어. 하지만 가능한지 아닌지 모르겠다. 어떻게 할 수 있니? 사용자가 새 열을 추가했다고 가정하십시오. TC_12. 따라서, TC_12이 존재하므로, colinfo[i]에 동적으로 입찰하고 싶습니다. 그래서 어떻게 든 나에게 info[i].TC_12을 양보 할 수 있습니다. 아이디어가 있으십니까?

+1

다음과 같은 방법으로 push'es'의 큰 시리즈를 정리할 수 있습니다 그냥 리터럴에 넣어 : https://gist.github.com/3185075 – Ryan

답변

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 차원 행렬을 제공하지 않습니다 ... 그래서 Google 시각화 API는 그것에 대해 불평하고 있습니다 ... – cybertextron

+0

@ 필립 : 왜 그렇게 될지 모르겠습니다. –

관련 문제