<?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>(function() { &#187; autocomplete</title>
	<atom:link href="http://maxb.net/blog/tag/autocomplete/feed/" rel="self" type="application/rss+xml" />
	<link>http://maxb.net/blog</link>
	<description>// MAXB.NET LABS</description>
	<lastBuildDate>Mon, 16 May 2011 19:12:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Autocomplete con jQuery</title>
		<link>http://maxb.net/blog/2009/09/14/autocomplete-con-jquery/</link>
		<comments>http://maxb.net/blog/2009/09/14/autocomplete-con-jquery/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 08:30:38 +0000</pubDate>
		<dc:creator>Massimiliano Balestrieri</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[autocomplete]]></category>
		<category><![CDATA[suggest]]></category>

		<guid isPermaLink="false">http://maxb.net/blog/?p=436</guid>
		<description><![CDATA[NB: ci sono novità al fondo del post Pubblico le mie 3 implementazioni del plugin jQuery Autocomplete di Jörn Zaefferer. Queste implementazioni servono ad attivare campi form con suggest automatico senza scrivere una sola riga di javascript. La prima implementazione &#8230; <a href="http://maxb.net/blog/2009/09/14/autocomplete-con-jquery/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><strong>NB: <a href="http://maxb.net/blog/2009/09/14/autocomplete-con-jquery/#update_post_autocomplete">ci sono novità al fondo del post</a></strong></p>
<p>Pubblico le mie 3 implementazioni del plugin jQuery <a href="http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/" target="_blank">Autocomplete</a> di Jörn Zaefferer.</p>
<p>Queste implementazioni servono ad attivare campi form con suggest automatico <strong>senza scrivere una sola riga di javascript.</strong></p>
<p>
La prima implementazione autoattiva il plugin originale (con supporto ai metadata):
</p>
<p>
<img src="http://maxb.net/blog/wp-content/uploads/2009/09/suggest1.png" alt="" />
</p>
<p>
La seconda implementazione aggiunge &#8220;magicamente&#8221; un campo &#8220;hidden&#8221; e  in fase di invio del &#8220;form&#8221; spedisce al server anche la chiave associata al valore selezionato nel suggest.
</p>
<p>
<img src="http://maxb.net/blog/wp-content/uploads/2009/09/suggest2.png" alt="" />
</p>
<p>
La terza invece crea un suggest &#8220;tabellare&#8221;. <strong>E&#8217; stato necessario modificare lo script</strong>.
</p>
<p>
<img src="http://maxb.net/blog/wp-content/uploads/2009/09/suggest3.png" alt="" />
</p>
<p>Per le demo ho usato:</p>
<ul>
<li><a href="http://jquery.com/" target="_blank">jQuery 1.3.2</a></li>
<li><a href="http://docs.jquery.com/Plugins/Metadata" target="_blank">jQuery Metadata</a></li>
<li><a href="http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/" target="_blank">jQuery Autocomplete 1.1</a></li>
</ul>
<h3>HEAD HTML</h3>
<p>Includere le librerie e i css necessari.</p>
<pre class="brush: xml; title: ; notranslate">
	&lt;link href=&quot;lib/jquery.autocomplete.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;screen&quot; /&gt;
	&lt;script src=&quot;lib/jquery-1.3.2.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;lib/jquery.metadata.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;lib/jquery.autocomplete.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
	&lt;script src=&quot;autocomplete.custom.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
</pre>
<p><a href="http://maxb.net/scripts/autocomplete/autocomplete.custom.zip">DOWNLOAD DEMO</a></p>
<h3>SUGGEST BASE</h3>
<p>Per attivare un suggest base è sufficiente</p>
<ul>
<li>assegnare al campo input la classe <strong>suggest</strong>.</li>
<li>valorizzare l&#8217;attributo alt del campo input inserendo un url <strong>relativo</strong> es: &#8220;php/mesi.php&#8221;</li>
</ul>
<p>Lo script php d&#8217;esempio che restituisce alle chiamate ajax l&#8217;elenco dei valori:</p>
<pre class="brush: php; title: ; notranslate">
$q = strtolower($_GET[&quot;q&quot;]);
if (!$q) return;
$items = array(
	'gennaio',
	'febbraio',
	'marzo',
	'aprile',
	'maggio',
	'giugno',
	'luglio',
	'agosto',
	'settembre',
	'ottobre',
	'novembre',
	'dicembre'
);

foreach ($items as $value) {
    if (strpos(strtolower($value), $q) !== false) {
        echo &quot;$value\n&quot;;
    }
}
</pre>
<p><strong>Chiaramente nelle applicazioni reali i dati non verranno presi da un array ma da un database</strong></p>
<h4>ESEMPIO 1</h4>
<ul>
<li><a href="http://maxb.net/scripts/autocomplete/1.html" target="_blank">DEMO</a></li>
</ul>
<pre class="brush: xml; title: ; notranslate">
&lt;input type=&quot;text&quot; name=&quot;suggest1&quot; id=&quot;suggest1&quot; class=&quot;suggest&quot; alt=&quot;php/mesi.php&quot; /&gt;
</pre>
<h4>ESEMPIO 2</h4>
<p>Grazie all&#8217;uso di jQuery metadata le opzioni dell&#8217;autocomplete possono essere settate direttamente nell&#8217;html (quindi senza scrivere codice javascript).<br />
L&#8217;elenco delle opzioni che modificano il comportamento del plugin si trova <a href="http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions" target="_blank">qui</a>.</p>
<ul>
<li><a href="http://maxb.net/scripts/autocomplete/2.html" target="_blank">DEMO</a></li>
</ul>
<pre class="brush: xml; title: ; notranslate">
&lt;input type=&quot;text&quot; name=&quot;suggest2&quot; id=&quot;suggest2&quot; class=&quot;suggest {multiple:true}&quot; alt=&quot;php/mesi.php&quot; /&gt;
</pre>
<h4>ESEMPIO 3</h4>
<ul>
<li><a href="http://maxb.net/scripts/autocomplete/3.html" target="_blank">DEMO</a></li>
</ul>
<pre class="brush: xml; title: ; notranslate">
&lt;input type=&quot;text&quot; name=&quot;suggest3&quot; id=&quot;suggest3&quot; class=&quot;suggest {minChars:3, max:3}&quot; alt=&quot;php/search.php&quot; /&gt;
</pre>
<h3>SUGGEST CHIAVE VALORE</h3>
<p>La seconda implementazione di jQuery Autocomplete crea un campo hidden per la chiave legata al valore visualizzato in suggest in modo da poter inviare al server sia la chiave sia il valore in fase di submit.</p>
<p>Per attivare un suggest chiave/valore è sufficiente</p>
<ul>
<li>assegnare al campo input la classe <strong>suggest_keys</strong>.</li>
<li>valorizzare l&#8217;attributo alt del campo input inserendo un url <strong>relativo</strong> es: &#8220;php/keys.php&#8221;</li>
</ul>
<p>Lo script php d&#8217;esempio che restituisce alle chiamate ajax l&#8217;elenco di chiavi|valori:</p>
<pre class="brush: php; title: ; notranslate">
$q = strtolower($_GET[&quot;q&quot;]);
if (!$q) return;
$items = array(
	&quot;10024&quot; =&gt; &quot;Moncalieri&quot;,
	&quot;20000&quot; =&gt; &quot;Monza&quot;,
	&quot;10100&quot;	=&gt; &quot;Torino&quot;
);

foreach ($items as $key=&gt;$value) {
    if (strpos(strtolower($value), $q) !== false) {
        echo &quot;$key|$value\n&quot;;
    }
}
</pre>
<h4>ESEMPIO 4</h4>
<ul>
<li><a href="http://maxb.net/scripts/autocomplete/4.html" target="_blank">DEMO</a></li>
</ul>
<pre class="brush: xml; title: ; notranslate">
&lt;input type=&quot;text&quot; name=&quot;suggest4&quot; id=&quot;suggest4&quot; class=&quot;suggest_keys&quot; alt=&quot;php/keys.php&quot; /&gt;
</pre>
<p>Se utilizzate firebug potrete vedere che viene creato un campo nascosto. Es:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;input type=&quot;hidden&quot; value=&quot;&quot; id=&quot;suggest4_hidden&quot; name=&quot;suggest4_hidden&quot;/&gt;
</pre>
<h4>ESEMPIO 5</h4>
<ul>
<li><a href="http://maxb.net/scripts/autocomplete/4.html" target="_blank">DEMO</a></li>
</ul>
<pre class="brush: xml; title: ; notranslate">
&lt;input type=&quot;text&quot; name=&quot;entita[campo]&quot; id=&quot;entita_campo&quot; class=&quot;suggest_keys&quot; alt=&quot;php/keys.php&quot; /&gt;
</pre>
<p>Se utilizzate firebug potrete vedere che viene creato un campo nascosto. Es:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;input type=&quot;hidden&quot; value=&quot;&quot; id=&quot;entita_campo_hidden&quot; name=&quot;entita[campo_hidden]&quot;/&gt;
</pre>
<h3>SUGGEST TABELLARE</h3>
<p>La terza implementazione di jQuery Autocomplete è un po&#8217; più complessa e prevede l&#8217;utilizzo di una versione modificata di jquery autocomplete.</p>
<p>Per attivare un suggest tabellare è sufficiente</p>
<ul>
<li>Includere nell&#8217;head la versione modificata di <a href="http://maxb.net/scripts/autocomplete/lib/jquery.autocomplete.mod.js" target="_blank">jQuery  Autocomplete</a></li>
<li>assegnare al campo input la classe <strong>suggest_table</strong>.</li>
<li>personalizzare le intestazioni di colonna nell&#8217;attributo class (vedi sotto l&#8217;esempio)</li>
<li>valorizzare l&#8217;attributo alt del campo input inserendo un url <strong>relativo</strong> es: &#8220;php/table.php&#8221;</li>
<li>Includere le seguenti istruzioni css personalizzando &#8220;le larghezze&#8221; che nell&#8217;esempio settano le 3 colonne della tabella</li>
</ul>
<pre class="brush: css; title: ; notranslate">
.ac_results ul li div.tbl{clear:both;}
.ac_results ul li div.tbl div{float:left;width:30%;overflow-x:hidden;}
.ac_intestazione{position:absolute;left:0px;top:-0px;z-index:1000;background:#000;color:#fff;}

/*PERSONALIZZA*/
.ac_results ul li div.tbl div.fld_1, #int_1{width:150px;}
.ac_results ul li div.tbl div.fld_2, #int_2{width:70px;}
.ac_results ul li div.tbl div.fld_3, #int_3{width:70px;}
</pre>
<p>Lo script php d&#8217;esempio che restituisce alle chiamate ajax l&#8217;elenco dei valori della tabella.</p>
<pre class="brush: php; title: ; notranslate">
$q = strtolower($_GET[&quot;q&quot;]);
if (!$q) return;
$items = array(
	&quot;10024&quot; =&gt; array(
		&quot;Moncalieri&quot;,
		&quot;Torino&quot;,
		&quot;Piemonte&quot;
	),
	&quot;20000&quot; =&gt; array(
		&quot;Conegliano&quot;,
		&quot;Treviso&quot;,
		&quot;Veneto&quot;
	),
	&quot;10100&quot;	=&gt; array(
		&quot;Alba&quot;,
		&quot;Cuneo&quot;,
		&quot;Piemonte&quot;
	)
);

foreach ($items as $key=&gt;$array) {
	$str = join($array,&quot;#&quot;);
    if (strpos(strtolower($str), $q) !== false) {
        echo &quot;$key|$str\n&quot;;
    }
}
</pre>
<h4>ESEMPIO TABELLARE</h4>
<ul>
<li><a href="http://maxb.net/scripts/autocomplete/table.html" target="_blank">DEMO</a></li>
</ul>
<pre class="brush: xml; title: ; notranslate">
&lt;input type=&quot;text&quot; id=&quot;suggest1&quot; name=&quot;suggest1&quot; class=&quot;suggest_table {th : ['città', 'provincia', 'regione']}&quot; alt=&quot;php/table.php&quot; style=&quot;width:300px;&quot; /&gt;
</pre>
<h3 id="update_post_autocomplete">AGGIORNAMENTO 13-11-2009:</h3>
<p>Ho aggiornato lo <a href="http://maxb.net/scripts/autocomplete/autocomplete.custom.zip">ZIP</a> con le demo e gli script di questo post.</p>
<p>Ho aggiornato in particolare</p>
<ul>
<li><a href="http://maxb.net/scripts/autocomplete/lib/jquery.autocomplete.mod.js" target="_blank">jQuery  Autocomplete &#8220;modificato&#8221;</a></li>
<li><a href="http://maxb.net/scripts/autocomplete/autocomplete.custom.js" target="_blank">jQuery  Autocomplete &#8220;custom&#8221;</a></li>
</ul>
<p>Ho aggiunto questa <a href="http://maxb.net/scripts/autocomplete/table_opzioni_autocomplete.html" target="_blank">DEMO</a> per il passaggio delle <strong>opzioni autocomplete</strong> al plugin base (jquery.autocomplete.mod.js).</p>
<p><strong>Le porzioni di jquery.autocomplete.js modificate sono a questo punto due</strong>, entrambe nel metodo/funzione fillList:</p>
<pre class="brush: jscript; title: ; notranslate">
var li = $(&quot;&lt;li/&gt;&quot;).html( options.highlight(formatted, term) ).addClass(i%2 == 0 ? &quot;ac_even&quot; : &quot;ac_odd&quot;).appendTo(list)[0];
//PATCH
if(i == 0 &amp;amp;&amp;amp; $(input).is(&quot;.suggest_table&quot;)){
	$(li).addClass(&quot;ac_first&quot;);
	var _ie_sucks = typeof document.body.style.maxHeight === &quot;undefined&quot; || //IE6
					(document.all &amp;amp;&amp;amp; !window.opera &amp;amp;&amp;amp; window.XMLHttpRequest); //IE7
	if (_ie_sucks || max == 1) {
		$(li).attr(&quot;style&quot;, &quot;padding-top:20px&quot;);
	}
}
//PATCH
$.data(li, &quot;ac_data&quot;, data[i]);
</pre>
<pre class="brush: jscript; title: ; notranslate">
listItems = list.find(&quot;li&quot;);
if ( options.selectFirst ) {
	//PATCH
	//listItems.slice(0, 1).addClass(CLASSES.ACTIVE);
	listItems.slice(1, 2).addClass(CLASSES.ACTIVE);
	//PATCH
	active = 0;
}
</pre>
<p>Per questa seconda modifica, ringrazio <strong>Mansportivo</strong> che nei commenti qui sotto mi ha segnalato l&#8217;anomalia.</p>
]]></content:encoded>
			<wfw:commentRss>http://maxb.net/blog/2009/09/14/autocomplete-con-jquery/feed/</wfw:commentRss>
		<slash:comments>39</slash:comments>
		</item>
	</channel>
</rss>

