My First Encounter with jQuery
Last week was the first time I used the jQuery javascript library and I must say I’m impressed. It has one of the simplest APIs that I have used which made learning how to use it quick and easy. In fact, I was able to write a custom plugin for it within a couple hours!
I would like to cover the business problem I had and how I used jQuery to solve it. One of the internal web sites at my company has a user rights management page where an administrator can control the individual rights on the site for each user. This page has a drop down list of all the employees which is used to give rights to a specific employee. The problem was the drop down list was not alphabetized and it contained over 1,400 employee names to choose from! It felt like finding a needle in a haystack every time a new employee was given rights.
The root cause of the issue was the use of a hacked together database view that returned the list of employees ordered by their full name. Database views are not supposed to have order by commands, but this one did and the website was expecting the view to provide the ordering. However, the view was not returning the employees as properly ordered. After some research it was found that the database optimizer figured that bypassing the view and using the data directly from the table was more efficient than going through the view, which meant that any code using the view would receive an unordered list of employees.
Once that was figured out I was able to add an order by to the sql command and the drop down list was now ordered correctly.
However, there is an acronym we often use at work as our motto: MISL (pronounced missile)
Make
It
Suck
Less
And only alphabetizing the list did not Make It Suck Less enough for me. It was still a pain the neck to scroll through the 1400 employee names in the drop down list.
My solution was to use jQuery to replace the drop down list with an auto complete text box that had the same value as the drop down list.
After finding an auto complete plugin for jQuery I was ready to implement my solution.
The first thing I did was hide the drop down list and add an input text box after it
$dropDown = $("#EmployeeDropDownList");
$dropDown.hide()
.after("<input type='text' />");
var $autoCompleteTextBox = $dropDown.next();
I then retrieve the array of employee names from the drop down list and pass it as an argument to the autocomplete plugin
var dropDownItems = [];
$dropDown.find("option").each(function() {
dropDownItems.push($(this).text());
});
$autoCompleteTextBox.autocomplete(dropDownItems);
I then subscribe to the blur event on the new auto complete text box to synchronize the selected value on the hidden drop down list with the value in the auto complete text box
$autoCompleteTextBox.blur(function() {
var textBoxValue = $autoCompleteTextBox.val();
$dropDown.val(textBoxValue).attr("selected", "selected");
});
And finally I validate the value in the text box is the selected value in the drop down list when the submit button is clicked.
$("#SubmitButton").click(function(e) {
var textBoxValue = $autoCompleteTextBox.val();
var selectedValue = $dropDown.find("option:selected").text();
if (textBoxValue != selectedValue)
{
alert(textBoxValue + " is an invalid selection");
e.preventDefault();
}
});
I am still pretty amazed by how simple and succinct jQuery is. It was actually really fun to learn and play around with and I look forward to using jQuery in the future.