TableWriter.js 5.0 KB

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