Add Ability to Pass a Custom Search Handler to UI Commons Module "select-drug" Directive

The Reason:

I am working on a custom implementation of the order entry UI module. When ordering drug, I want to be able to provide a mechanism to record the order even when the drug is not yet available in the system (that is as a drug with associated concept). The drug order API luckily allows recording of these type of orders by using “non_coded_drug” field in the drug_order table.

I am using select-drug directive provided by the UI Commons module. What I want on the UI is for the user to see something like the image below when they type in a drug (in this case Aspirin) that is not already in the system.

This gives the user ability to choose the drug as non coded without having to deal with more clicking or typing.


As far as I know I can’t do this without modifying the directive. The most straight forward way of achieving this is modifying the search handler such that when the backend returns an empty result set, I create one element using the entered text with “NON CODED” appended and return this as results. But this is not always desirable behavior because not all directive users wants this behavior, hence my suggested solution which is below.

Proposed Solution

Modify the directive to allow the client to pass in a custom handler if they wish to. I propose the Handler to be called with two parameters, the search term and the DrugService, that is this piece of code is to be changed to be as below.

$ = function(term) {
    if(attrs.searchFunction) {
       return $scope.searchFunction(term, DrugService);
    return DrugService.getDrugs({ q: term });


First, the trivial problem with this feature is that there could be a match in the DB but the user just entered the wrong text when searching for it because they have a different spelling in their mind of what they are looking for or they could still be typing if you are using autocomplete and you never know when the user is done typing, you don’t want to end up storing half/miss spelt text values so you might have to have that extra step you’re trying to avoid for the user to explicitly confirm that they want to store the non coded text as it is.

With that said, I believe the coded-or-free-text-answer directive has this feature out of the box without tampering with the search handlers, it is used when adding a new patient allergy, you might want to look at it to see how it is done. Rather than adding the ‘fake’ item for the entered search phrase via the handler, the directive appends it directly to the list of the autocomplete’s items if there is no exact match, you can do the same when there is no match instead.

In this case though, rather than creating a new ‘coded-or-free-text-drug’ I suggest that you add a new attribute to the existing select-drug directive that tells it if it should support this feature, it should default to its current behavior if the attribute isn’t present.

Since this is new behavior supported by the OpenMRS API that wasn’t there when I originally wrote the orderentryui module, so I agree with Wyclif: you don’t need to do this as a custom extension in your module, but rather you should add it directly to select-drug.

I would suggest that you add an optional setting on the select-drug directive (defaulting to false) that allows for specifying a free-text drug name, and then you can enable this from the orderentryui module (assuming a high enough version of openmrs-core).

Thanks all I will go ahead and modify the select-drug directive.

Well, if the user made a mistake I think it is debatable whether you want to ask for confirmation or not but this is a workflow issue which can be handled on the UI side.

Regarding completion, I believe the directive fires a rest call every time there is a change on the input, so basically the trick is, when do you decide to show non coded? In other words when do you decide the user is done? I was thinking to keep showing non coded as long as the search has returned an empty result set because if the user keep typing the results will be updated anyway. What do you think?

Yes the directive shouldn’t be concerned about confirmation from the user, that is an implementation detail.

The issue is that the user sort of isn’t aware that the item for the search phrase is non coded text, they get deceived that a match was found for a coded drug, they could have searched again with another phrase that would match an existing drug in case they had misspelled it or made a typo. It would be preferable to sort of let them be aware of this, that way when they select it you assume they know exactly what they doing, in the allergy UI module this done by putting the item for the search phrase in quotes, you could do the same and add a hint to the field that the quoted item is non coded.

I chose to append the text (NON CODED) to the end. I guess adding quotes could make the choice even clearer.

I suggest that you make it work like the Diagnosis selection in the Reference Application: if there’s an exact match for what you typed, then free-text is not an option. But if there’s a non-exact match, you may choose non-coded. So if you type Paracet, then you have the options (1) Paracetamol, (2) “Paracet” (non-coded).

I strongly feel against showing non coded even in the events of partial matches. I am not a clinician but I think clattering up all partial searching with a non coded option is not optimal because for the most part we don’t expect users to type in the whole thing anyway.