Online Book Reader

Home Category

AJAX In Action [181]

By Root 3880 0
none;

}

The position of the span is initially set to arbitrary positions on the screen by adding the top and left parameters. We set a default width for our span and set the z-index to be the uppermost layer on the page. The CSS rule also lets us style the background and border of our span so it stands out on the page. The display property is set to none so that it is hidden from the user’s view when the page is initially loaded. As the user starts to input data in the type-ahead text field, the display property is changed so that we can see the results.

Assigning the type-ahead functionality to a textbox

Because we may want to use the type-ahead functionality on multiple fields, we should develop a way to have different properties assigned to the various elements. The properties are used to determine how the script reacts. We set properties to match text with case sensitivity, match anywhere in the text, use timeouts, and perform other features we will discuss shortly. One way to do this is to build an object that contains all the needed parameters that are unique to the individual textbox. Therefore, when we have the textbox in focus, we can reference the object that is attached to the element to obtain the correct settings. In listing 10.6, a new object is created so we are able to organize the list of parameters that we assign to the textbox. Listing 10.6 Building a custom object

function SetProperties(xElem,xHidden,xserverCode,

xignoreCase,xmatchAnywhere,xmatchTextBoxWidth,

xshowNoMatchMessage,xnoMatchingDataMessage,xuseTimeout,

xtheVisibleTime){

var props={

elem: xElem,

hidden: xHidden,

serverCode: xserverCode,

regExFlags: ( (xignoreCase) ? "i" : "" ), regExAny: ( (xmatchAnywhere) ? "^" : "" ), matchAnywhere: xmatchAnywhere,

matchTextBoxWidth: xmatchTextBoxWidth,

theVisibleTime: xtheVisibleTime,

showNoMatchMessage: xshowNoMatchMessage,

Licensed to jonathan zheng

374

CHAPTER 10

Type-ahead suggest

noMatchingDataMessage: xnoMatchingDataMessage,

useTimeout: xuseTimeout

};

AddHandler(xElem);

return props;

}

The first step in creating our objects for the type-ahead suggest is to create a new function called setProperties(), which can assign properties to the object. In this example, we are going to be passing in several parameters to this function. The list of parameters includes the textbox that the type-ahead is assigned to, the hidden element used to hold the value, the URL to the server-side page, a boolean to ignore case in the search, a boolean to match the text anywhere in the string, a boolean to match the textbox width, a boolean to show no matching message, the message to display, a boolean to determine if the options should hide after a given period of time, and the time span it should remain open.

This is a large list of parameters to pass into the function. We must take these parameters and assign them to our object. To do this, we use the JavaScript Object Notation (JSON), which we describe in more detail in appendix B. The keyword is defined before the colon, and the value afterward. Our treatment of two parameters, ignoreCase and matchAnywhere, is slightly more complex. Instead of storing the boolean value, we store the regular expression equivalent in the property. In this case, we use i to ignore case and ^ to match the beginning of a string in regular expressions. It is easier for us to set the regular expression parameters here instead of using if statements each time the functions are called. The last step in our function is assigning the event handlers to the textbox. For this example, we’ll call a function that adds the event handlers automatically. We develop the code for the function in a moment, but first let’s call the function SetProperties() to create our object. The code in listing 10.7 is executed on the page onload event handler, enabling us to set the properties to the textbox. Listing 10.7 Initializing the script

window.onload = function(){

var elemSpan = document.createElement("span");

elemSpan.id = "spanOutput";

elemSpan.className =

Return Main Page Previous Page Next Page

®Online Book Reader