Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Background

 Prior to OpenMRS version 1.8, the search widgets were written with dojo which involved writing a separate javascript file that extended the parent openmrsSearch for each Openmrs Object if it were to be searchable on w web page, the  dojo widgets would fetch all hits in one ajax query which would take sometime making the widgets slow. In 1.8, this was changed by introducing a single and more generic search widget written with jquery with the enhancements below: 

How to include a search widget for a domain object in a jsp

This can be relatively trivial if the domain object already has the required methods for the search widgets to work as expected. Let's assume you wish to add an encounter search to you jsp, below is what you need to do.

Code Block
<openmrs:htmlInclude file="/dwr/interface/DWREncounterService.js"/>
<openmrs:htmlInclude file="/scripts/jquery/dataTables/css/dataTables_jui.css"/>
<openmrs:htmlInclude file="/scripts/jquery/dataTables/js/jquery.dataTables.min.js"/>
<openmrs:htmlInclude file="/scripts/jquery-ui/js/openmrsSearch.js" />

<script type="text/javascript">
	var lastSearch;
	$j(document).ready(function() {
		new OpenmrsSearch("findEncounter", true, doEncounterSearch, doSelectionHandler,
				[	{fieldName:"personName", header:omsgs.patientName"Patient Name},
					{fieldName:"encounterType", header:omsgs.encounterType"Encounter Type},
					{fieldName:"formName", header:omsgs.encounterForm"Encounter Form},
					{fieldName:"providerName", header:omsgs.encounterProvider"Encounter Provider},
					{fieldName:"location", header:omsgs.encounterLocation"Encounter Location},
					{fieldName:"encounterDateString", header:omsgs.encounterDate"Encounter Date}
				],
                {
                    searchLabel: '<spring:message code="Encounter.search" javaScriptEscape="true"/>',
                    searchPlaceholder:'<spring:message code="Encounter.search.placeholder" javaScriptEscape="true"/>'
                });
	});

        //The action to take when the user selects an item from the hits in the widget
	function doSelectionHandler(index, data) {
		document.location = "encounter.form?encounterId=" + data.encounterId + "&phrase=" + lastSearch;
	}

	//Contains the logic that fetches the hits from the server
	function doEncounterSearch(text, resultHandler, getMatchCount, opts) {
		lastSearch = text;
		DWREncounterService.findCountAndEncounters(text, opts.includeVoided, opts.start, opts.length, getMatchCount, resultHandler);
	}
</script>

...

Code Block
$j(document).ready(function() {
  		$j("#elementId").openmrsSearch({
  			searchLabel:'<spring:message code="General.search"/>',
  			searchPlaceholder: '<spring:message code="Encounter.search.placeholder" javaScriptEscape="true"/>',
  			searchHandler: doSearchHandler,
  			selectionHandler: doSelectionHandler,
  			fieldsAndHeaders: [	{fieldName:"personName", header:omsgs.patientName"Patient Name},
					{fieldName:"encounterType", header:omsgs.encounterType"Encounter Type},
					{fieldName:"formName", header:omsgs.encounterForm"Encounter Form},
					{fieldName:"providerName", header:omsgs.encounterProvider"Encounter Provider},
					{fieldName:"location", header:omsgs.encounterLocation"Encounter Location},
					{fieldName:"encounterDateString", header:omsgs.encounterDate"Encounter Date}
				]
  		});
  	});

The arguments passed to the widgets are to used to customize the widget properties, in the example above, 'searchLabel' specifies the label that appears to the left of the search input box, 'searchPlaceHolder' specifies the text to to display inside the input box as a place holder, 'searchHandler' specifies the javascript function to be called to fetch the hits from the server, the properties of the objects in the returned list should match the field names specified for the fieldsAndHeaders property.