handsontable_search.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Handsontable - Excel-like tables with extensive funtionality
  4. *
  5. * Demo JS code for handsontable_search.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var HotSearch = function() {
  11. //
  12. // Setup module components
  13. //
  14. // HOT search examples
  15. var _componentHotSearch = function() {
  16. if (typeof Handsontable == 'undefined') {
  17. console.warn('Warning - handsontable.min.js is not loaded.');
  18. return;
  19. }
  20. // Basic search
  21. // ------------------------------
  22. var car_data = [
  23. {car: "Mercedes", model: "GL500", year: "11/01/2015", price: 32500, share: 0.64},
  24. {car: "Chevrolet", model: "Camaro", year: "11/02/2015", price: 42400, share: 0.37},
  25. {car: "Dodge", model: "Charger", year: "11/03/2015", price: 24900, share: 0.33},
  26. {car: "Hummer", model: "H3", year: "11/04/2015", price: 54000, share: 0.15},
  27. {car: "Chevrolet", model: "Tahoe", year: "11/05/2015", price: 29300, share: 0.27},
  28. {car: "Toyota", model: "Land Cruiser", year: "11/06/2015", price: 54500, share: 0.43},
  29. {car: "Nissan", model: "GTR", year: "11/07/2015", price: 44900, share: 0.35},
  30. {car: "Porsche", model: "Cayenne", year: "11/08/2015", price: 35000, share: 0.63},
  31. {car: "Volkswagen", model: "Touareg", year: "11/09/2015", price: 41000, share: 0.15},
  32. {car: "BMW", model: "X5", year: "11/10/2015", price: 48800, share: 0.35},
  33. {car: "Audi", model: "Q7", year: "11/11/2015", price: 21000, share: 0.53},
  34. {car: "Cadillac", model: "Escalade", year: "11/12/2015", price: 63900, share: 0.38},
  35. {car: "Suzuki", model: "SX4", year: "11/13/2015", price: 23700, share: 0.8},
  36. {car: "Opel", model: "Insignia", year: "11/14/2015", price: 43900, share: 0.27}
  37. ];
  38. // Define element
  39. var hot_search_basic = document.getElementById('hot_search_basic');
  40. // Initialize with options
  41. var hot_search_basic_init = new Handsontable(hot_search_basic, {
  42. data: car_data,
  43. stretchH: 'all',
  44. colHeaders: ['Brand', 'Model', 'Date', 'Price', 'Market share'],
  45. search: true,
  46. columns: [
  47. {
  48. data: 'car'
  49. },
  50. {
  51. data: 'model'
  52. },
  53. {
  54. data: 'year',
  55. type: 'date',
  56. dateFormat: 'MM/DD/YYYY'
  57. },
  58. {
  59. data: 'price',
  60. type: 'numeric',
  61. className: 'htLeft',
  62. numericFormat: {
  63. pattern: '$0,0.00'
  64. }
  65. },
  66. {
  67. data: 'share',
  68. type: 'numeric',
  69. className: 'htLeft',
  70. numericFormat: {
  71. pattern: '0%'
  72. },
  73. width: 50
  74. }
  75. ]
  76. });
  77. // Define search field
  78. var hot_search_basic_input = document.getElementById('hot_search_basic_input');
  79. // Setup matching function
  80. function onlyExactMatch(queryStr, value) {
  81. return queryStr.toString() === value.toString();
  82. }
  83. // Add event
  84. Handsontable.dom.addEvent(hot_search_basic_input, 'keyup', function (event) {
  85. var queryResult = hot_search_basic_init.search.query(this.value);
  86. console.log(queryResult);
  87. hot_search_basic_init.render();
  88. });
  89. // Custom search result class
  90. // ------------------------------
  91. // Define element
  92. var hot_search_class = document.getElementById("hot_search_class");
  93. // Initialize with options
  94. var hot_search_class_init = new Handsontable(hot_search_class, {
  95. data: car_data,
  96. colHeaders: ['Brand', 'Model', 'Date', 'Price', 'Market share'],
  97. stretchH: 'all',
  98. search: {
  99. searchResultClass: 'bg-blue'
  100. },
  101. columns: [
  102. {
  103. data: 'car'
  104. },
  105. {
  106. data: 'model'
  107. },
  108. {
  109. data: 'year',
  110. type: 'date',
  111. dateFormat: 'MM/DD/YYYY'
  112. },
  113. {
  114. data: 'price',
  115. type: 'numeric',
  116. className: 'htLeft',
  117. numericFormat: {
  118. pattern: '$0,0.00'
  119. }
  120. },
  121. {
  122. data: 'share',
  123. type: 'numeric',
  124. className: 'htLeft',
  125. numericFormat: {
  126. pattern: '0%'
  127. },
  128. width: 50
  129. }
  130. ]
  131. });
  132. // Define search field
  133. var hot_search_class_input = document.getElementById('hot_search_class_input');
  134. // Add event
  135. Handsontable.dom.addEvent(hot_search_class_input,'keyup', function (event) {
  136. var queryResult = hot_search_class_init.search.query(this.value);
  137. console.log(queryResult);
  138. hot_search_class_init.render();
  139. });
  140. // Custom query method
  141. // ------------------------------
  142. // Define element
  143. var hot_search_query = document.getElementById("hot_search_query");
  144. // Initialize with options
  145. var hot_search_query_init = new Handsontable(hot_search_query, {
  146. data: car_data,
  147. colHeaders: ['Brand', 'Model', 'Date', 'Price', 'Market share'],
  148. stretchH: 'all',
  149. search: {
  150. queryMethod: onlyExactMatch
  151. },
  152. columns: [
  153. {
  154. data: 'car'
  155. },
  156. {
  157. data: 'model'
  158. },
  159. {
  160. data: 'year',
  161. type: 'date',
  162. dateFormat: 'MM/DD/YYYY'
  163. },
  164. {
  165. data: 'price',
  166. type: 'numeric',
  167. className: 'htLeft',
  168. numericFormat: {
  169. pattern: '$0,0.00'
  170. }
  171. },
  172. {
  173. data: 'share',
  174. type: 'numeric',
  175. className: 'htLeft',
  176. numericFormat: {
  177. pattern: '0%'
  178. },
  179. width: 50
  180. }
  181. ]
  182. });
  183. // Define search field
  184. var hot_search_query_input = document.getElementById('hot_search_query_input');
  185. // Exact match function
  186. function onlyExactMatch(queryStr, value) {
  187. return queryStr.toString() === value.toString();
  188. };
  189. // Add event
  190. Handsontable.dom.addEvent(hot_search_query_input,'keyup', function(event) {
  191. var queryResult = hot_search_query_init.search.query(this.value);
  192. console.log(queryResult);
  193. hot_search_query_init.render();
  194. });
  195. // Custom callback
  196. // ------------------------------
  197. // Define element
  198. var hot_search_callback = document.getElementById("hot_search_callback");
  199. // Initialize with options
  200. var hot_search_callback_init = new Handsontable(hot_search_callback, {
  201. data: car_data,
  202. colHeaders: ['Brand', 'Model', 'Date', 'Price', 'Market share'],
  203. stretchH: 'all',
  204. search: {
  205. callback: searchResultCounter
  206. },
  207. columns: [
  208. {
  209. data: 'car'
  210. },
  211. {
  212. data: 'model'
  213. },
  214. {
  215. data: 'year'
  216. },
  217. {
  218. data: 'price',
  219. type: 'numeric',
  220. className: 'htLeft',
  221. numericFormat: {
  222. pattern: '$0,0.00'
  223. }
  224. },
  225. {
  226. data: 'share',
  227. type: 'numeric',
  228. className: 'htLeft',
  229. numericFormat: {
  230. pattern: '0%'
  231. },
  232. width: 50
  233. }
  234. ]
  235. });
  236. // Define count element
  237. var resultCount = document.getElementById('result-count');
  238. // Search result count
  239. var searchResultCount = 0;
  240. function searchResultCounter(instance, row, col, value, result) {
  241. Handsontable.plugins.Search.DEFAULT_CALLBACK.apply(this, arguments);
  242. if (result) {
  243. searchResultCount++;
  244. }
  245. }
  246. // Define search field
  247. var hot_search_callback_input = document.getElementById('hot_search_callback_input');
  248. // Add event
  249. Handsontable.dom.addEvent(hot_search_callback_input, 'keyup', function(event) {
  250. var queryResult;
  251. searchResultCount = 0;
  252. queryResult = hot_search_callback_init.search.query(this.value);
  253. console.log(queryResult);
  254. resultCount.innerText = searchResultCount.toString();
  255. hot_search_callback_init.render();
  256. });
  257. };
  258. //
  259. // Return objects assigned to module
  260. //
  261. return {
  262. init: function() {
  263. _componentHotSearch();
  264. }
  265. }
  266. }();
  267. // Initialize module
  268. // ------------------------------
  269. document.addEventListener('DOMContentLoaded', function() {
  270. HotSearch.init();
  271. });