Documentation by version:
A widget for rendering feature-rich forms defined in JSON definition files
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.
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:
(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).
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.
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.
<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>
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');
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).
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").
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);
}]);
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:
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.
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:
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:
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:
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:
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.
See the LICENSE.md file in the lforms package on GitHub.