Documentation by version:

LForms

A widget for rendering feature-rich forms defined in JSON definition files


What is LForms?

LForms (a.k.a. LHC-Forms) is a feature-rich, open-source widget that creates input forms based on definition files for Web-based applications. It was developed by the Lister Hill National Center for Biomedical Communications (LHNCBC), National Library of Medicine (NLM), part of the National Institutes of Health (NIH), with the collaboration and support from the Regenstrief Institute, Inc. and the LOINC Committee.

Features

See the demos for a step-by-step introduction. Features include:

Installation

LForms can be used either as a prebuilt package or installed using the using the bower package manager (i.e. bower install lforms). If you aren't using bower, the simplest way to start using LForms is by linking to the prebuilt version hosted on clinicaltables.nlm.nih.gov. The URLs for the JavaScript and the CSS are:

JavaScript
https://clinicaltables.nlm.nih.gov/lforms-versions/11.2.0/lforms.min.js
CSS
https://clinicaltables.nlm.nih.gov/lforms-versions/11.2.0/styles/lforms.min.css

(Note that the URLs contain a release version number, 11.2.0, for which you can substitute any of the other prebuilt versions available at https://clinicaltables.nlm.nih.gov/lforms-versions).

Usage

Although LForms internally uses AngularJS and offers a directive for use by AngularJS projects, you do not need to know anything about AngularJS to use it.

Rendering a Form (Without AngularJS)

In your HTML, create a an element to hold the form. You will pass a reference to this element or its ID to a utility function load the form.

HTML (without AngularJS)

<div id="myFormContainer"></div>

Also include the LForms JavaScript and CSS files on the page. In your page header:


<link
 href="https://clinicaltables.nlm.nih.gov/lforms-versions/11.2.0/styles/lforms.min.css"
 media="screen" rel="stylesheet" />
And with your other JavaScript:
<script
 src="https://clinicaltables.nlm.nih.gov/lforms-versions/11.2.0/lforms.min.js"
 ></script>

JavaScript (without AngularJS)

Your form definition should be loaded into a variable on the window object, e.g.:
window.myFormDefinition = {...}
Pass that variable name and the form container element to a utility function which will render the form:
LForms.Util.addFormToPage('myFormDefinition', 'myFormContainer');

Rendering a Form (AngularJS Method)

As in the non-Angular method, you will need to include the LForms CSS and JavaScript (possibly using bower and wiredep, if you are using those).

HTML

The HTML in your page will look something like:

<body ng-app="myApp">
  <div ng-controller="myController">
    <lforms lf-data="myFormDefinition"></lforms>
  </div>
</body>

The directive is contained by a controller (in this example named "myController") which will have the responsibility of providing the form definition data as a JSON object (in this example named "myFormDefinition").

JavaScript

In the JavaScript for the AngularJS app, include 'lformsWidget' as a module to be loaded. Then, in the JavaScript for the AngularJS controller, construct an LFormsData object with the JSON form definition and assign that object to the scope variable "lfData". The form should initialize and display. For example:

angular.module('myApp', ['lformsWidget'])
  .controller('myController', ['$scope', function ($scope) {
    $scope.myFormData = new LForms.LFormsData(myFormDefinition);
  }]);

Retrieving User-Entered Data

After the user fills out a form, the data they have entered and things like codes for coded answer lists will be stored in the data model. To retrieve that data, LForms provides the following utility method:

LForms.Util.getUserData(formElement, noFormDefData, noEmptyValue, noHiddenItem)
Or, from wihin an AngularJS app, there is also an API on the LFormsData object:

$scope.myFormData.getUserData(noFormDefData, noEmptyValue, noHiddenItem)

With no arguments (i.e. LForms.Util.getUserData()), the data for the first LForm found on that page will be returned, and will include the form definition, along with entries for questions the user left blank and for questions that were hidden by skip-logic (which the user might not have seen). This default return behavior can be changed by the parameters, but in all cases the returned data will follow the structure of the form, in that answers will be nested inside containing sections.

The parameters are:

  1. formElement - an HTML element that includes the LForm's rendered form. If this is omitted, the "body" tag will be used. If there is more than one LForm within formElement, the first found will be used.
  2. noFormDefData (optional, default false) - If this is true, the form definition data will not be returned along with the data.
  3. noEmptyValue (optional, default false) - If this is true, items that have an empty value will be removed.
  4. noHiddenItem (optional, default false) - If this is true, items that are hidden by skip logic will be removed.

As an example, here is the data from a partially filled-in vital signs panel, returned via $scope.myFormData.getUserData(null, true, true, true):

    {
      "itemsData": [{
        "questionCode": "35094-2",
        "items": [{
          "questionCode": "8480-6",
          "value": "100",
          "unit": {
            "name": "mm[Hg]",
            "default": false,
            "normalRange": null,
            "absoluteRange": null
          }
        }, {
          "questionCode": "8357-6",
          "value": {
            "label": null,
            "code": "LA24014-5",
            "text": "Oscillometry",
            "other": null
          }
        }]
      }],
      "templateData": [{
        "value": "2015-11-09T05:00:00.000Z"
      }]
    }

The first section in the returned data "itemsData" contains all of the data from the form itself. LForms (optionally) adds a section to the top of the form that includes fields like "Date" and "Comment", and the data for these elements shows up in "templateData". The form definition data was not included in the above example, but you can see the structure if you look closely at itemsData. In this form, there was a section (question code "35094-2") which contained both of the two filled-in items as data. That is why there is just one entry in the itemsData itself, and that item has two sub-items in "items" array. One of the two items was numeric and had an associated unit field, while the other was a coded list field.

Another utility method, LForms.Util.getFormData(), will return both the user data and form definition data together in a way that can be fed back to LForms to display the form with the data. This is useful when you want to allow the user to save the form so it can be redisplayed later for completion or editing.

Retrieving User-Entered Data as HL7 v2 OBR and OBX segments (version >= 12.8)

The user entered data, along with some form definition data can be retrieved as HL7 v2 OBR and OBX segments. To retrieve that data, LForms provides the following utility method:

    LForms.Util.getFormHL7Data(element)

The parameter is:

  1. element - optional if there is only one LForms widget on the page, The containing HTML element that includes the LForms rendered form. It could be the DOM element or its id.

Retrieving User-Entered Data as FHIR Resources (version >= 12.8)

The user entered data, along with some form definition data, can be retrieved as FHIR resources. To retrieve that data, LForms provides the following utility method:

    LForms.Util.getFormFHIRData(resourceType, element, inBundle, bundleType)

The parameters are:

  1. resourceType - a FHIR resource type. Currently only "DiagnosticReport", "Questionnaire" (SDC profile) and "QuestionnaireResponse" (SDC profile) are supported.
  2. element - optional if there is only one LForms widget on the page. The containing HTML element that includes the LForms rendered form. It could be the DOM element or its id.
  3. inBundle - optional, a flag that a DiagnosticReport resources and associated Observation resources are included in a FHIR Bundle. The default is false.
  4. bundleType - optional, the FHIR Bundle type if inBundle is true.

Converting FHIR Questionnaire Resource to LForms Data (version >= 12.8)

The FHIR Questionnaire resource retrieved from LForms form can be converted back to LForms data, which can then be loaded into the LForms widget to show the form. To convert FHIR Questionnaire resources to LForms data, LForms provides the following utility method:

    LForms.Util.convertFHIRQuestionnaireToLForms(fhirData)

The parameter is:

  1. fhirData - a FHIR Questionnaire resource, which should be generated through the LForms.Util.getFormFHIRData() function

Merging FHIR Resources into LForms Data (version >= 12.8)

The FHIR resource retrieved from LForms form can be merged back into LForms data, which can then be loaded into the LForms widget to show the form along with the user data. To merge FHIR resources into LForms data, LForms provides the following utility method:

    LForms.Util.mergeFHIRDataIntoLForms(resourceType, fhirData, formData)

The parameters are:

  1. resourceType - a FHIR resource type. Currently only "DiagnosticReport" (a DiagnosticReport resource with "contained" Observation resources or a "searchset" typed Bundle of a DiagnosticReport and associated Observation resources), and "QuestionnaireResponse" (SDC profile) are supported.
  2. fhirData - a FHIR resource, which should be generated through the LForms.Util.getFormFHIRData() function
  3. formData - an LForms form data object.

Form Definition Format

Form definitions are stored in a JSON structure. To get a rough idea of what these are you can take a look at one of the samples, or for a detailed description see the form definition documentation.

Licensing and Copyright Notice

See the LICENSE.md file in the lforms package on GitHub.


Project maintained by lhncbc Hosted on GitHub Pages — Based on theme by mattgraham