Building surveys - concatenate field values

LabKey Support Forum (Inactive)
Building surveys - concatenate field values simonm  2016-02-09 15:11
Status: Closed
 
Hi,

I'm having trouble building a survey with some simple functionality and I was hoping someone could point me in the right direction. Specifically:

1) Does anyone have a simple working example of showing/hiding fields based on the input value of another field? I know an example is included in the documentation but I can't make it work.

2) I would like to set the primary key field to a value concatenated from three other fields (patient,visit,sample). How do I do this?

In general, any reference to allowed syntax for a LabKey survey would be greatly appreciated, or pointers to example surveys (aside from those in the documentation).

Thanks,

Simon
 
 
Jon (LabKey DevOps) responded:  2016-02-10 16:27
Hi Simon,

Yes, you can show and hide fields based on input of another field. This is known as Skip Logic or Conditional logic.

A good code example can be found here:

https://www.labkey.org/home/Documentation/wiki-page.view?name=questionMetadata

Can you provide me with more information on your second question? Where is this Primary Key going to be on? Is it the Schema and Query you're tying to the Survey or is it for the Survey Answers?

Regards,

Jon
 
simonm responded:  2016-02-11 07:58
Hi Jon,

Thanks for your response, and for the link to the example of skip logic in the documentation. I'll take another look at it.


Regarding my question on concatenating values in a survey to fill the primary key field, here is some more information:

I need to be able to locate and identify records after they have been entered using a survey, but the Surveys webpart will only display a single field containing identifying information from a record - either the Label field or, if you customize it, the user-defined primary key.

The date I am entering is uniquely identified through a combination of three pieces of data - the Patient ID, the Visit, and the Sample.

I therefore need to concatenate these three pieces of data into a single field to display it - and that field must be either the Label or the user-defined Primary Key.

So for example if the Patient was number 1, the visit was day 3 and the sample taken was blood, the data would be: PatientID = 01, Visit = D3 and Sample = PB, and the user will enter these three values in a survey form. The desired value for the field displayed (either the Label or the user-defined PK) would therefore be 01_D3_PB.

My question: is it possible to write the Survey form in such a manner as to automatically populate a field with this concatenated string once the user has entered these three values?

Hope this helps,

Simon
 
Jon (LabKey DevOps) responded:  2016-02-12 17:55
Hi Simon,

I'm still looking into this for you. It should be possible to setup a few listeners either on the Label/Primary Key or a set of listeners beforeLoad using a function on the survey to then:

1. Obtain the values inputted into those fields
2. Store those values into defined variables
3. Call those variables back into the value field of the Label/Primary Key in the survey before it gets submitted.

Regards,

Jon
 
simonm responded:  2016-02-16 09:09
Hi Jon,

Yes, that sounds like what I need. Can you share any example code so I can see how it is done?

Thanks,

Simon
 
Jon (LabKey DevOps) responded:  2016-02-18 23:35
Hi Simon,

I don't have anything available that is working just yet. I was able to figure out how to read the fields and push them into another field. However, the problem I'm running into is trying to identify the values from all three ID fields separately and then concatenate them together.

Thank you for your patience.

Regards,

Jon
 
Jon (LabKey DevOps) responded:  2016-02-19 17:37
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
    } ]
  }
}
 
simonm responded:  2016-02-29 10:59
Hi Jon,

Sorry it has taken me a while to test this but I have done so now and it works just fine! This will be very useful.

Thank you very much for your help,

Simon