renderers_html.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset='utf-8'>
  5. <title>Custom HTML in cells and headers - Handsontable</title>
  6. <!--
  7. Loading Handsontable (full distribution that includes all dependencies)
  8. -->
  9. <link data-jsfiddle="common" rel="stylesheet" media="screen" href="../dist/handsontable.css">
  10. <link data-jsfiddle="common" rel="stylesheet" media="screen" href="../dist/pikaday/pikaday.css">
  11. <script data-jsfiddle="common" src="../dist/pikaday/pikaday.js"></script>
  12. <script data-jsfiddle="common" src="../dist/moment/moment.js"></script>
  13. <script data-jsfiddle="common" src="../dist/zeroclipboard/ZeroClipboard.js"></script>
  14. <script data-jsfiddle="common" src="../dist/numbro/numbro.js"></script>
  15. <script data-jsfiddle="common" src="../dist/numbro/languages.js"></script>
  16. <script data-jsfiddle="common" src="../dist/handsontable.js"></script>
  17. <!--
  18. Loading demo dependencies. They are used here only to enhance the examples on this page
  19. -->
  20. <link data-jsfiddle="common" rel="stylesheet" media="screen" href="css/samples.css?20140331">
  21. <script src="js/samples.js"></script>
  22. <script src="js/highlight/highlight.pack.js"></script>
  23. <script src="js/moment/moment.js"></script>
  24. <script src="js/pikaday/pikaday.js"></script>
  25. <link rel="stylesheet" media="screen" href="js/highlight/styles/github.css">
  26. <link rel="stylesheet" href="css/font-awesome/css/font-awesome.min.css">
  27. <link rel="stylesheet" href="js/pikaday/css/pikaday.css">
  28. <!--
  29. Facebook open graph. Don't copy this to your project :)
  30. -->
  31. <meta property="og:title" content="Custom HTML in cells and headers">
  32. <meta property="og:description"
  33. content="This example shows how to use custom cell renderers to display HTML content in a cell and header.">
  34. <meta property="og:url" content="http://handsontable.com/demo/reference_html.html">
  35. <meta property="og:image" content="http://handsontable.com/demo/image/og-image.png">
  36. <meta property="og:image:type" content="image/png">
  37. <meta property="og:image:width" content="409">
  38. <meta property="og:image:height" content="164">
  39. <link rel="canonical" href="http://handsontable.com/demo/reference_html.html">
  40. <!--
  41. Google Analytics for GitHub Page. Don't copy this to your project :)
  42. -->
  43. <script src="js/ga.js"></script>
  44. </head>
  45. <body>
  46. <div class="wrapper">
  47. <div class="wrapper-row">
  48. <div id="global-menu-clone">
  49. <h1><a href="../index.html">Handsontable</a></h1>
  50. </div>
  51. <div id="container">
  52. <div class="columnLayout">
  53. <div class="rowLayout">
  54. <div class="descLayout">
  55. <div class="pad">
  56. <h2>Custom HTML in cells and headers</h2>
  57. <p>On this page:</p>
  58. <ul>
  59. <li><a href="#cell">Rendering custom HTML in cells</a></li>
  60. <li><a href="#header">Rendering custom HTML in header</a></li>
  61. <li><a href="#dropdown">Changing cell type from a dropdown menu in cell header</a></li>
  62. </ul>
  63. </div>
  64. </div>
  65. </div>
  66. <div class="rowLayout">
  67. <div class="descLayout">
  68. <div class="pad" data-jsfiddle="example1">
  69. <a name="cell"></a>
  70. <h2>Rendering custom HTML in cells</h2>
  71. <p>This example shows how to use custom cell renderers to display HTML content in a cell.</p>
  72. <p>This is a very powerful feature. Just remember to escape any HTML code that could be used for XSS
  73. attacks.</p>
  74. <p>In the below configuration:</p>
  75. <ul>
  76. <li><strong>Title</strong> column uses built-in HTML renderer that allows any HTML. This is unsafe if your code comes from untrusted source. Take notice that a Handsontable user can use it to enter <code>&lt;script&gt;</code> or other potentially malicious tags using the cell editor!</li>
  77. <li><strong>Description</strong> column also uses HTML renderer (same as above)</li>
  78. <li><strong>Comments</strong> column uses a custom renderer (<code>safeHtmlRenderer</code>). This should be safe for user input, because only certain tags are allowed</li>
  79. <li><strong>Cover</strong> column accepts image URL as a string and converts it to a <code>&lt;img&gt;</code> in the renderer</li>
  80. </ul>
  81. <div id="example1"></div>
  82. <p>
  83. <button name="dump" data-dump="#example1" data-instance="hot1" title="Prints current data source to Firebug/Chrome Dev Tools">
  84. Dump data to console
  85. </button>
  86. </p>
  87. </div>
  88. </div>
  89. <div class="codeLayout">
  90. <div class="pad">
  91. <div class="jsFiddle">
  92. <button class="jsFiddleLink" data-runfiddle="example1">Edit in jsFiddle</button>
  93. </div>
  94. <script data-jsfiddle="example1">
  95. var data = [
  96. {
  97. title: "<a href='http://www.amazon.com/Professional-JavaScript-Developers-Nicholas-Zakas/dp/1118026691'>Professional JavaScript for Web Developers</a>",
  98. description: "This <a href='http://bit.ly/sM1bDf'>book</a> provides a developer-level introduction along with more advanced and useful features of <b>JavaScript</b>.",
  99. comments: "I would rate it &#x2605;&#x2605;&#x2605;&#x2605;&#x2606;",
  100. cover: "http://ecx.images-amazon.com/images/I/51bRhyVTVGL._SL50_.jpg"
  101. },
  102. {
  103. title: "<a href='http://shop.oreilly.com/product/9780596517748.do'>JavaScript: The Good Parts</a>",
  104. description: "This book provides a developer-level introduction along with <b>more advanced</b> and useful features of JavaScript.",
  105. comments: "This is <big>the</big> book about JavaScript",
  106. cover: "http://ecx.images-amazon.com/images/I/51gdVAEfPUL._SL50_.jpg"
  107. },
  108. {
  109. title: "<a href='http://shop.oreilly.com/product/9780596805531.do'>JavaScript: The Definitive Guide</a>",
  110. description: "<em>JavaScript: The Definitive Guide</em> provides a thorough description of the core <b>JavaScript</b> language and both the legacy and standard DOMs implemented in web browsers.",
  111. comments: "I've never actually read it, but the <a href='http://shop.oreilly.com/product/9780596805531.do'>comments</a> are highly <strong>positive</strong>.",
  112. cover: "http://ecx.images-amazon.com/images/I/51VFNL4T7kL._SL50_.jpg"
  113. }
  114. ],
  115. container1,
  116. hot1;
  117. container1 = document.getElementById('example1');
  118. hot1 = new Handsontable(container1, {
  119. data: data,
  120. colWidths: [200, 200, 200, 60],
  121. colHeaders: ["Title", "Description", "Comments", "Cover"],
  122. columns: [
  123. {data: "title", renderer: "html"},
  124. {data: "description", renderer: "html"},
  125. {data: "comments", renderer: safeHtmlRenderer},
  126. {data: "cover", renderer: coverRenderer}
  127. ]
  128. });
  129. // original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  130. function strip_tags(input, allowed) {
  131. var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
  132. commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
  133. // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
  134. allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('');
  135. return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
  136. return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
  137. });
  138. }
  139. function safeHtmlRenderer(instance, td, row, col, prop, value, cellProperties) {
  140. var escaped = Handsontable.helper.stringify(value);
  141. escaped = strip_tags(escaped, '<em><b><strong><a><big>'); //be sure you only allow certain HTML tags to avoid XSS threats (you should also remove unwanted HTML attributes)
  142. td.innerHTML = escaped;
  143. return td;
  144. }
  145. function coverRenderer (instance, td, row, col, prop, value, cellProperties) {
  146. var escaped = Handsontable.helper.stringify(value),
  147. img;
  148. if (escaped.indexOf('http') === 0) {
  149. img = document.createElement('IMG');
  150. img.src = value;
  151. Handsontable.dom.addEvent(img, 'mousedown', function (e){
  152. e.preventDefault(); // prevent selection quirk
  153. });
  154. Handsontable.dom.empty(td);
  155. td.appendChild(img);
  156. }
  157. else {
  158. // render as text
  159. Handsontable.renderers.TextRenderer.apply(this, arguments);
  160. }
  161. return td;
  162. }
  163. </script>
  164. </div>
  165. </div>
  166. </div>
  167. <div class="rowLayout">
  168. <div class="descLayout">
  169. <div class="pad" data-jsfiddle="example2">
  170. <a name="header"></a>
  171. <h2>Rendering custom HTML in header</h2>
  172. <p>You can also put HTML into row and column headers.</p>
  173. <p>If you need to attach events to DOM elements like the checkbox below, just remember to identify the element
  174. by class name, not by id. This is because row and column headers are duplicated in the DOM tree and id
  175. attribute must be unique.</p>
  176. <div id="example2"></div>
  177. <p>
  178. <button name="dump" data-dump="#example2" data-instance="hot2" title="Prints current data source to Firebug/Chrome Dev Tools">
  179. Dump data to console
  180. </button>
  181. </p>
  182. </div>
  183. </div>
  184. <div class="codeLayout">
  185. <div class="pad">
  186. <div class="jsFiddle">
  187. <button class="jsFiddleLink" data-runfiddle="example2">Edit in jsFiddle</button>
  188. </div>
  189. <script data-jsfiddle="example2">
  190. var
  191. isChecked,
  192. container2 = document.getElementById('example2'),
  193. hot2;
  194. hot2 = new Handsontable(container2, {
  195. startCols: 2,
  196. columns: [
  197. {},
  198. {renderer: customRenderer}
  199. ],
  200. colHeaders: function (col) {
  201. var txt;
  202. switch (col) {
  203. case 0:
  204. return '"<b>Bold</b> and <em>Beautiful</em>';
  205. case 1:
  206. txt = "Some <input type='checkbox' class='checker' ";
  207. txt += isChecked ? 'checked="checked"' : '';
  208. txt += "> checkbox";
  209. return txt;
  210. }
  211. }
  212. });
  213. function customRenderer(instance, td) {
  214. Handsontable.renderers.TextRenderer.apply(this, arguments);
  215. if (isChecked) {
  216. td.style.backgroundColor = 'yellow';
  217. }
  218. else {
  219. td.style.backgroundColor = 'white';
  220. }
  221. return td;
  222. }
  223. Handsontable.dom.addEvent(container, 'mousedown', function (event) {
  224. if (event.target.nodeName == 'INPUT' && event.target.className == 'checker') {
  225. event.stopPropagation();
  226. }
  227. });
  228. Handsontable.dom.addEvent(container, 'mouseup', function (event) {
  229. if (event.target.nodeName == 'INPUT' && event.target.className == 'checker') {
  230. isChecked = !event.target.checked;
  231. hot2.render();
  232. }
  233. });
  234. </script>
  235. </div>
  236. </div>
  237. </div>
  238. <div class="rowLayout">
  239. <div class="descLayout">
  240. <div class="pad" data-jsfiddle="example2">
  241. <a name="dropdown"></a>
  242. <h2>Changing cell type from a dropdown menu in cell header</h2>
  243. <p>This example makes use of a plugin hook to add a custom dropdown menu to the cell header</p>
  244. <div id="example3"></div>
  245. <p>
  246. <button name="dump" data-dump="#example3" data-instance="hot3" title="Prints current data source to Firebug/Chrome Dev Tools">
  247. Dump data to console
  248. </button>
  249. </p>
  250. </div>
  251. </div>
  252. <div class="codeLayout">
  253. <div class="pad">
  254. <div class="jsFiddle">
  255. <button class="jsFiddleLink" data-runfiddle="example2">Edit in jsFiddle</button>
  256. </div>
  257. <style data-jsfiddle="example3">
  258. .changeType {
  259. border: 1px solid #bbb;
  260. color: #bbb;
  261. background: #eee;
  262. border-radius: 2px;
  263. padding: 2px;
  264. font-size: 9px;
  265. float: right;
  266. line-height: 9px;
  267. margin: 3px 3px 0 0;
  268. }
  269. .changeType:hover {
  270. border: 1px solid #777;
  271. color: #777;
  272. cursor: pointer;
  273. }
  274. .changeType.pressed {
  275. background-color: #999;
  276. }
  277. .changeTypeMenu {
  278. position: absolute;
  279. border: 1px solid #ccc;
  280. margin-top: 18px;
  281. box-shadow: 0 1px 3px -1px #323232;
  282. background: white;
  283. padding: 0;
  284. display: none;
  285. z-index: 10;
  286. }
  287. .changeTypeMenu li {
  288. text-align: left;
  289. list-style: none;
  290. padding: 2px 20px;
  291. cursor: pointer;
  292. margin-bottom: 0;
  293. }
  294. .changeTypeMenu li.active:before {
  295. content: "\2714";
  296. margin-left: -15px;
  297. margin-right: 3px;
  298. }
  299. .changeTypeMenu li:hover {
  300. background: #eee;
  301. }
  302. </style>
  303. <script data-jsfiddle="example3">
  304. var
  305. data3 = [
  306. ['', 'Maserati', 'Mazda', 'Mercedes', 'Mini', 'Mitsubishi'],
  307. ['2009', 0, 2941, 4303, 354, 5814],
  308. ['2010', 3, 2905, 2867, 412, 5284],
  309. ['2011', 4, 2517, 4822, 552, 6127],
  310. ['2012', 2, 2422, 5399, 776, 4151]
  311. ],
  312. columns = [
  313. {type: 'numeric'},
  314. {type: 'numeric'},
  315. {type: 'numeric'},
  316. {type: 'numeric'},
  317. {type: 'numeric'},
  318. {type: 'numeric'}
  319. ],
  320. container = document.getElementById('example3'),
  321. hot3;
  322. hot3 = new Handsontable(container,{
  323. data: data3,
  324. colHeaders: true,
  325. minSpareRows: 1,
  326. type: 'numeric',
  327. columns: columns,
  328. afterGetColHeader: function (col, TH) {
  329. var instance = this,
  330. menu = buildMenu(columns[col].type),
  331. button = buildButton();
  332. addButtonMenuEvent(button, menu);
  333. Handsontable.dom.addEvent(menu, 'click', function (event) {
  334. if (event.target.nodeName == 'LI') {
  335. setColumnType(col, event.target.data['colType'], instance);
  336. }
  337. });
  338. TH.firstChild.appendChild(button);
  339. TH.style['white-space'] = 'normal';
  340. },
  341. cells: function (row, col, prop) {
  342. var cellProperties;
  343. if (row === 0) {
  344. cellProperties = {
  345. type: 'text' // force text type for first row
  346. };
  347. return cellProperties;
  348. }
  349. }
  350. });
  351. function addButtonMenuEvent(button, menu) {
  352. Handsontable.dom.addEvent(button, 'click', function (event) {
  353. var changeTypeMenu, position, removeMenu;
  354. document.body.appendChild(menu);
  355. event.preventDefault();
  356. Handsontable.dom.stopImmediatePropagation(event);
  357. changeTypeMenu = document.querySelectorAll('.changeTypeMenu');
  358. for (var i = 0, len = changeTypeMenu.length; i < len; i++) {
  359. changeTypeMenu[i].style.display = 'none';
  360. }
  361. menu.style.display = 'block';
  362. position = button.getBoundingClientRect();
  363. menu.style.top = (position.top + (window.scrollY || window.pageYOffset)) + 2 + 'px';
  364. menu.style.left = (position.left) + 'px';
  365. removeMenu = function (event) {
  366. if (event.target.nodeName == 'LI' && event.target.parentNode.className.indexOf('changeTypeMenu') !== -1) {
  367. if (menu.parentNode) {
  368. menu.parentNode.removeChild(menu);
  369. }
  370. }
  371. };
  372. Handsontable.dom.removeEvent(document, 'click', removeMenu);
  373. Handsontable.dom.addEvent(document, 'click', removeMenu);
  374. });
  375. }
  376. function buildMenu(activeCellType){
  377. var
  378. menu = document.createElement('UL'),
  379. types = ['text', 'numeric', 'date'],
  380. item;
  381. menu.className = 'changeTypeMenu';
  382. for (var i = 0, len = types.length; i< len; i++) {
  383. item = document.createElement('LI');
  384. if('innerText' in item) {
  385. item.innerText = types[i];
  386. } else {
  387. item.textContent = types[i];
  388. }
  389. item.data = {'colType': types[i]};
  390. if (activeCellType == types[i]) {
  391. item.className = 'active';
  392. }
  393. menu.appendChild(item);
  394. }
  395. return menu;
  396. }
  397. function buildButton() {
  398. var button = document.createElement('BUTTON');
  399. button.innerHTML = '\u25BC';
  400. button.className = 'changeType';
  401. return button;
  402. }
  403. function setColumnType(i, type, instance) {
  404. columns[i].type = type;
  405. instance.updateSettings({columns: columns});
  406. instance.validateCells(function() {
  407. instance.render();
  408. });
  409. }
  410. </script>
  411. </div>
  412. </div>
  413. </div>
  414. <div class="footer-text">
  415. </div>
  416. </div>
  417. </div>
  418. </div>
  419. </div>
  420. <div id="outside-links-wrapper"></div>
  421. </body>
  422. </html>