Building surveys - concatenate field values

LabKey Support Forum (Inactive)
Building surveys - concatenate field values Jon (LabKey DevOps)  2016-02-19 17:37
Status: Closed
 
Hi Simon,

It took a bit, but I think I created a pretty solid way to give you what you're looking for.

So the code does a few things:

1. It removes the actual Survey Label and hides it. If you use "useDefaultLabel", it hides the survey label and uses a date and timestamp on the table where it stores the survey information.
2. This survey has two sections:

- One for the User ID that combines all three values from patient, visit, and sample fields into one concatenated value
- One for the three values of Patient ID, Value ID, and Sample IDs.

3. The User ID field has a listener attached that listens for all three fields in the lower section and then populates the value in the User ID box separated with underscores.

For example, a Patient ID of "123ABC", a Visit ID of "02012016-1330", and a Sample ID of "Samp9999" would become "123ABC_02012016-1330_Samp9999" in the User ID field.

4. The User ID field has been purposely configured to ignore anything put in that box and will also disable itself so you can't change it without changing the three other fields.

It took a bit to figure out how to write out the function within the listener, since I had to find a way to have the listener specifically target only those three fields and then concatenating the values and then setting the value of the User ID field accordingly.

Take a look at the code below and give it a try. I think this should do the trick.

Regards,

Jon



{
  "survey" : {
    "layout" : "auto",
    "start" : {
       "useDefaultLabel": true
    },
    "showCounts" : false,
    "sections" : [{
      "questions" : [{
        "name" : "user",
        "caption" : "User Id",
        "shortCaption" : "User Id",
        "hidden" : false,
        "jsonType" : "string",
        "inputType" : "text",
        "required" : false,
        "width" : 800,
         "setDisabled": true,
        "listeners" : {
            "change" : {
          "question" : ["patient","visit","sample"],
          "fn" : "function(me, cmp, newValue, oldValue){var pid = cmp.getName(); vid = cmp.getName(); sid = cmp.getName(); if (pid == 'patient') {pidVal = newValue;} else if (vid == 'visit') {vidVal = newValue;} else if (sid == 'sample') {sidVal = newValue;} else {return true;} me.setValue(pidVal + '_' + vidVal + '_' + sidVal); me.setDisabled(true);} "
        }
      }
      }],
      "description" : null,
      "header" : true,
      "title" : "User Information",
      "collapsible" : false,
      "defaultLabelWidth" : 350
    },{
      "questions" : [{
        "name" : "patient",
        "caption" : "Patient Id",
        "shortCaption" : "Patient Id",
        "hidden" : false,
        "jsonType" : "string",
        "inputType" : "text",
        "required" : true,
        "width" : 800
      },{
        "name" : "visit",
        "caption" : "Visit Id",
        "shortCaption" : "Visit Id",
        "hidden" : false,
        "jsonType" : "string",
        "inputType" : "text",
        "required" : true,
        "width" : 800
      }, {
        "name" : "sample",
        "caption" : "Sample Id",
        "shortCaption" : "Sample Id",
        "hidden" : false,
        "jsonType" : "string",
        "inputType" : "text",
        "required" : true,
        "width" : 800
      }],
      "description" : null,
      "header" : true,
      "title" : "Medical Form",
      "collapsible" : true,
      "defaultLabelWidth" : 350
    } ]
  }
}