conditional.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset='utf-8'>
  5. <title>Conditional formatting - 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. <link rel="stylesheet" media="screen" href="js/highlight/styles/github.css">
  24. <link rel="stylesheet" href="css/font-awesome/css/font-awesome.min.css">
  25. <!--
  26. Facebook open graph. Don't copy this to your project :)
  27. -->
  28. <meta property="og:title" content="Conditional formatting - Handsontable">
  29. <meta property="og:description"
  30. content="This demo shows how to use the cell type renderer feature to make some conditional formatting">
  31. <meta property="og:url" content="http://handsontable.com/demo/conditional.html">
  32. <meta property="og:image" content="http://handsontable.com/demo/image/og-image.png">
  33. <meta property="og:image:type" content="image/png">
  34. <meta property="og:image:width" content="409">
  35. <meta property="og:image:height" content="164">
  36. <link rel="canonical" href="http://handsontable.com/demo/conditional.html">
  37. <!--
  38. Google Analytics for GitHub Page. Don't copy this to your project :)
  39. -->
  40. <script src="js/ga.js"></script>
  41. </head>
  42. <body>
  43. <div class="wrapper">
  44. <div class="wrapper-row">
  45. <div id="global-menu-clone">
  46. <h1><a href="../index.html">Handsontable</a></h1>
  47. </div>
  48. <div id="container">
  49. <div class="columnLayout">
  50. <div class="rowLayout">
  51. <div class="descLayout">
  52. <div class="pad" data-jsfiddle="example1">
  53. <h2>Conditional formatting</h2>
  54. <p>This demo shows how to use the cell type renderer feature to make some conditional formatting:</p>
  55. <ol>
  56. <li>first row is <span style="font-weight: bold">read-only</span>, and formatted in <span
  57. style="color: green; font-weight: bold">green bold</span> text
  58. </li>
  59. <li>all cells in the Nissan column are written in <span style="font-style: italic">italic</span></li>
  60. <li>empty cells have <span style="background: silver">silver</span> background</li>
  61. <li>negative numbers are written in <span style="color: red">red</span></li>
  62. </ol>
  63. <div id="example1"></div>
  64. <style>
  65. .negative {
  66. color: red;
  67. }
  68. </style>
  69. <p>
  70. <button name="dump" data-dump="#example1" data-instance="hot1" title="Prints current data source to Firebug/Chrome Dev Tools">
  71. Dump data to console
  72. </button>
  73. </p>
  74. </div>
  75. </div>
  76. <div class="codeLayout">
  77. <div class="pad">
  78. <div class="jsFiddle">
  79. <button class="jsFiddleLink" data-runfiddle="example1">Edit in jsFiddle</button>
  80. </div>
  81. <script data-jsfiddle="example1">
  82. var data = [
  83. ['', 'Kia', 'Nissan', 'Toyota', 'Honda'],
  84. ['2008', -5, '', 12, 13],
  85. ['2009', '', -11, 14, 13],
  86. ['2010', '', 15, -12, 'readOnly']
  87. ],
  88. container,
  89. hot1;
  90. function firstRowRenderer(instance, td, row, col, prop, value, cellProperties) {
  91. Handsontable.renderers.TextRenderer.apply(this, arguments);
  92. td.style.fontWeight = 'bold';
  93. td.style.color = 'green';
  94. td.style.background = '#CEC';
  95. }
  96. function negativeValueRenderer(instance, td, row, col, prop, value, cellProperties) {
  97. Handsontable.renderers.TextRenderer.apply(this, arguments);
  98. // if row contains negative number
  99. if (parseInt(value, 10) < 0) {
  100. // add class "negative"
  101. td.className = 'negative';
  102. }
  103. if (!value || value === '') {
  104. td.style.background = '#EEE';
  105. }
  106. else {
  107. if (value === 'Nissan') {
  108. td.style.fontStyle = 'italic';
  109. }
  110. td.style.background = '';
  111. }
  112. }
  113. // maps function to lookup string
  114. Handsontable.renderers.registerRenderer('negativeValueRenderer', negativeValueRenderer);
  115. container = document.getElementById('example1');
  116. hot1 = new Handsontable(container, {
  117. data: data,
  118. startRows: 5,
  119. startCols: 5,
  120. minSpareRows: 1,
  121. contextMenu: true,
  122. onSelection: function (row, col, row2, col2) {
  123. var meta = this.getCellMeta(row2, col2);
  124. if (meta.readOnly) {
  125. this.updateSettings({fillHandle: false});
  126. }
  127. else {
  128. this.updateSettings({fillHandle: true});
  129. }
  130. },
  131. cells: function (row, col, prop) {
  132. var cellProperties = {};
  133. if (row === 0 || this.instance.getData()[row][col] === 'readOnly') {
  134. cellProperties.readOnly = true; // make cell read-only if it is first row or the text reads 'readOnly'
  135. }
  136. if (row === 0) {
  137. cellProperties.renderer = firstRowRenderer; // uses function directly
  138. }
  139. else {
  140. cellProperties.renderer = "negativeValueRenderer"; // uses lookup map
  141. }
  142. return cellProperties;
  143. }
  144. });
  145. </script>
  146. </div>
  147. </div>
  148. </div>
  149. <div class="footer-text">
  150. </div>
  151. </div>
  152. </div>
  153. </div>
  154. </div>
  155. <div id="outside-links-wrapper"></div>
  156. </body>
  157. </html>