Zend_Controller-ActionHelpers-AutoComplete.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect3 id="zend.controller.actionhelpers.autocomplete">
  4. <title>AutoComplete</title>
  5. <para>
  6. Many <acronym>AJAX</acronym> javascript libraries offer functionality for providing
  7. autocompletion whereby a selectlist of potentially matching results is
  8. displayed as the user types. The <emphasis>AutoComplete</emphasis> helper aims
  9. to simplify returning acceptable responses to such methods.
  10. </para>
  11. <para>
  12. Since not all JS libraries implement autocompletion in the same way, the
  13. <emphasis>AutoComplete</emphasis> helper provides some abstract base
  14. functionality necessary to many libraries, and concrete implementations
  15. for individual libraries. Return types are generally either <acronym>JSON</acronym> arrays
  16. of strings, <acronym>JSON</acronym> arrays of arrays (with each member array being an
  17. associative array of metadata used to create the selectlist), or <acronym>HTML</acronym>.
  18. </para>
  19. <para>
  20. Basic usage for each implementation is the same:
  21. </para>
  22. <programlisting language="php"><![CDATA[
  23. class FooController extends Zend_Controller_Action
  24. {
  25. public function barAction()
  26. {
  27. // Perform some logic...
  28. // Encode and send response;
  29. $this->_helper->autoCompleteDojo($data);
  30. // Or explicitly:
  31. $response = $this->_helper->autoCompleteDojo
  32. ->sendAutoCompletion($data);
  33. // Or simply prepare autocompletion response:
  34. $response = $this->_helper->autoCompleteDojo
  35. ->prepareAutoCompletion($data);
  36. }
  37. }
  38. ]]></programlisting>
  39. <para>
  40. By default, autocompletion does the following:
  41. </para>
  42. <itemizedlist>
  43. <listitem><para>Disables layouts and ViewRenderer.</para></listitem>
  44. <listitem><para>Sets appropriate response headers.</para></listitem>
  45. <listitem>
  46. <para>Sets response body with encoded or formatted autocompletion data.</para>
  47. </listitem>
  48. <listitem><para>Sends response.</para></listitem>
  49. </itemizedlist>
  50. <para>
  51. Available methods of the helper include:
  52. </para>
  53. <itemizedlist>
  54. <listitem>
  55. <para>
  56. <methodname>disableLayouts()</methodname> can be used to disable layouts and
  57. the ViewRenderer. Typically, this is called within
  58. <methodname>prepareAutoCompletion()</methodname>.
  59. </para>
  60. </listitem>
  61. <listitem>
  62. <para>
  63. <methodname>encodeJson($data, $keepLayouts = false)</methodname> will encode
  64. data to <acronym>JSON</acronym>, optionally enabling or disabling layouts.
  65. Typically, this is called within
  66. <methodname>prepareAutoCompletion()</methodname>.
  67. </para>
  68. </listitem>
  69. <listitem>
  70. <para>
  71. <methodname>prepareAutoCompletion($data, $keepLayouts = false)</methodname>
  72. is used to prepare data in the response format necessary for the
  73. concrete implementation, optionally enabling or disabling
  74. layouts. The return value will vary based on the implementation.
  75. </para>
  76. </listitem>
  77. <listitem>
  78. <para>
  79. <methodname>sendAutoCompletion($data, $keepLayouts = false)</methodname>
  80. is used to send data in the response format necessary for the
  81. concrete implementation. It calls
  82. <methodname>prepareAutoCompletion()</methodname>, and then sends the
  83. response.
  84. </para>
  85. </listitem>
  86. <listitem>
  87. <para>
  88. <methodname>direct($data, $sendNow = true, $keepLayouts =
  89. false)</methodname> is used when calling the helper as a method of
  90. the helper broker. The <varname>$sendNow</varname> flag is used to
  91. determine whether to call <methodname>sendAutoCompletion()</methodname> or
  92. <methodname>prepareAutoCompletion()</methodname>, respectively.
  93. </para>
  94. </listitem>
  95. </itemizedlist>
  96. <para>
  97. Currently, <emphasis>AutoComplete</emphasis> supports the Dojo and Scriptaculous
  98. <acronym>AJAX</acronym> libraries.
  99. </para>
  100. <sect4 id="zend.controller.actionhelpers.autocomplete.dojo">
  101. <title>AutoCompletion with Dojo</title>
  102. <para>
  103. Dojo does not have an AutoCompletion widget per se, but has two
  104. widgets that can perform AutoCompletion: ComboBox and
  105. FilteringSelect. In both cases, they require a data store that
  106. implements the QueryReadStore; for more information on these topics, see the <ulink
  107. url="http://dojotoolkit.org/reference-guide/dojo/data.html">dojo.data</ulink>
  108. documentation.
  109. </para>
  110. <para>
  111. In Zend Framework, you can pass a simple indexed array to the
  112. AutoCompleteDojo helper, and it will return a <acronym>JSON</acronym> response suitable
  113. for use with such a store:
  114. </para>
  115. <programlisting language="php"><![CDATA[
  116. // within a controller action:
  117. $this->_helper->autoCompleteDojo($data);
  118. ]]></programlisting>
  119. <example id="zend.controller.actionhelpers.autocomplete.dojo.example1">
  120. <title>AutoCompletion with Dojo Using Zend MVC</title>
  121. <para>
  122. AutoCompletion with Dojo via the Zend <acronym>MVC</acronym> requires several
  123. things: generating a form object for the ComboBox on which you
  124. want AutoCompletion, a controller action for serving the
  125. AutoCompletion results, creating a custom QueryReadStore to connect
  126. to the AutoCompletion action, and generation of the javascript
  127. to use to initialize AutoCompletion on the server side.
  128. </para>
  129. <para>
  130. First, let's look at the javascript necessary. Dojo offers a
  131. complete framework for creating <acronym>OOP</acronym> javascript, much as Zend
  132. Framework does for <acronym>PHP</acronym>. Part of that is the ability to create
  133. pseudo-namespaces using the directory hierarchy. We'll create a
  134. 'custom' directory at the same level as the Dojo directory
  135. that's part of the Dojo distribution. Inside that directory,
  136. we'll create a javascript file, <filename>TestNameReadStore.js</filename>, with the
  137. following contents:
  138. </para>
  139. <programlisting language="javascript"><![CDATA[
  140. dojo.provide("custom.TestNameReadStore");
  141. dojo.declare("custom.TestNameReadStore", dojox.data.QueryReadStore, {
  142. fetch:function (request) {
  143. request.serverQuery = { test:request.query.name };
  144. return this.inherited("fetch", arguments);
  145. }
  146. });
  147. ]]></programlisting>
  148. <para>
  149. This class is simply an extension of Dojo's own QueryReadStore,
  150. which is itself an abstract class. We simply define a method by
  151. which to request, and assigning it to the 'test' element.
  152. </para>
  153. <para>
  154. Next, let's create the form element for which we want
  155. AutoCompletion:
  156. </para>
  157. <programlisting language="php"><![CDATA[
  158. class TestController extends Zend_Controller_Action
  159. {
  160. protected $_form;
  161. public function getForm()
  162. {
  163. if (null === $this->_form) {
  164. $this->_form = new Zend_Form();
  165. $this->_form->setMethod('get')
  166. ->setAction(
  167. $this->getRequest()->getBaseUrl() . '/test/process'
  168. )
  169. ->addElements(array(
  170. 'test' => array('type' => 'text', 'options' => array(
  171. 'filters' => array('StringTrim'),
  172. 'dojoType' => array('dijit.form.ComboBox'),
  173. 'store' => 'testStore',
  174. 'autoComplete' => 'false',
  175. 'hasDownArrow' => 'true',
  176. 'label' => 'Your input:',
  177. )),
  178. 'go' => array('type' => 'submit',
  179. 'options' => array('label' => 'Go!'))
  180. ));
  181. }
  182. return $this->_form;
  183. }
  184. }
  185. ]]></programlisting>
  186. <para>
  187. Here, we simply create a form with 'test' and 'go' methods. The
  188. 'test' method adds several special, Dojo-specific attributes:
  189. dojoType, store, autoComplete, and hasDownArrow. The dojoType is
  190. used to indicate that we are creating a ComboBox, and we will
  191. link it to a data store (key 'store') of 'testStore' -- more on
  192. that later. Specifying 'autoComplete' as <constant>FALSE</constant> tells
  193. Dojo not to automatically select the first match, but instead show a list of
  194. matches. Finally, 'hasDownArrow' creates a down arrow similar to
  195. a select box so we can show and hide the matches.
  196. </para>
  197. <para>
  198. Let's add a method to display the form, as well as an end point
  199. for processing AutoCompletion:
  200. </para>
  201. <programlisting language="php"><![CDATA[
  202. class TestController extends Zend_Controller_Action
  203. {
  204. // ...
  205. /**
  206. * Landing page
  207. */
  208. public function indexAction()
  209. {
  210. $this->view->form = $this->getForm();
  211. }
  212. public function autocompleteAction()
  213. {
  214. if ('ajax' != $this->_getParam('format', false)) {
  215. return $this->_helper->redirector('index');
  216. }
  217. if ($this->getRequest()->isPost()) {
  218. return $this->_helper->redirector('index');
  219. }
  220. $match = trim($this->getRequest()->getQuery('test', ''));
  221. $matches = array();
  222. foreach ($this->getData() as $datum) {
  223. if (0 === strpos($datum, $match)) {
  224. $matches[] = $datum;
  225. }
  226. }
  227. $this->_helper->autoCompleteDojo($matches);
  228. }
  229. }
  230. ]]></programlisting>
  231. <para>
  232. In our <methodname>autocompleteAction()</methodname> we do a number of
  233. things. First, we look to make sure we have a post request, and
  234. that there is a 'format' parameter set to the value 'ajax';
  235. these are simply to help reduce spurious queries to the action.
  236. Next, we check for a 'test' parameter, and compare it against
  237. our data. (I purposely leave out the implementation of
  238. <methodname>getData()</methodname> here -- it could be any sort of data
  239. source.) Finally, we send our matches to our AutoCompletion
  240. helper.
  241. </para>
  242. <para>
  243. Now that we have all the pieces on the backend, let's look at
  244. what we need to deliver in our view script for the landing page.
  245. First, we need to setup our data store, then render our form,
  246. and finally ensure that the appropriate Dojo libraries --
  247. including our custom data store -- get loaded. Let's look at the
  248. view script, which comments the steps:
  249. </para>
  250. <programlisting language="php"><![CDATA[
  251. <?php // setup our data store: ?>
  252. <div dojoType="custom.TestNameReadStore" jsId="testStore"
  253. url="<?php echo $this->baseUrl() ?>/unit-test/autocomplete/format/ajax"
  254. requestMethod="get"></div>
  255. <?php // render our form: ?>
  256. <?php echo $this->form ?>
  257. <?php // setup Dojo-related CSS to load in HTML head: ?>
  258. <?php $this->headStyle()->captureStart() ?>
  259. @import "<?php echo $this->baseUrl()
  260. ?>/javascript/dijit/themes/tundra/tundra.css";
  261. @import "<?php echo $this->baseUrl() ?>/javascript/dojo/resources/dojo.css";
  262. <?php $this->headStyle()->captureEnd() ?>
  263. <?php // setup javascript to load in HTML head, including all required
  264. // Dojo libraries: ?>
  265. <?php $this->headScript()
  266. ->setAllowArbitraryAttributes(true)
  267. ->appendFile($this->baseUrl() . '/javascript/dojo/dojo.js',
  268. 'text/javascript',
  269. array('djConfig' => 'parseOnLoad: true'))
  270. ->captureStart() ?>
  271. djConfig.usePlainJson=true;
  272. dojo.registerModulePath("custom","../custom");
  273. dojo.require("dojo.parser");
  274. dojo.require("dojox.data.QueryReadStore");
  275. dojo.require("dijit.form.ComboBox");
  276. dojo.require("custom.TestNameReadStore");
  277. <?php $this->headScript()->captureEnd() ?>
  278. ]]></programlisting>
  279. <para>
  280. Note the calls to view helpers such as headStyle and headScript;
  281. these are placeholders, which we can then render in the <acronym>HTML</acronym>
  282. head section of our layout view script.
  283. </para>
  284. <para>
  285. We now have all the pieces to get Dojo AutoCompletion working.
  286. </para>
  287. </example>
  288. </sect4>
  289. <sect4 id="zend.controller.actionhelpers.autocomplete.scriptaculous">
  290. <title>AutoCompletion with Scriptaculous</title>
  291. <para>
  292. <ulink
  293. url="http://wiki.script.aculo.us/scriptaculous/show/Ajax.Autocompleter">Scriptaculous</ulink>
  294. expects an <acronym>HTML</acronym> response in a specific format.
  295. </para>
  296. <para>
  297. The helper to use with this library is 'AutoCompleteScriptaculous'.
  298. Simply provide it an array of data, and the helper will create an
  299. <acronym>HTML</acronym> response compatible with Ajax.Autocompleter.
  300. </para>
  301. </sect4>
  302. </sect3>
  303. <!--
  304. vim:se ts=4 sw=4 et:
  305. -->