Java Reference
In-Depth Information
Figure 15.11: Filtering the contact list
Filtering the Contact List
As the user types characters in the Filter field, the contact list is fil-
tered to show only the contacts for whom either the first name or the
last name starts with the letters in the filter. An example is shown in
We can use a plain HTML text field with an
onkeyup=
event:
<s:url var="url"
beanclass="stripesbook.action.ContactListActionBean"/>
<fmt:message key="contactList.filter"/>
:
<input type="text" onkeyup="filterContacts(this, '${url}');"/>
The
filterContacts
( ) JavaScript method receives the text field and the
URL to
ContactListActionBean
. A simple Ajax request to the
findByName
( )
event handler is made, passing the characters from the text field via the
filter
parameter. The response is the page fragment with the filtered con-
tact table. It's easy to place the fragment back with a jQuery selector:
'#contact_table'
refers to the element with
id="contact_table"
.
function
filterContacts(field, url) {
$.get(url,
{ 'filter': $(field).
val
(),
'_eventName': 'findByName'
},
function
(data) {
$('#contact_table').html(data);
}
);
}
Search WWH ::
Custom Search