TableWriter.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.grid.enhanced.plugins.exporter.TableWriter"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.grid.enhanced.plugins.exporter.TableWriter"] = true;
  8. dojo.provide("dojox.grid.enhanced.plugins.exporter.TableWriter");
  9. dojo.require("dojox.grid.enhanced.plugins.exporter._ExportWriter");
  10. dojox.grid.enhanced.plugins.Exporter.registerWriter("table",
  11. "dojox.grid.enhanced.plugins.exporter.TableWriter");
  12. dojo.declare("dojox.grid.enhanced.plugins.exporter.TableWriter",
  13. dojox.grid.enhanced.plugins.exporter._ExportWriter, {
  14. // summary:
  15. // Export grid to HTML table format. Primarily used by Printer plugin.
  16. constructor: function(/* object? */writerArgs){
  17. // summary:
  18. // The generated table only defines the col/rowspan, height and width of
  19. // all the cells in the style attribute, no other attributes
  20. // (like border, cellspacing, etc.) are used.
  21. // Users can define these attributes in the writerArgs object, like:
  22. // {table:"border='border'",thead:"cellspacing='3'"}
  23. this._viewTables = [];
  24. this._tableAttrs = writerArgs || {};
  25. },
  26. _getTableAttrs: function(/* string */tagName){
  27. // summary:
  28. // Get html attribute string for the given kind of tag.
  29. // tags:
  30. // private
  31. // tagName: string
  32. // An html tag name
  33. // returns:
  34. // The well formatted attributes for the given html table.tag
  35. var attrs = this._tableAttrs[tagName] || '';
  36. //To ensure the attribute list starts with a space
  37. if(attrs && attrs[0] != ' '){
  38. attrs = ' ' + attrs;
  39. }
  40. return attrs; //String
  41. },
  42. _getRowClass: function(/* object */arg_obj){
  43. // summary:
  44. // Get CSS class string for a row
  45. // tags:
  46. // private
  47. return arg_obj.isHeader ? " grid_header" : [//String
  48. " grid_row grid_row_",
  49. arg_obj.rowIdx + 1,
  50. arg_obj.rowIdx % 2 ? " grid_even_row" : " grid_odd_row"
  51. ].join('');
  52. },
  53. _getColumnClass: function(/* object */arg_obj){
  54. // summary:
  55. // Get CSS class string for a column
  56. // tags:
  57. // private
  58. var col_idx = arg_obj.cell.index + arg_obj.colOffset + 1;
  59. return [" grid_column grid_column_", col_idx,//String
  60. col_idx % 2 ? " grid_odd_column" : " grid_even_column"].join('');
  61. },
  62. beforeView: function(/* object */arg_obj){
  63. // summary:
  64. // Overrided from _ExportWriter
  65. var viewIdx = arg_obj.viewIdx,
  66. table = this._viewTables[viewIdx],
  67. height, width = dojo.marginBox(arg_obj.view.contentNode).w;
  68. if(!table){
  69. var left = 0;
  70. for(var i = 0; i < viewIdx; ++i){
  71. left += this._viewTables[i]._width;
  72. }
  73. table = this._viewTables[viewIdx] = ['<div class="grid_view" style="position: absolute; top: 0; ',
  74. dojo._isBodyLtr() ? 'left' : 'right', ':', left,
  75. 'px;">'];
  76. }
  77. table._width = width;
  78. if(arg_obj.isHeader){
  79. height = dojo.contentBox(arg_obj.view.headerContentNode).h;
  80. }else{
  81. var rowNode = arg_obj.grid.getRowNode(arg_obj.rowIdx);
  82. if(rowNode){
  83. height = dojo.contentBox(rowNode).h;
  84. }else{
  85. //This row has not been loaded from store, so we should estimate it's height.
  86. height = arg_obj.grid.scroller.averageRowHeight;
  87. }
  88. }
  89. table.push('<table class="', this._getRowClass(arg_obj),
  90. '" style="table-layout:fixed; height:', height, 'px; width:', width, 'px;" ',
  91. 'border="0" cellspacing="0" cellpadding="0" ',
  92. this._getTableAttrs("table"),
  93. '><tbody ', this._getTableAttrs('tbody'), '>');
  94. return true; //Boolean
  95. },
  96. afterView: function(/* object */arg_obj){
  97. // summary:
  98. // Overrided from _ExportWriter
  99. this._viewTables[arg_obj.viewIdx].push('</tbody></table>');
  100. },
  101. beforeSubrow: function(/* object */arg_obj){
  102. // summary:
  103. // Overrided from _ExportWriter
  104. this._viewTables[arg_obj.viewIdx].push('<tr', this._getTableAttrs('tr'), '>');
  105. return true; //Boolean
  106. },
  107. afterSubrow: function(/* object */arg_obj){
  108. // summary:
  109. // Overrided from _ExportWriter
  110. this._viewTables[arg_obj.viewIdx].push('</tr>');
  111. },
  112. handleCell: function(/* object */arg_obj){
  113. // summary:
  114. // Overrided from _ExportWriter
  115. var cell = arg_obj.cell;
  116. if(cell.hidden || dojo.indexOf(arg_obj.spCols, cell.index) >= 0){
  117. //We are not interested in indirect selectors and row indexes.
  118. return;
  119. }
  120. var cellTagName = arg_obj.isHeader ? 'th' : 'td',
  121. attrs = [cell.colSpan ? ' colspan="' + cell.colSpan + '"' : '',
  122. cell.rowSpan ? ' rowspan="' + cell.rowSpan + '"' : '',
  123. ' style="width: ', dojo.contentBox(cell.getHeaderNode()).w, 'px;"',
  124. this._getTableAttrs(cellTagName),
  125. ' class="', this._getColumnClass(arg_obj), '"'].join(''),
  126. table = this._viewTables[arg_obj.viewIdx];
  127. table.push('<', cellTagName, attrs, '>');
  128. if(arg_obj.isHeader){
  129. table.push(cell.name || cell.field);
  130. } else{
  131. table.push(this._getExportDataForCell(arg_obj.rowIdx, arg_obj.row, cell, arg_obj.grid));
  132. }
  133. table.push('</', cellTagName, '>');
  134. },
  135. afterContent: function(){
  136. // summary:
  137. // Overrided from _ExportWriter
  138. dojo.forEach(this._viewTables, function(table){
  139. table.push('</div>');
  140. });
  141. },
  142. toString: function(){
  143. // summary:
  144. // Overrided from _ExportWriter
  145. var viewsHTML = dojo.map(this._viewTables, function(table){ //String
  146. return table.join('');
  147. }).join('');
  148. return ['<div style="position: relative;">', viewsHTML, '</div>'].join('');
  149. }
  150. });
  151. }