...
'SearchHandler' specifies the function that will get called by the script to fetch results from the server, the function gets called at least once once the search is triggered. The first call is expected to return a map with 2 key-value pairs i.e the expected total count of results and results to display on the first page. Typically, you should have logic on the server that get called by this function via ajax and it is important to make sure it returns the expected map. The keys in the returned map are 'count' and 'objectList' and their values should be the total count of expected results and the results to display on the first page respectively. Any subsequent calls to the search handler after the first call don't require that the count gets returned.
In this example, we call the DWREncounterService.findCountAndEncounters(String, boolean, Integer, Integer, boolean) method and below is how it would look like the code snippet below:
Code Block |
---|
public Map<String, Object> findCountAndEncounters(String phrase, boolean includeVoided, Integer start, Integer length,
boolean getMatchCount) throws APIException {
//Map to return
Map<String, Object> resultsMap = new HashMap<String, Object>();
Vector<Object> objectList = new Vector<Object>();
try {
EncounterService es = Context.getEncounterService();
int encounterCount = 0;
if (getMatchCount)
encounterCount += es.getCountOfEncounters(phrase, includeVoided);
//If we have any matches, fetch them or if this is not the first ajax call
//for displaying the results on the first page, the getMatchCount is expected to be zero
if (encounterCount > 0 || !getMatchCount)
objectList = findBatchOfEncounters(phrase, includeVoided, start, length);
resultsMap.put("count", encounterCount);
resultsMap.put("objectList", objectList);
}
catch (Exception e) {
objectList.clear();
objectList.add("Error while searching for encounters");
resultsMap.put("count", 0);
resultsMap.put("objectList", objectList);
}
return resultsMap;
}
|
The implementation of the server side logic should call the API methods that return the count and the appropriate search method that supports paging for the given domain object, in this case they would be EncounterService.getCountOfEncounters(String, boolean) and EncounterService.getEncounters(String, Integer, Integer, boolean). In the DAO layer, it's highly recommended that these methods use the same criteria object and the difference should be that the one that gets the count sets a row count projection while the other should return the actual objects matching the criteria. This implies that the total number of hits fetched when the search is done should always match the value returned by the get Count method otherwise the scripts in the widgets will fail with the assumption that there is either more hits to fetch or some will get left out. For an example implementation of the methods in the DAO layer, you can look at HibernateEncounterDAO.getCountOfEncounters(String, boolean) and HibernateEncounterDAO.getEncounters(String, Integer, Integer, boolean).
Widget properties
- minLength: int (default: 1) The minimum number of characters required to trigger a search, this is ignored if 'doSearchWhenEmpty' is set to true
- searchLabel: string (default: omsgs.searchLabel) The text to be used as the label for the search textbox
- includeVoidedLabel: string (default: omsgs.includeVoided) The text to be used as the label for the 'includeVoided' checkbox
- showIncludeVoided: bool (default: false) - Specifies whether the 'includeVoided' checkbox and label should be displayed
- includeVerboseLabel: string (default: omsgs.includeVerbose) The text to be used as the label for the 'includeVerbose' checkbox
- showIncludeVerbose: bool (default: false) Specifies whether the 'includeVerbose' checkbox and label should be displayed
- searchHandler: function(text, resultHandler, options) (default:null) The function to be called to fetch search results from the server
- resultsHandler: function(results) (default:null) The function to be called
- selectionHandler: function(index, rowData
- fieldsAndHeaders: Array of fieldNames and column header maps
- displayLength: int (default: 10)
- columnWidths: an array of column widths, the length of the array should be equal to the number of columns, should be of the same length as the number of columns
- columnRenderers: array of fnRender(s) for each column, should be of the same length as the number of columns, set a value of null for columns with no renderers
- columnVisibility: array of bVisible values for each column, true/false are the only possible values in the array and should be of the same length as the number of columns
- initialData: (default: null) The initial data to be displayed e.g if it is an encounter search, it should be an encounter list
- searchPhrase: string The phrase to be set in the search box so that a search is triggered on page load to display initial items
- doSearchWhenEmpty: string (default:false): If it is set to true, it lists all items initially and filters them with the given search phrase.
- verboseHandler: function to be called to return the text to display as verbose output