<?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>M4d3L-Network Blog&#187; Javascript</title>
	<atom:link href="http://www.m4d3l-network.com/category/developpement/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.m4d3l-network.com</link>
	<description>Ma seule limite est mon imagination</description>
	<lastBuildDate>Wed, 07 Jul 2010 21:43:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Autocomplete field with Zend Framework and Dojo</title>
		<link>http://www.m4d3l-network.com/developpement/php/autocomplete-field-with-zend-framework-and-dojo/</link>
		<comments>http://www.m4d3l-network.com/developpement/php/autocomplete-field-with-zend-framework-and-dojo/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 00:20:32 +0000</pubDate>
		<dc:creator>M4d3L</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[autocomplete]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[dojox]]></category>
		<category><![CDATA[field]]></category>
		<category><![CDATA[FilteringSelect]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[QueryReadStore]]></category>
		<category><![CDATA[Zend_Form]]></category>
		<category><![CDATA[zf]]></category>
		<guid isPermaLink="false">http://www.m4d3l-network.com/?p=416</guid>
		<description><![CDATA[In this exemple, we will se how to create an autocomplete field with Zend Framework and Dojo in the ZF MVC environment. We will use the QueryReadStore Dojo component to load new data when input value change. Initialize Dojo in your layout In the header of your layout, you must be able to load Dojo [...]
Related posts:<ol><li><a href='http://www.m4d3l-network.com/developpement/php/zend-framework/zend_form-how-to-remove-decorator-from-hidden-field/' rel='bookmark' title='Permanent Link: Zend_Form : How to remove decorator from hidden field.'>Zend_Form : How to remove decorator from hidden field.</a></li>
<li><a href='http://www.m4d3l-network.com/developpement/php/zend-framework/add-language-route-to-your-zend-framework-project/' rel='bookmark' title='Permanent Link: Add language route to your Zend Framework project.'>Add language route to your Zend Framework project.</a></li>
<li><a href='http://www.m4d3l-network.com/developpement/php/zend-framework/how-to-use-flickr-with-zend-framework/' rel='bookmark' title='Permanent Link: How to use Flickr with Zend Framework?'>How to use Flickr with Zend Framework?</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><p>In this exemple, we will se how to create an autocomplete field with Zend Framework and Dojo in the ZF MVC environment. We will use the QueryReadStore Dojo component to load new data when input value change.</p>
<p><span id="more-416"></span></p>
<h3>Initialize Dojo in your layout</h3>
<p>In the header of your layout, you must be able to load Dojo in page that need it loaded only to not overload other page that doesn&#8217;t have Javascript in it.</p>
<p>Exemple :</p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doctype</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'XHTML1_STRICT'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; lang=&quot;en&quot; xml:lang=&quot;en&quot;&gt;
&lt;head&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">headTitle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getActionName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">headTitle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getControllerName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">headLink</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendStylesheet</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">theme_url</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/css/style.css'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">headLink</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'rel'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'favicon'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'href'</span> <span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'/images/favicon.ico'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'PREPEND'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">headMeta</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendHttpEquiv</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'expires'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Wed, 26 Feb 1997 08:21:57 GMT'</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendHttpEquiv</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pragma'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'no-cache'</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendHttpEquiv</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'text/html;charset=UTF-8'</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendHttpEquiv</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Cache-Control'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'no-cache'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dojo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dojo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLocalPath</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">BaseUrl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/plugins/dojo/dojo.js'</span><span style="color: #009900;">&#41;</span>
         <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addStyleSheetModule</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dijit.themes.tundra'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dojo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/head&gt;</pre></td></tr></table></div>
<p>This code will add all javascript library needed and css style in the head of your page.</p>
<h3>Setup the form and choose the good element</h3>
<p>In your main view, you create the form and add the good element for your autocomplete input.</p>
<p>Dojo provide 2 type of Element : FilteringSelect and ComboBox. For this exemple, I have choose the FilteringSelect element.</p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Form<span style="color: #339933;">;</span>
<span style="color: #000088;">$element</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Dojo_Form_Element_FilteringSelect<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$element</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAutoComplete</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
			<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStoreId</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tidStore'</span><span style="color: #009900;">&#41;</span>
			<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStoreType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dojox.data.QueryReadStore'</span><span style="color: #009900;">&#41;</span>
			<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStoreParams</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'url'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">BaseUrl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/admin/translate/autocomplete'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttrib</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;searchAttr&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setRequired</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$submit</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'submit'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'submit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElements</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$element</span><span style="color: #339933;">,</span> <span style="color: #000088;">$submit</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #000088;">$form</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>
<h3>Providing data for the autocomplete field</h3>
<p>Now in the controller, we create a new action autocompleteAction to generate data in json format.<br />
In this function, you must disable the view renderer and the layout.</p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> autocompleteAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_helper<span style="color: #339933;">-&gt;</span><span style="color: #004000;">viewRenderer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setNoRender</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_helper<span style="color: #339933;">-&gt;</span><span style="color: #004000;">layout</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disableLayout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_getParam<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//fix : remove * at the end of the ID.</span>
&nbsp;
        <span style="color: #000088;">$MyTable</span><span style="color: #339933;">=</span> MyTable<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$select</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$MyTable</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">limit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000088;">$select</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id LIKE ?'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;%&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$id</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;%&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$MyTable</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchAll</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$select</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Dojo_Data<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$results</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Send our output</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_helper<span style="color: #339933;">-&gt;</span><span style="color: #004000;">autoCompleteDojo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>
<p>This action will be called by the form element each time the value will change in the input and add propose 5 new possibility for the user to select. </p>
<p>(This exemple was tested on Zend Framework 1.9.5 and Dojo 1.3.2)</p>
<!-- google_ad_section_end --><img src="http://www.m4d3l-network.com/?ak_action=api_record_view&id=416&type=feed" alt="" />
<p>Related posts:<ol><li><a href='http://www.m4d3l-network.com/developpement/php/zend-framework/zend_form-how-to-remove-decorator-from-hidden-field/' rel='bookmark' title='Permanent Link: Zend_Form : How to remove decorator from hidden field.'>Zend_Form : How to remove decorator from hidden field.</a></li>
<li><a href='http://www.m4d3l-network.com/developpement/php/zend-framework/add-language-route-to-your-zend-framework-project/' rel='bookmark' title='Permanent Link: Add language route to your Zend Framework project.'>Add language route to your Zend Framework project.</a></li>
<li><a href='http://www.m4d3l-network.com/developpement/php/zend-framework/how-to-use-flickr-with-zend-framework/' rel='bookmark' title='Permanent Link: How to use Flickr with Zend Framework?'>How to use Flickr with Zend Framework?</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.m4d3l-network.com/developpement/php/autocomplete-field-with-zend-framework-and-dojo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
