generateTable.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. function generateTable(listName,divName,unique){
  2. //add temp variables
  3. LABKEY.Query.selectRows({schemaName: 'lists',queryName: listName, requiredVersion:"8.3",
  4. parameters:{"divName":divName}, success:function(data){populateTable(data,divName,unique)} });
  5. }
  6. function populateTable(data,divName,unique){
  7. //generate and populate table with the first suitable entry
  8. document.getElementById('formStatus').value+="\n Query: "+data.queryName;
  9. document.getElementById('formStatus').value+="\n divName: "+divName;
  10. //document.getElementById('formStatus').value+="\n Div: "+resp.params["query.param.divName"];
  11. var obj=data.metaData.fields;
  12. for (f in obj){
  13. document.getElementById('formStatus').value+="\n Data["+f+"]: "+obj[f];
  14. for (g in obj[f]){
  15. document.getElementById('formStatus').value+="\n Data.metaData.fields["+f+"]["+g+"]: "+obj[f][g];
  16. }
  17. if ("lookup" in obj[f]){
  18. for (h in obj[f]["lookup"]){
  19. document.getElementById('formStatus').value+="\n Lookup["+h+"]: "+obj[f]["lookup"][h];
  20. }
  21. }
  22. }
  23. var crfRef=document.getElementById("crfRefId").innerHTML;
  24. var entry=new Object();
  25. for (i=0;i<data.rows.length;i++){
  26. var entry=data.rows[i];
  27. if (entry.crfRef!=crfRef) {
  28. document.getElementById('formStatus').value+="\n Crfref mismatch: "+crfRef+"/"+entry[crfRef];
  29. continue;
  30. }
  31. break;
  32. }
  33. var tb=document.createElement('table');
  34. tb.className="t2";
  35. document.getElementById(divName).appendChild(tb);
  36. var fields=data.metaData.fields;
  37. for (f in fields){
  38. if (fields[f].hidden) continue;
  39. var vName=fields[f].name;
  40. if (vName=="crfRef") continue;
  41. var vType=fields[f].type;
  42. let row=tb.insertRow();
  43. let cell=row.insertCell();
  44. let text = document.createTextNode(fields[f].shortCaption);
  45. cell.appendChild(text);
  46. cell=row.insertCell();
  47. var cellClass="input";
  48. var cellType="text";
  49. if ("lookup" in fields[f]){
  50. cellClass="select";
  51. }
  52. if (vType=="string"){
  53. ;
  54. }
  55. if (vType=="date"){
  56. cellType="date";
  57. }
  58. var input = document.createElement(cellClass);
  59. input.type = cellType;
  60. input.id = vName;
  61. if (vType=="string" ){
  62. if (vName in entry) { input.value=entry[vName];}
  63. }
  64. if (vType=="date"){
  65. if (vName in entry) {input.valueAsDate=new Date(entry[vName]);}
  66. }
  67. if ("lookup" in fields[f]){
  68. var lookup=fields[f]["lookup"];
  69. var config=new Object();
  70. document.getElementById('formStatus').value+="\n Query: "+lookup.queryName;
  71. config.schemaName=lookup.schemaName;
  72. config.queryName=lookup.queryName;
  73. var parameters={"elementId":input.id,"keyColumn":lookup.keyColumn,"displayColumn":lookup.displayColumn};
  74. if (vName in entry) parameters.selectedKey=entry[vName];
  75. config.success=function(data){populateSelect(data,parameters)};
  76. LABKEY.Query.selectRows(config);
  77. }
  78. cell.appendChild(input);
  79. }
  80. var row=tb.insertRow();
  81. let cell=row.insertCell();
  82. cell.setAttribute("colspan","2");
  83. var input=document.createElement("input");
  84. input.type="button";
  85. input.value="Add";
  86. input.onclick=function(){saveData(data.queryName,unique)};
  87. cell.appendChild(input);
  88. }
  89. function populateSelect(data,parameters){
  90. document.getElementById('formStatus').value+="\n Data: "+data;
  91. var elementId=parameters.elementId;
  92. var keyColumn=parameters.keyColumn;
  93. var displayColumn=parameters.displayColumn;
  94. document.getElementById('formStatus').value+="\n ElementId: "+elementId;
  95. document.getElementById('formStatus').value+="\n keyColumn: "+keyColumn;
  96. document.getElementById('formStatus').value+="\n displayColumn: "+displayColumn;
  97. if ("selectedKey" in parameters){
  98. document.getElementById('formStatus').value+="\n SelectedKey: "+parameters["selectedKey"];
  99. }
  100. var el = document.getElementById(elementId);
  101. document.getElementById('formStatus').value+="\n Element: "+el;
  102. for(i = el.options.length; i >= 0; i--) {
  103. el.remove(i);
  104. }
  105. var opt = document.createElement("option");
  106. opt.text = "<Select>";
  107. opt.value = -1;
  108. el.options[0] = opt;
  109. for (var i = 0; i < data.rows.length; i++) {
  110. var opt = document.createElement("option");
  111. opt.text = data.rows[i][displayColumn];
  112. opt.value = data.rows[i][keyColumn];
  113. document.getElementById('formStatus').value+="\n Adding: "+opt.value+" : "+opt.text;
  114. el.options[el.options.length] = opt;
  115. if ("selectedKey" in parameters){
  116. document.getElementById('formStatus').value+="\n Comparing: " + opt.value + "/" +
  117. parameters["selectedKey"];
  118. if (opt.value==parameters["selectedKey"]){
  119. el.selectedIndex=el.options.length-1;
  120. document.getElementById('formStatus').value+="\n Equal; "+el.selectedIndex;
  121. }
  122. }
  123. }
  124. }
  125. function failureSelect(errorInfo,resp,opt){
  126. document.getElementById('formStatus').value+="\n Error: ";
  127. }
  128. function saveData(queryName,unique){
  129. document.getElementById('formStatus').value+="\n saveData: ";
  130. var config=new Object();
  131. config.schemaName='lists';//could be made more generic
  132. config.queryName=queryName;
  133. var crfRef=document.getElementById("crfRefId").innerHTML;
  134. config.filterArray=[ LABKEY.Filter.create('crfRef',crfRef)];
  135. config.success=function(data){saveDataToList(data,unique)};
  136. LABKEY.Query.selectRows(config);
  137. }
  138. function saveDataToList(data,unique){
  139. document.getElementById('formStatus').value+="\n saveDataToList: ";
  140. var update=false;
  141. if (unique && data.rows.length>0){
  142. var entry=data.rows[0]
  143. update=true;
  144. }
  145. else{
  146. var entry=new Object();
  147. entry.crfRef=document.getElementById("crfRefId").innerHTML;
  148. }
  149. var config=new Object();
  150. config.schemaName=data.schemaName;//could be made more generic
  151. config.queryName=data.queryName;
  152. //entry.crfRef=document.getElementById("crfRefId").innerHTML;
  153. var fields=data.metaData.fields;
  154. for (f in fields){
  155. if (fields[f].hidden) continue;
  156. var vName=fields[f].name;
  157. if (vName=="crfRef") continue;
  158. var el=document.getElementById(vName);
  159. if (!el) continue;
  160. var vType=fields[f].type;
  161. if ("lookup" in fields[f]){
  162. entry[vName]=el.options[el.selectedIndex].value;
  163. continue;
  164. }
  165. if (vType=="date"){
  166. var date=el.valueAsDate;
  167. if (date==="null") continue;
  168. date.setUTCHours(12);
  169. entry[vName]=date.toString();
  170. }
  171. if (vType=="string"){
  172. entry[vName]=el.value;
  173. }
  174. }
  175. config.rows=[entry];
  176. if (update)
  177. LABKEY.Query.updateRows(config);
  178. else
  179. LABKEY.Query.insertRows(config);
  180. }
  181. //exports.generateTable=generateTable;
  182. //exports.saveData=saveData;