Zend_Controller-ActionHelpers-AutoComplete.xml 14 KB

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