View on GitHub

Autocomplete-lhc

An autocompleter package used by the NLM PHR and LHC-Forms projects

Installation Demos Features
Documentation
Download pre-built JavaScript and CSS as a .zip file

Documentation for autocomplete-lhc

The easiest way to get started is to check the demos. Below each demo is a "show source" link that shows the code needed to construct an autocompleter with the options in the demo. What follows here is detailed documentation for the various constructor options, list events (e.g. item selection) and the AngularJS directive.

Constructing an Autocompleter

Options supported by both "search" (AJAX) and "prefetch" (non-AJAX) lists

The constructors for both types of lists (Def.Autocompleter.Prefetch and Def.Autocompleter.Search) take an options argument as their third parameter. The following options are supported by both:

Constructing a "prefetch" (non-AJAX) autocompleter

A "prefetch" list gets its name from the having all of its list items "fetched" in advance, i.e. loaded with the page and not via AJAX, although it is possible to completely replace the list with another after the page loads (via the setListAndField method). The syntax is as follows:

new Def.Autocompleter.Prefetch(field, [array, of, items], options);

Parameters:
  1. the ID or DOM Element of the form field for which the list is displayed. If an element is provided, it must contain an ID attribute, or one will be assigned.
  2. the array of completion options (list values). This must be at least an empty array (in which case the list can be specified later with the setListAndField function.
  3. (optional) an options hash, which can include:
    • any of the common options described above
    • addSeqNum - whether sequence numbers should be added to the items in the prefetched answer list (default true).
    • defaultValue - Either the code or the item text of the default value for this list's field. The default value is placed in the field the first time the user focuses the field.
    • codes - an array of code strings for the list values, for use in contexts where answer lists have associated codes which are relied on rather than the display strings for the users. This should be the same length as the list values array.
    • autoFill - If true, the field will be filled in with the list's value if setListAndField is called with a one-item list.
    • itemToHeading - a hash of item codes to codes of item headings, where both items and headings appear in the listItems array. This parameter requires that the codes parameter also be supplied.
    • formattedListItems - an optional HTML formatted list of descriptions. When provided, the descriptions will be appended to corresponding items for display. Filtering does not cover content in this formatted list.

Constructing a "search" (AJAX) autocompleter

A "search" list is an AJAX-based autocompleter, where AJAX requests return result lists which match the user's input in the field. The syntax is as follows:

new Def.Autocompleter.Search(field, url, options);

Parameters:
  1. the ID or DOM Element of the form field for which the list is displayed. If an element is provided, it must contain an ID attribute, or one will be assigned.
  2. url for getting the completion list. The website answering the URL is expected to understand the following parameters:
    • terms - the text from the field. This should be used to find matching list items.
    • maxList - (optional) If present, this signifies that this is a request for a large list of search results (e.g. by using the "see more" link on the list). If maxList is not present, that means this is an autocompletion request and the server should return a short list (e.g. 7 items) as quickly as possible. In version 10, this replaced the autocomp parameter, which had the opposite meaning and default.
    • authenticity_token - (optional) This is an anti-CSRF parameter. If the page has a value in window._token, it will get sent in this parameter.
    • suggest - (optional) User input that does not match a list value will trigger a request for suggestions that are close to what the user typed. A "suggest" parameter value of "1" means the request is for suggestions.
    • field_val - When "suggest"==1, this will contain the value the user typed in the field (instead of the "terms" parameter).
    The URL's response should be an array. For a non-suggestion request (suggest != '1'), it should have the following elements:
    • position 0 - the total search result count (including the ones not returned, if autocomp==1). As of version 18.1.3, this can be null if the total is not known.
    • position 1 - the list of codes for the list items (if the items are coded)
    • position 2 - A hash of extra data about the list items (e.g. an extra set of codes), or null if there is none. The keys in the hash should be names for the data elements, and the values should be an array of values, one for each returned item. Configuration controlling what gets returned here is out of scope for this class; the search autocompleter just sends the parameters above to the given URL. The extra data for the selected item (when the user makes a selection) can get be retrieved with getItemData(itemText).
    • position 3 - the list item data; each item is an array of display string fields which will be joined together. (At a mimimum, each item should be an array of one string.)
    • position 4 - (optional) This is an array of code system names identifying the code system for each of the codes in the code array in position 1. This is useful for lists that contain entries from different code systems. (Prior to version 15, this field was a flag indicating the presence of span tags in the display strings for highlighting matched regions. Those are still supported, but the flag is no longer needed.)
    For a suggest request, the response should have the following elements:
    • position 0 - the list of codes for the suggested items (if the items have codes)
    • position 1 - the list of display strings (an array of strings, not of arrays) for the suggested items.
    • position 2 - A hash of extra data about the list items (the same as position 2 for the non-suggestion request above.)
  3. (optional) an options hash, which can include:
    • any of the common options described above
    • search - Either this or the "url" paramter should be set, but not both, and this will take precedence if both are specified. When set, this should be a function that takes the current field value and a requested result count, and returns a promise that resolves to results in the same format as specified for the "url" parameter above, unless the "fhir" option is specified, in which case the Promise should resolve to a ValueSet expansion.
    • fhir - If present, this parameter will switch the autocompleter into HL7 FHIR mode, sending FHIR $expand requests and processing ValueSet expansion results.
    • minChars - (default 1, as of version 11) The minimum number of characters that must be in the field before autocompletion will start.
    • sort - Whether or not display values should be sorted after being retrieved from the server. (Default: true). Note that if you do not want sorting, you might also want set the suggestionMode parameter to Def.Autocompleter.NO_COMPLETION_SUGGESTIONS so that a suggestion is not moved to the top of the list.
    • matchListValue - Whether the field value is required to be one from the list (default: false).
    • tableFormat - If true, then if the list's items contain multiple fields, the list will be formatted in a table instead of just concatenating the fields together for each list item.
    • valueCols - Used when tableFormat is true to indicate which columns in the table should be combined to form the field value when the row is selected. This should be an array of column indices (starting with 0). If absent, all columns will be combined for the value. Note that the specification here must result in unique field values for each table row.
    • colHeaders - Used when tableFormat is true, this is an array of column headers for the columns in the table. If this is not supplied, no header row will be created.
    • useResultCache - (default: true) Whether or not the results should be cached. As of 10.0.3, the cache is based on the URL, so that autocompleting fields on the same page using the same URL will use the same cache. (In prior versions, the cache was shared only for fields with the same name, or more specifically, with the same return value for Def.Autocompleter.getFieldLookupKey.)
    • autocomp - a boolean that controls whether the field should also autocomplete as the user types. When this is false, the user won't see an autocompletion list until they hit return. (Default: true)
    • buttonID - (optional) the ID of a button to initiate a search based on the field's value. This is useful if you do not want the field to autocomplete (i.e. autocomp==false) but you still want to show a list of search results in response to a button.
    • nonMatchSuggestions - (optional; default: false, as of version 10) If this is set to true, and matchListValue is false, then if the user enters a value that did not match a list value, a second query will be sent (using the "suggest" parameter described above) to retrieve a list of suggestions to be shown to the user. As of version 17, the autocompleter no longer opens a dialog to show these suggestions. If suggestions are desired, it is now up the to program using the autocompleter, to 1) register an event callback (for receiving the suggestion list) via Def.Autocompleter.Event.observeSuggestions, 2) show the suggestions to the user, and 3) call the acceptSuggestion function on the instance of the autocompleter with the index of the selected suggestion.
    • showListOnFocusIfEmpty - (default: false) Whether to show a list when the empty field receives focus. (As of version 18.1.4)
    • showLoadingIndicator - (default: true) Whether to show a loading indicator during search. (As of version 19.2.0)

Notifications

The autocompleter allows you to register callbacks for certain events. The source file autoCompEvents.js provides the full list; here we'll just document the list selection event, which is the most likely to be useful.

List Selection Events

Sometimes you might need to get notified when the user has picked a value from a list, or has entered a non-list value. In that case you can register a callback like this:

Def.Autocompleter.Event.observeListSelections(fieldKey, callback);

The parameters are as follows:

Autocompleter APIs

The following sections document functions that can be called on instances of the autocompleter returned by the constructors above.

Getting the selection data

For simple use cases, getting what the user picked is as simple as looking in the field. For more complicated ones, APIs are provided for getting the code for the selected item, for getting selected items for multi-select lists (for which the field itself will be blank), and for getting the extra item data returned for a search list's AJAX call. These APIs are defined on the autocompleter instance, and if you don't have a reference to that, you can access it from the field's DOM element as element.autocomp

getSelectedItems()
Returns the display strings for the currently selected items or an empty array if there are none.
getSelectedCodes()
Returns the codes for the currently selected items or an empty array if there are none. If some of the selected items do not have a code, there will be null in that place in the returned array.
getSelectedItemData()
Returns all information about the currently selected list items, as an array of hashes, each with at least a "text" property for the item's display text. The hashes may also contain (if the data was provided) properties "code", "code_system", and "data" (which for search lists contains the "extra data" fields for that item). The return value will be null if there are no selected items.
getItemCode(itemText)
Returns the code for the given item text, or null if there isn't one. For search lists, itemText must be present in the list retrieved for the last character typed.
getItemData(itemText)
(For search lists only.)Returns a hash of all data about the given item, which must be among the items of the last list retrieved. The returned hash will have "code" and "text" properties for the selected item, and if there is any extra data for the item, that will be under a "data" sub-hash. If the items came with code system data, there will also be a "code_system" property with the code system corresponding to "code". Properties for which there are no values will not be present, except for the "text" property. Note that this data is also collected and returned by the getSelectedItemData() call above, which might be easier to use.

Prefetch list APIs

The following functions are availble on instance of the prefetch autocompleter.

setListAndField

If the prefetch list needs to be changed to a new list, you can do that with setListAndField([array of items], [array of codes]). If there is just one item in the new list, the field will be set to that value; otherwise the field value will be cleared.

setFieldToListValue

After constructing the prefetch list, the field value can be initialized with one of the list values by calling setFieldToListValue(fieldValue) where the "fieldValue" should be one of the list's display values. The autocompleter keeps track of what has been selected, so it is necessary to call this function rather than just assign to the .value attribute of the field.

preventListFromShowing

This is a property on the autocomplete instance. The using application can set this property to true to prevent the list from showing on focus or click.

AngularJS Directive

If you are using AngularJS, you can set up your autocompleters via directive. For example:

<input id="myfield" autocomplete-lhc="opts" ng-model="selectedVal">

The opts object contains the options normally passed into the constructor. (Note: As of version 8.0.0 of the autocompleter, this object is watched for changes, so if you return it via a function call, the function should return the same object unless the options really change.) As mentioned above, there are two types of autocompleters. You can either have a "prefetch" list where all the items are given to the autocompleter at construction time, or a "search" list where the list items are discovered as the user types, via AJAX calls. For both types, opts is a hash, but the keys on the hash differ.

For "prefetched lists", opts can contain:

For "search" lists, opts can contain: