123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- function generateTable(listName,divName,unique){
- //add temp variables
- LABKEY.Query.selectRows({schemaName: 'lists',queryName: listName, requiredVersion:"8.3",
- parameters:{"divName":divName}, success:function(data){populateTable(data,divName,unique)} });
- }
- function populateTable(data,divName,unique){
- //generate and populate table with the first suitable entry
- document.getElementById('formStatus').value+="\n Query: "+data.queryName;
- document.getElementById('formStatus').value+="\n divName: "+divName;
- //document.getElementById('formStatus').value+="\n Div: "+resp.params["query.param.divName"];
- var obj=data.metaData.fields;
- for (f in obj){
- document.getElementById('formStatus').value+="\n Data["+f+"]: "+obj[f];
- for (g in obj[f]){
- document.getElementById('formStatus').value+="\n Data.metaData.fields["+f+"]["+g+"]: "+obj[f][g];
- }
- if ("lookup" in obj[f]){
- for (h in obj[f]["lookup"]){
- document.getElementById('formStatus').value+="\n Lookup["+h+"]: "+obj[f]["lookup"][h];
-
- }
- }
- }
- var crfRef=document.getElementById("crfRefId").innerHTML;
- var entry=new Object();
- for (i=0;i<data.rows.length;i++){
- var entry=data.rows[i];
- if (entry.crfRef!=crfRef) {
- document.getElementById('formStatus').value+="\n Crfref mismatch: "+crfRef+"/"+entry[crfRef];
- continue;
- }
- break;
- }
-
- var tb=document.createElement('table');
- tb.className="t2";
- document.getElementById(divName).appendChild(tb);
- var fields=data.metaData.fields;
- for (f in fields){
- if (fields[f].hidden) continue;
-
- var vName=fields[f].name;
- if (vName=="crfRef") continue;
- var vType=fields[f].type;
- let row=tb.insertRow();
- let cell=row.insertCell();
- let text = document.createTextNode(fields[f].shortCaption);
- cell.appendChild(text);
- cell=row.insertCell();
- var cellClass="input";
- var cellType="text";
- if ("lookup" in fields[f]){
- cellClass="select";
- }
- if (vType=="string"){
- ;
- }
- if (vType=="date"){
- cellType="date";
- }
-
- var input = document.createElement(cellClass);
- input.type = cellType;
- input.id = vName;
- if (vType=="string" ){
- if (vName in entry) { input.value=entry[vName];}
- }
- if (vType=="date"){
- if (vName in entry) {input.valueAsDate=new Date(entry[vName]);}
- }
- if ("lookup" in fields[f]){
- var lookup=fields[f]["lookup"];
- var config=new Object();
- document.getElementById('formStatus').value+="\n Query: "+lookup.queryName;
- config.schemaName=lookup.schemaName;
- config.queryName=lookup.queryName;
- var parameters={"elementId":input.id,"keyColumn":lookup.keyColumn,"displayColumn":lookup.displayColumn};
- if (vName in entry) parameters.selectedKey=entry[vName];
- config.success=function(data){populateSelect(data,parameters)};
- LABKEY.Query.selectRows(config);
- }
- cell.appendChild(input);
- }
- var row=tb.insertRow();
- let cell=row.insertCell();
- cell.setAttribute("colspan","2");
- var input=document.createElement("input");
- input.type="button";
- input.value="Add";
- input.onclick=function(){saveData(data.queryName,unique)};
- cell.appendChild(input);
-
- }
- function populateSelect(data,parameters){
- document.getElementById('formStatus').value+="\n Data: "+data;
- var elementId=parameters.elementId;
- var keyColumn=parameters.keyColumn;
- var displayColumn=parameters.displayColumn;
- document.getElementById('formStatus').value+="\n ElementId: "+elementId;
- document.getElementById('formStatus').value+="\n keyColumn: "+keyColumn;
- document.getElementById('formStatus').value+="\n displayColumn: "+displayColumn;
-
- if ("selectedKey" in parameters){
- document.getElementById('formStatus').value+="\n SelectedKey: "+parameters["selectedKey"];
- }
- var el = document.getElementById(elementId);
- document.getElementById('formStatus').value+="\n Element: "+el;
- for(i = el.options.length; i >= 0; i--) {
- el.remove(i);
- }
- var opt = document.createElement("option");
- opt.text = "<Select>";
- opt.value = -1;
- el.options[0] = opt;
- for (var i = 0; i < data.rows.length; i++) {
- var opt = document.createElement("option");
- opt.text = data.rows[i][displayColumn];
- opt.value = data.rows[i][keyColumn];
- document.getElementById('formStatus').value+="\n Adding: "+opt.value+" : "+opt.text;
-
- el.options[el.options.length] = opt;
- if ("selectedKey" in parameters){
- document.getElementById('formStatus').value+="\n Comparing: " + opt.value + "/" +
- parameters["selectedKey"];
-
- if (opt.value==parameters["selectedKey"]){
- el.selectedIndex=el.options.length-1;
- document.getElementById('formStatus').value+="\n Equal; "+el.selectedIndex;
- }
- }
- }
- }
- function failureSelect(errorInfo,resp,opt){
- document.getElementById('formStatus').value+="\n Error: ";
- }
- function saveData(queryName,unique){
- document.getElementById('formStatus').value+="\n saveData: ";
- var config=new Object();
- config.schemaName='lists';//could be made more generic
- config.queryName=queryName;
- var crfRef=document.getElementById("crfRefId").innerHTML;
- config.filterArray=[ LABKEY.Filter.create('crfRef',crfRef)];
- config.success=function(data){saveDataToList(data,unique)};
- LABKEY.Query.selectRows(config);
- }
- function saveDataToList(data,unique){
- document.getElementById('formStatus').value+="\n saveDataToList: ";
- var update=false;
- if (unique && data.rows.length>0){
- var entry=data.rows[0]
- update=true;
- }
- else{
- var entry=new Object();
- entry.crfRef=document.getElementById("crfRefId").innerHTML;
- }
-
- var config=new Object();
- config.schemaName=data.schemaName;//could be made more generic
- config.queryName=data.queryName;
- //entry.crfRef=document.getElementById("crfRefId").innerHTML;
-
- var fields=data.metaData.fields;
- for (f in fields){
- if (fields[f].hidden) continue;
-
- var vName=fields[f].name;
- if (vName=="crfRef") continue;
- var el=document.getElementById(vName);
- if (!el) continue;
-
- var vType=fields[f].type;
- if ("lookup" in fields[f]){
- entry[vName]=el.options[el.selectedIndex].value;
- continue;
- }
- if (vType=="date"){
- var date=el.valueAsDate;
- if (date==="null") continue;
- date.setUTCHours(12);
- entry[vName]=date.toString();
- }
- if (vType=="string"){
- entry[vName]=el.value;
- }
- }
- config.rows=[entry];
- if (update)
- LABKEY.Query.updateRows(config);
- else
- LABKEY.Query.insertRows(config);
- }
- //exports.generateTable=generateTable;
- //exports.saveData=saveData;
|