autocomplete.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset='utf-8'>
  5. <title>Autocomplete cell type - Handsontable</title>
  6. <!--
  7. Loading Handsontable (full distribution that includes all dependencies)
  8. -->
  9. <script data-jsfiddle="common" src="../demo/js/jquery.min.js"></script>
  10. <link data-jsfiddle="common" rel="stylesheet" media="screen" href="../dist/handsontable.css">
  11. <link data-jsfiddle="common" rel="stylesheet" media="screen" href="../dist/pikaday/pikaday.css">
  12. <script data-jsfiddle="common" src="../dist/pikaday/pikaday.js"></script>
  13. <script data-jsfiddle="common" src="../dist/moment/moment.js"></script>
  14. <script data-jsfiddle="common" src="../dist/zeroclipboard/ZeroClipboard.js"></script>
  15. <script data-jsfiddle="common" src="../dist/numbro/numbro.js"></script>
  16. <script data-jsfiddle="common" src="../dist/numbro/languages.js"></script>
  17. <script data-jsfiddle="common" src="../dist/handsontable.js"></script>
  18. <!--
  19. Loading demo dependencies. They are used here only to enhance the examples on this page
  20. -->
  21. <link data-jsfiddle="common" rel="stylesheet" media="screen" href="css/samples.css?20140331">
  22. <script src="js/samples.js"></script>
  23. <script src="js/highlight/highlight.pack.js"></script>
  24. <link rel="stylesheet" media="screen" href="js/highlight/styles/github.css">
  25. <link rel="stylesheet" href="css/font-awesome/css/font-awesome.min.css">
  26. <!--
  27. Facebook open graph. Don't copy this to your project :)
  28. -->
  29. <meta property="og:title" content="Autocomplete cell type">
  30. <meta property="og:description"
  31. content="This example shows the usage of the Autocomplete feature.">
  32. <meta property="og:url" content="http://handsontable.com/demo/autocomplete.html">
  33. <meta property="og:image" content="http://handsontable.com/demo/image/og-image.png">
  34. <meta property="og:image:type" content="image/png">
  35. <meta property="og:image:width" content="409">
  36. <meta property="og:image:height" content="164">
  37. <link rel="canonical" href="http://handsontable.com/demo/autocomplete.html">
  38. <!--
  39. Google Analytics for GitHub Page. Don't copy this to your project :)
  40. -->
  41. <script src="js/ga.js"></script>
  42. <script data-jsfiddle="common">
  43. function getCarData() {
  44. return [
  45. ["Nissan", 2009, "black", "black"],
  46. ["Nissan", 2006, "blue", "blue"],
  47. ["Chrysler", 2004, "yellow", "black"],
  48. ["Volvo", 2012, "white", "gray"]
  49. ];
  50. }
  51. </script>
  52. </head>
  53. <body>
  54. <div class="wrapper">
  55. <div class="wrapper-row">
  56. <div id="global-menu-clone">
  57. <h1><a href="../index.html">Handsontable</a></h1>
  58. </div>
  59. <div id="container">
  60. <div class="columnLayout">
  61. <div class="rowLayout">
  62. <div class="descLayout">
  63. <div class="pad">
  64. <h2>Autocomplete cell type</h2>
  65. <p>This page shows how to configure Handsontable with Autocomplete cell type, which expands the
  66. <a href="handsontable.html">Handsontable cell type.</a> with features typical for a combo box that
  67. updates its options based on user input.</p>
  68. <p>There are three demos on this page:</p>
  69. <ul>
  70. <li><a href="#lazy">Autocomplete lazy mode</a></li>
  71. <li><a href="#strict">Autocomplete strict mode</a></li>
  72. <li><a href="#strict-ajax">Autocomplete strict mode (Ajax)</a></li>
  73. </ul>
  74. </div>
  75. </div>
  76. </div>
  77. <div class="rowLayout">
  78. <div class="descLayout">
  79. <div class="pad" data-jsfiddle="example1">
  80. <a name="lazy"></a>
  81. <h2>Autocomplete lazy mode</h2>
  82. <p>This example shows the usage of the Autocomplete feature in the default <strong>lazy&nbsp;mode</strong>. In this mode,
  83. user can choose one of the suggested options while typing or enter a custom value that is not included in the suggestions.</p>
  84. <p>In this mode, the mouse and keyboard bindings are identical as in <a href="handsontable.html">Handsontable cell type.</a></p>
  85. <p>The options are rendered from the <code>source</code> property which can be an array, or a function that returns an array.</p>
  86. <div id="example1"></div>
  87. <p>
  88. <button name="dump" data-dump="#example1" data-instance="hot1" title="Prints current data source to Firebug/Chrome Dev Tools">Dump
  89. data to console
  90. </button>
  91. </p>
  92. </div>
  93. </div>
  94. <div class="codeLayout">
  95. <div class="pad">
  96. <div class="jsFiddle">
  97. <button class="jsFiddleLink" data-runfiddle="example1">Edit in jsFiddle</button>
  98. </div>
  99. <script data-jsfiddle="example1">
  100. var example1 = document.getElementById('example1'),
  101. hot1;
  102. hot1 = new Handsontable(example1, {
  103. data: getCarData(),
  104. startRows: 7,
  105. startCols: 4,
  106. colHeaders: ['Car', 'Year', 'Chassis color', 'Bumper color'],
  107. columns: [
  108. {
  109. type: 'autocomplete',
  110. source: ['BMW', 'Chrysler', 'Nissan', 'Suzuki', 'Toyota', 'Volvo'],
  111. strict: false
  112. },
  113. {type: 'numeric'},
  114. {
  115. type: 'autocomplete',
  116. source: ['yellow', 'red', 'orange', 'green', 'blue', 'gray', 'black', 'white', 'purple', 'lime', 'olive', 'cyan'],
  117. strict: false
  118. },
  119. {
  120. type: 'autocomplete',
  121. source: ['yellow', 'red', 'orange', 'green', 'blue', 'gray', 'black', 'white', 'purple', 'lime', 'olive', 'cyan'],
  122. strict: false
  123. }
  124. ]
  125. });
  126. </script>
  127. </div>
  128. </div>
  129. </div>
  130. <div class="rowLayout">
  131. <div class="descLayout">
  132. <div class="pad" data-jsfiddle="example2">
  133. <a name="strict"></a>
  134. <h2>Autocomplete strict mode</h2>
  135. <p>This is the same example as above with a difference that autocomplete now runs in <strong>strict
  136. mode</strong>.</p>
  137. <p>In this mode, the autocomplete cells will only accept values that are defined in the source
  138. array.</p>
  139. <p>In this mode, the mouse and keyboard bindings are identical as in <a href="handsontable.html">Handsontable cell type</a> with the below differences:</p>
  140. <ul>
  141. <li>If there is at least one option visible, there always is a selection in HOT-in-HOT.</li>
  142. <li>When the first row is selected, pressing <kbd>Arrow up</kbd> does not deselect HOT-in-HOT. Instead behaves as the ENTER key but moves the selection in the main HOT upwards.</li>
  143. </ul>
  144. <p>In strict mode, the <strong>allowInvalid</strong> option determines the behaviour in case of manual user input:</p>
  145. <ul>
  146. <li><code>allowInvalid: true</code> (optional) - allows manual input of value that does not exist in the
  147. <code>source</code>. In this case, the field background highlight becomes red and the selection
  148. advances to the next cell
  149. </li>
  150. <li><code>allowInvalid: false</code> - does not allow manual input of value that does not exist in the
  151. <code>source</code>. In this case, the <kbd>ENTER</kbd> key is ignored and the editor field remains opened.</li>
  152. </ul>
  153. <div id="example2"></div>
  154. <p>
  155. <button name="dump" data-dump="#example2" data-instance="hot2" title="Prints current data source to Firebug/Chrome Dev Tools">Dump
  156. data to console
  157. </button>
  158. </p>
  159. </div>
  160. </div>
  161. <div class="codeLayout">
  162. <div class="pad">
  163. <div class="jsFiddle">
  164. <button class="jsFiddleLink" data-runfiddle="example2">Edit in jsFiddle</button>
  165. </div>
  166. <script data-jsfiddle="example2">
  167. var container2 = document.getElementById('example2'),
  168. hot2;
  169. hot2 = new Handsontable(container2, {
  170. data: getCarData(),
  171. startRows: 7,
  172. startCols: 4,
  173. colHeaders: ['Car<br>(allowInvalid true)', 'Year', 'Chassis color<br>(allowInvalid false)', 'Bumper color<br>(allowInvalid true)'],
  174. columns: [
  175. {
  176. type: 'autocomplete',
  177. source: ['BMW', 'Chrysler', 'Nissan', 'Suzuki', 'Toyota', 'Volvo'],
  178. strict: true
  179. // allowInvalid: true // true is default
  180. },
  181. {},
  182. {
  183. type: 'autocomplete',
  184. source: ['yellow', 'red', 'orange', 'green', 'blue', 'gray', 'black', 'white', 'purple', 'lime', 'olive', 'cyan'],
  185. strict: true,
  186. allowInvalid: false
  187. },
  188. {
  189. type: 'autocomplete',
  190. source: ['yellow', 'red', 'orange', 'green', 'blue', 'gray', 'black', 'white', 'purple', 'lime', 'olive', 'cyan'],
  191. strict: true,
  192. allowInvalid: true //true is default
  193. }
  194. ]
  195. });
  196. </script>
  197. </div>
  198. </div>
  199. </div>
  200. <div class="rowLayout">
  201. <div class="descLayout">
  202. <div class="pad">
  203. <a name="strict-ajax"></a>
  204. <h2>Autocomplete strict mode (Ajax)</h2>
  205. <p>Autocomplete can be also used with Ajax data source. In the below example, suggestions for the "Car" column are
  206. loaded from server.</p>
  207. <p>To load data from remote (asynchronous) source, assign a function to the 'source' property. Function should
  208. perform the server side request and call the callback function when the result is available.</p>
  209. <div id="example3"></div>
  210. <p>
  211. <button name="dump" data-dump="#example3" data-instance="hot3" title="Prints current data source to Firebug/Chrome Dev Tools">Dump
  212. data to console
  213. </button>
  214. </p>
  215. </div>
  216. </div>
  217. <div class="codeLayout">
  218. <div class="pad">
  219. <div class="jsFiddle">
  220. <!--
  221. Ajax example will not work with JSFiddle at the moment because `php/cars.php` does not implement CORS.
  222. <button class="jsFiddleLink">Edit in jsFiddle</button>
  223. -->
  224. </div>
  225. <script>
  226. var container3 = document.getElementById('example3'),
  227. hot3;
  228. hot3 = new Handsontable(container3, {
  229. data: getCarData(),
  230. startRows: 7,
  231. startCols: 4,
  232. colHeaders: ['Car', 'Year', 'Chassis color', 'Bumper color'],
  233. columns: [
  234. {
  235. type: 'autocomplete',
  236. source: function (query, process) {
  237. $.ajax({
  238. //url: 'php/cars.php', // commented out because our website is hosted on static GitHub Pages
  239. url: 'json/autocomplete.json',
  240. dataType: 'json',
  241. data: {
  242. query: query
  243. },
  244. success: function (response) {
  245. console.log("response", response);
  246. //process(JSON.parse(response.data)); // JSON.parse takes string as a argument
  247. process(response.data);
  248. }
  249. });
  250. },
  251. strict: true
  252. },
  253. {}, // Year is a default text column
  254. {}, // Chassis color is a default text column
  255. {} // Bumper color is a default text column
  256. ]
  257. });
  258. </script>
  259. </div>
  260. </div>
  261. </div>
  262. <div class="footer-text"></div>
  263. </div>
  264. </div>
  265. </div>
  266. </div>
  267. <div id="outside-links-wrapper"></div>
  268. </body>
  269. </html>