index.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Multi-view dgrid</title>
  6. <link rel="stylesheet" href="../../../dojo/resources/dojo.css">
  7. <link rel="stylesheet" href="../../css/skins/slate.css">
  8. <style>
  9. #grid {
  10. width: 80%;
  11. margin: auto;
  12. height: 332px; /* tall enough for 2 rows in gallery view */
  13. }
  14. /* styles for icon node in various views */
  15. .icon {
  16. font-size: 0; /*TODO: is this needed?*/
  17. background-repeat: no-repeat;
  18. }
  19. .table .icon {
  20. width: 32px;
  21. height: 32px;
  22. }
  23. .details .icon {
  24. width: 72px; /* 64px + buffer to separate icon from text */
  25. height: 64px;
  26. }
  27. .gallery .icon {
  28. width: 128px;
  29. height: 128px;
  30. margin: auto;
  31. }
  32. /* styles for "table" view (using Grid.renderRow) */
  33. .table .field-icon {
  34. width: 33px;
  35. padding: 0;
  36. }
  37. .table .field-icon .dgrid-cell-padding { /* old IE */
  38. padding: 0;
  39. }
  40. .table .dgrid-cell {
  41. vertical-align: middle;
  42. }
  43. /* Table view allows expanding/collapsing rows to show summary */
  44. .table .expando {
  45. /* style of area containing summary, expanded on click */
  46. padding-left: 40px;
  47. }
  48. .table .collapsed .expando {
  49. display: none;
  50. }
  51. /* styles for details view */
  52. .details .dgrid-row {
  53. clear: both;
  54. min-height: 64px;
  55. padding: 0.5em;
  56. }
  57. .details .icon {
  58. float: left;
  59. }
  60. .details .name {
  61. font-weight: bold;
  62. margin-bottom: 1em;
  63. }
  64. /* styles for gallery view */
  65. .gallery .dgrid-row {
  66. width: 25%;
  67. float: left;
  68. text-align: center;
  69. padding: 1em 0;
  70. }
  71. </style>
  72. </head>
  73. <body class="slate">
  74. <p>
  75. This page demonstrates presenting several different views within a single
  76. Grid instance, by swapping out the <code>renderRow</code> function.
  77. The details and gallery views also demonstrate switching the grid header
  78. off by calling <code>setShowHeader</code>.
  79. (In fact, if only the Details and Gallery views were desired,
  80. the <code>List</code> module would be sufficient.)
  81. </p>
  82. <p>
  83. In addition, the "table" view demonstrates hooking up a click callback,
  84. in order to display more information when a row is clicked.
  85. </p>
  86. <p>Switch View:
  87. <button id="btnTable">Table</button>
  88. <button id="btnDetails">Details</button>
  89. <button id="btnGallery">Gallery</button>
  90. </p>
  91. <div id="grid" class="table"></div>
  92. <script src="../../../dojo/dojo.js" data-dojo-config="async:1"></script>
  93. <script>
  94. require(["dgrid/OnDemandGrid", "dgrid/Selection", "dgrid/Keyboard", "dojo/_base/declare", "dojo/on", "dojo/query", "dojo/store/Memory", "dojo/store/Observable", "put-selector/put"],
  95. function(Grid, Selection, Keyboard, declare, on, query, Memory, Observable, put){
  96. var grid, store, origRenderRow, expandoListener, expandedNode,
  97. renderers = {
  98. gallery: function(obj, options){
  99. // function used for renderRow for gallery view (large tiled thumbnails)
  100. var div = put("div");
  101. div.innerHTML = '<div class="icon" style="background-image:url(resources/' +
  102. obj.icon + '-128.png);">&nbsp;</div><div class="name">' + obj.name + '</div>';
  103. return div;
  104. },
  105. details: function(obj, options){
  106. // function used for renderRow for details view (items w/ summary)
  107. var div = put("div");
  108. div.innerHTML = '<div class="icon" style="background-image:url(resources/' +
  109. obj.icon + '-64.png);">&nbsp;</div><div class="name">' +
  110. obj.name + '</div><div class="summary">' + obj.summary + '</div>';
  111. return div;
  112. },
  113. table: function(obj, options){
  114. var div = put("div.collapsed", Grid.prototype.renderRow.apply(this, arguments)),
  115. expando = put(div, "div.expando", obj.summary);
  116. return div;
  117. }
  118. };
  119. function viewClickHandler(view){
  120. return function(){
  121. // pause/resume click listener for expando in "table" view
  122. expandoListener[view == "table" ? "resume" : "pause"]();
  123. // reset expanded node for table view
  124. expandedNode = null;
  125. // update renderRow function
  126. grid.renderRow = renderers[view];
  127. // update class on grid domNode
  128. put(grid.domNode, "!table!gallery!details." + view);
  129. // only show headers if we're in "table" view
  130. grid.set("showHeader", view == "table");
  131. // force redraw of rows
  132. grid.refresh();
  133. }
  134. }
  135. function byId(id) { return document.getElementById(id); }
  136. store = new Memory({ data: [
  137. { id: "dojo", name: "Dojo Core", icon: "dojo",
  138. summary: "Dojo core is a powerful, lightweight library that makes common tasks quicker and easier. Animate elements, manipulate the DOM, and query with easy CSS syntax, all without sacrificing performance."
  139. }, {
  140. id: "dijit", name: "Dijit", icon: "dojo",
  141. summary: "Dijit provides a complete collection of user interface controls based on Dojo, giving you the power to create web applications that are highly optimized for usability, performance, internationalization, accessibility, but above all deliver an incredible user experience."
  142. }, {
  143. id: "dgrid", name: "dgrid", icon: "df",
  144. summary: "A lightweight, mobile-ready, data-driven, modular widget designed for lists and grids."
  145. }, {
  146. id: "xstyle", name: "xstyle", icon: "df",
  147. summary: "CSS framework providing polyfills, extensions, dynamic loading, and selector based DOM manipulation."
  148. }, {
  149. id: "put-selector", name: "put-selector", icon: "df",
  150. summary: "A high-performance, lightweight function for creating and manipulating DOM elements with succinct, elegant, familiar CSS selector-based syntax."
  151. }
  152. ] });
  153. grid = new Grid({
  154. columns: [
  155. {
  156. label: " ",
  157. field: "icon",
  158. sortable: false,
  159. formatter: function(icon){
  160. return '<div class="icon" style="background-image:url(resources/' +
  161. icon + '-32.png);">&nbsp;</div>';
  162. }
  163. },
  164. { label: "Package", field: "id" },
  165. { label: "Name", field: "name" }
  166. ],
  167. store: store,
  168. renderRow: renderers.table
  169. }, "grid");
  170. // store initially-active renderRow as renderer for table view
  171. renderers.table = grid.renderRow;
  172. // listen for clicks to trigger expand/collapse in table view mode
  173. expandoListener = on.pausable(grid.domNode, ".dgrid-row:click", function(evt){
  174. var
  175. node = grid.row(evt).element,
  176. collapsed = node.className.indexOf("collapsed") >= 0;
  177. // toggle state of node which was clicked
  178. put(node, (collapsed ? "!" : ".") + "collapsed");
  179. // if clicked row wasn't expanded, collapse any previously-expanded row
  180. collapsed && expandedNode && put(expandedNode, ".collapsed");
  181. // if the row clicked was previously expanded, nothing is expanded now
  182. expandedNode = collapsed ? node : null;
  183. });
  184. // switch views when buttons are clicked
  185. byId("btnTable").onclick = viewClickHandler("table");
  186. byId("btnDetails").onclick = viewClickHandler("details");
  187. byId("btnGallery").onclick = viewClickHandler("gallery");
  188. });
  189. </script>
  190. </body>
  191. </html>