Introduction
A live search is a function where get search results returned while you type. In this post I will describe how you can add such a feature using the jQuery Tokeninput plugin and perform a full text search with Domino Access Services (DAS)as your data provider.
But first let’s take a look at how the end result looks like:
As in other examples we will apply the live search to the infamous fakenames application. You can grab this directory application from Codestore.
Step 1 – Download the resources and add them to your XPage
Add the following resources to a custom control:
Step 2 – Add the filter and search field
In the result screenshot you noticed the following elements:
- A filter (People, Groups, Holidays)
- A search field (with typeahead).
- A (formatted) result list.
Therefor add the following HTML on your custom control:
As you notice we will be using Bootstrap to make the appearance a bit more attractive.
search_param
The hidden input element search_param will be the container for our selected filter. The following code will set the value:
livesearch-input
The Tokeninput plugin will inject an (initially empty) unordered result list and input field in the livesearch-input input element.
Include a function to initiate the Tokeninput plugin:
Step 3 – Activate the plugin
In order to activate the Tokeninput plugin you need to include a script block control on your custom control and ad the following script:
function getObjType() {
var val = $(“#search_param”).val();
return val;
}var restPrefix = “./api/data/collections/name/”
var hintText = “Enter text, use asterix(*) for Fulltext wildcards”;
var searchingText = “Searching…”;
var queryParam = “search”;
var propertyToSearch = “name”;
var searchDelay = 2000;
var minChars = 2;
var resultsLimit = 5; //not working??
var noResultsText = “No matches”;
var tokenLimit = 1;
var preventDuplicates = true;
var onAdd = function(item) {
var objType = getObjType();
var docID = item[‘@unid’];
var URL = “”;
switch (objType) {
case “Groups”:
URL = “Group.xsp?unid=” + docID;
break;
case “Holidays”:
URL = “Holiday.xsp?unid=” + docID;
break;
default:
URL = “Person.xsp?unid=” + docID;
}
window.location = URL;
};
var resultsFormatter = function(item) {
var objType = getObjType();
switch (objType) {
case “Group”:
sub = “dummy group sub”;
break;
case “Holiday”:
sub = “dummy Holiday sub”;
break;
default:
sub = “” + item.job;
}
if (sub == “”) {
sub = “No title available“;
}
if (sub.length > 30) sub = sub.substring(0, 30) + “…”;
var name = item.name;
if (name.length > 30) name = name.substring(0, 30) + “…”;
var img = ‘‘;
var text = ‘‘ + name + ‘
‘ + sub + ‘
‘
return ‘
- ‘ + ‘
‘ + img + text + ‘‘;
};function setPlaceHolder() {
$(“#token-input-livesearch-input”).attr(“placeholder”, “Enter text”);
}function init() {
$(“#livesearch-input”).tokenInput(restPrefix + getObjType(), {
hintText: hintText,
searchingText: searchingText,
queryParam: queryParam,
propertyToSearch: propertyToSearch,
searchDelay: searchDelay,
minChars: minChars,
resultsLimit: resultsLimit,
noResultsText: noResultsText,
tokenLimit: tokenLimit,
onAdd: onAdd,
preventDuplicates: preventDuplicates,
resultsFormatter: resultsFormatter
});
setPlaceHolder();
}function re_init(objType) {
$(“#token-input-livesearch-input”).remove();
$(“.token-input-list”).remove();
$(“#livesearch-input”).tokenInput(restPrefix + getObjType(), {
hintText: hintText,
searchingText: searchingText,
queryParam: queryParam,
propertyToSearch: propertyToSearch,
searchDelay: searchDelay,
minChars: minChars,
resultsLimit: resultsLimit,
noResultsText: noResultsText,
tokenLimit: tokenLimit,
onAdd: onAdd,
preventDuplicates: preventDuplicates,
resultsFormatter: resultsFormatter
});
setPlaceHolder();
}function submitSearch() {
//not required, overwritten by onAdd function.
}
As you see the Tokeinput plugin can be steered via diverse parameters. A clear description of the available options you can read here.
In our case we determine:
Data Provider
var restPrefix = “./api/data/collections/name/”
This defines we will be using Domino Access Services as data provider. We will use the names of the views People, Groups, Holidays which correspond with the options in our filter list.
Remember you need to enable DAS for the database AND the views.
onAdd = function(item)
The variable onAdd is a function that will be called when we select an item from the result list. We have set to allow only one item to be selected and the onAdd function will open a new window location with the corresponding object/document.
URL = “Person.xsp?unid=” + docID;
}
window.location = URL;
resultsFormatter = function(item)
This variable defines how the items in the collection provided by Domino Access Services will be presented in the UI. You can be as creative with it as you like. I choose for the option for an image, distinguished name and a sub-title e.g. the function for a person.
Ready
Are we done already? Yes we are! All the heavy lifting is done by the Tokeninput plugin and Domino Access Services.
Notice that Full Text search in Domino Access Services supports the usage of an asterix. Otherwise you get only results returned with an exact match.
Use a plugin for your browser to check the URL call and response to DAS.
Some final thoughts
Do I found the live search feature useful?
– Yeah. Especially in mobile web applications you want to avoid that a user has to go back to home navigation each and every time. And with large data-sets infinite scrolling is a good option.
Do I find the Tokeninput plugin valuable?
– I find the plugin easy to understand but I noticed not all properties are implemented An example of something what I expected to be implemented is the number of returned results (fixed to max 10 items).
Can I recommend the plugin?
– In case you use Bootstrap applying a bootstrap theme meant mostly stripping the default style sheet. In case you use the Application Layout control from the Extension Library you will discover some problems:
- When applying the live search in the search bar you get overlay with the Utility links.
- When applying a filter option you get scrolling issues when opening the dropdown menu in the navbar section.
- The result list can not be displayed direct under the search field due to the overlay of the navbar and it’s margins.
The consequence of these problems above made me decide to place the search bar in the main column section instead of the search bar and align it to the right via the pull-right class in Bootstrap. The result looks similar but I loose some white space. (perhaps the solution lies in understanding Bootstrap a bit more).
In case you can live with these conditions or restrictions I think you have an awesome desired feature!
Your turn!
What are your thoughts? In case you have provide a live search feature via some other plugin or custom code I am happy to hear from you.
I have uploaded a sample on Dropbox. I am happy to hear the improvements you have made.
Happy coding =)