<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ZX:rishistar programming blocks &#187; jQuery</title>
	<atom:link href="http://rishistar.com/zx/category/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://rishistar.com/zx</link>
	<description>Java/Wordpress programming tips and tricks</description>
	<lastBuildDate>Thu, 28 Jul 2011 22:23:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>jQuery / Java / Autocomplete</title>
		<link>http://rishistar.com/zx/2010/03/jquery-java-autocomplete/</link>
		<comments>http://rishistar.com/zx/2010/03/jquery-java-autocomplete/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 12:25:39 +0000</pubDate>
		<dc:creator>rishi</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://rishistar.com/zx/?p=87</guid>
		<description><![CDATA[If you haven&#8217;t heard of jQuery, its a Javascript library that allows you to implement AJAX functionality in your web pages. You can find out more about it from the jQuery website. My first attempt with jQuery involved using an autocomplete  plugin provided by Pengoworks. Autocomplete is where you start typing in a few letters [...]]]></description>
			<content:encoded><![CDATA[<p>If you haven&#8217;t heard of jQuery, its a Javascript library that allows you to implement AJAX functionality in your web pages. You can find out more about it from <a href="http://jquery.com">the jQuery website</a>.</p>
<p>My first attempt with jQuery involved using an autocomplete  <a href="http://www.pengoworks.com/workshop/jquery/autocomplete.htm">plugin provided by Pengoworks</a>. Autocomplete is where you start typing in a few letters and the text box will come up with some suggestions. There was a tutorial for this present on the <a href="http://docs.jquery.com/Plugins/Autocomplete">jquery autocomplete page</a>, but it didn&#8217;t quite meet my needs, and maybe it doesn&#8217;t meet yours.</p>
<p>There are two ways in which to use the auto complete functionality. If there are a few options (ie upto 50) the list of suggestions can be embedded in the web page itself, but if there are many options (1000s) the suggestions need to come back from a server. It was this second process that the tutorial did not cover, and hence the reason for providing a step by step guide to doing it here.</p>
<p>In partcular, I&#8217;m working from Java in Netbeans 6.8.</p>
<p><span id="more-87"></span></p>
<h2>Server Side:Put data in the database/class in which it will reside</h2>
<p>The list of suggestions will live on a server somewhere. If working with 1000&#8242;s of items you will need to run it in a database.</p>
<h2>Server Side: Provide a class that will take a substring and find matches from your word list</h2>
<p>Returning your data as type List is useful.</p>
<p><code><br />
public List getAutoCompleteMatches( String starter ) throws SQLException<br />
</code></p>
<p>Another handy bit of info here is to use a regular expression in any SELECT statement being generated to match the start of a word.</p>
<pre><code><span style="color: #ff0000;">String sql = "SELECT * FROM tablename WHERE name REGEXP '^" + starter + "' " ;</span></code></pre>
<pre><span style="color: #ff0000;">String sql = "SELECT * FROM tablename WHERE name LIKE '" + starter + "'" ;</span></pre>
<h2>Server Side: Provide A Web Page Containing the results of such a query</h2>
<p>Your web page will need somewhere to call to get its results. This will take the form of a servlet or JSP page. The code for either is easy &#8211; here is the code as a JSP page.</p>
<p><code><br />
&lt;%@page import="java.util.Iterator"%&gt;<br />
&lt;%@page import="java.util.List"%&gt;<br />
&lt;%@page import="com.rishistar.mirbaseUtils.MirBaseMatches"%&gt;<br />
&lt;%<br />
MirBaseMatches m = new MirBaseMatches() ;<br />
String query = request.getParameter( "q" ) ;<br />
List matches = m.getAutoCompleteMatches( query ) ;<br />
Iterator iterator = matches.iterator() ;<br />
while( iterator.hasNext() )<br />
{<br />
out.println( (String)iterator.next() ) ;<br />
}<br />
%&gt;<br />
</code></p>
<h2>Client Side:Include the jQuery Script</h2>
<p>Obviously if you are going to use jQuery to do the job of autocompletion and fetching results from the server you&#8217;ll need to include it in the generated web page. Simple enough&#8230;download the latest version from jQuery.com and include it. There are two versions &#8211; one to use during development to allow debugging and one to use on a live site &#8211; the &#8216;min&#8217; version. Change the below to meet your path requirements.</p>
<pre>&lt;script type="text/javascript" src="jquery.1.4.2.js"&gt;
&lt;/script&gt;
&lt;script type="text/javascript" src="jquery.autocomplete.js"&gt;
&lt;/script&gt;
</pre>
<h2>Client Side:Include the code to get your autocomplete box</h2>
<p>So the final bit is putting the autocomplete box into your page.</p>
<pre>&lt;input id="mirna" name="mirna" type="text" /&gt;</pre>
<pre>&lt;script type="text/javascript"&gt;</pre>
<pre>     $("#mirna").autocomplete("getmirmatch.jsp") ;</pre>
<pre>&lt;/script&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://rishistar.com/zx/2010/03/jquery-java-autocomplete/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

