footable.export.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * FooTable v3 - FooTable is a jQuery plugin that aims to make HTML tables on smaller devices look awesome.
  3. * @version 3.1.6
  4. * @link http://fooplugins.com
  5. * @copyright Steven Usher & Brad Vincent 2015
  6. * @license Released under the GPLv3 license.
  7. */
  8. (function($, F){
  9. F.Export = F.Component.extend(/** @lends FooTable.Export */{
  10. /**
  11. * @summary This component provides some basic export functionality.
  12. * @memberof FooTable
  13. * @constructs Export
  14. * @param {FooTable.Table} table - The current instance of the plugin.
  15. */
  16. construct: function(table){
  17. // call the constructor of the base class
  18. this._super(table, true);
  19. /**
  20. * @summary A snapshot of the working set of rows prior to being trimmed by the paging component.
  21. * @memberof FooTable.Export#
  22. * @name snapshot
  23. * @type {FooTable.Row[]}
  24. */
  25. this.snapshot = [];
  26. },
  27. /**
  28. * @summary Hooks into the predraw pipeline after sorting and filtering have taken place but prior to paging.
  29. * @memberof FooTable.Export#
  30. * @function predraw
  31. * @description This method allows us to take a snapshot of the working set of rows before they are trimmed by the paging component and is called by the plugin instance.
  32. */
  33. predraw: function(){
  34. this.snapshot = this.ft.rows.array.slice(0);
  35. },
  36. /**
  37. * @summary Return the columns as simple JavaScript objects in an array.
  38. * @memberof FooTable.Export#
  39. * @function columns
  40. * @returns {Object[]}
  41. */
  42. columns: function(){
  43. var result = [];
  44. F.arr.each(this.ft.columns.array, function(column){
  45. if (!column.internal){
  46. result.push({
  47. type: column.type,
  48. name: column.name,
  49. title: column.title,
  50. visible: column.visible,
  51. hidden: column.hidden,
  52. classes: column.classes,
  53. style: column.style
  54. });
  55. }
  56. });
  57. return result;
  58. },
  59. /**
  60. * @summary Return the rows as simple JavaScript objects in an array.
  61. * @memberof FooTable.Export#
  62. * @function rows
  63. * @param {boolean} [filtered=false] - Whether or not to exclude filtered rows from the result.
  64. * @returns {Object[]}
  65. */
  66. rows: function(filtered){
  67. filtered = F.is.boolean(filtered) ? filtered : false;
  68. var rows = filtered ? this.ft.rows.all : this.snapshot, result = [];
  69. F.arr.each(rows, function(row){
  70. result.push(row.val());
  71. });
  72. return result;
  73. },
  74. /**
  75. * @summary Return the columns and rows as a properly formatted JSON object.
  76. * @memberof FooTable.Export#
  77. * @function json
  78. * @param {boolean} [filtered=false] - Whether or not to exclude filtered rows from the result.
  79. * @returns {Object}
  80. */
  81. json: function(filtered){
  82. return JSON.parse(JSON.stringify({columns: this.columns(),rows: this.rows(filtered)}));
  83. },
  84. /**
  85. * @summary Return the columns and rows as a properly formatted CSV value.
  86. * @memberof FooTable.Export#
  87. * @function csv
  88. * @param {boolean} [filtered=false] - Whether or not to exclude filtered rows from the result.
  89. * @returns {string}
  90. */
  91. csv: function(filtered){
  92. var csv = "", columns = this.columns(), value, escaped;
  93. F.arr.each(columns, function(column, i){
  94. escaped = '"' + column.title.replace(/"/g, '""') + '"';
  95. csv += (i === 0 ? escaped : "," + escaped);
  96. });
  97. csv += "\n";
  98. var rows = filtered ? this.ft.rows.all : this.snapshot;
  99. F.arr.each(rows, function(row){
  100. F.arr.each(row.cells, function(cell, i){
  101. if (!cell.column.internal){
  102. value = cell.column.stringify.call(cell.column, cell.value, cell.ft.o, cell.row.value);
  103. escaped = '"' + value.replace(/"/g, '""') + '"';
  104. csv += (i === 0 ? escaped : "," + escaped);
  105. }
  106. });
  107. csv += "\n";
  108. });
  109. return csv;
  110. }
  111. });
  112. // register the component using a priority of 490 which falls just after filtering (500) and before paging (400).
  113. F.components.register("export", F.Export, 490);
  114. })(jQuery, FooTable);
  115. (function(F){
  116. // this is used to define the filtering specific properties on column creation
  117. F.Column.prototype.__export_define__ = function(definition){
  118. this.stringify = F.checkFnValue(this, definition.stringify, this.stringify);
  119. };
  120. // overrides the public define method and replaces it with our own
  121. F.Column.extend('define', function(definition){
  122. this._super(definition); // call the base so we don't have to redefine any previously set properties
  123. this.__export_define__(definition); // then call our own
  124. });
  125. /**
  126. * @summary Return the supplied value as a string.
  127. * @memberof FooTable.Column#
  128. * @function stringify
  129. * @returns {string}
  130. */
  131. F.Column.prototype.stringify = function(value, options, rowData){
  132. return value + "";
  133. };
  134. if (F.is.defined(F.DateColumn)){
  135. // override the base method for DateColumns
  136. F.DateColumn.prototype.stringify = function(value, options, rowData){
  137. return F.is.object(value) && F.is.boolean(value._isAMomentObject) && value.isValid() ? value.format(this.formatString) : '';
  138. };
  139. }
  140. // override the base method for ObjectColumns
  141. F.ObjectColumn.prototype.stringify = function(value, options, rowData){
  142. return F.is.object(value) ? JSON.stringify(value) : "";
  143. };
  144. // override the base method for ArrayColumns
  145. F.ArrayColumn.prototype.stringify = function(value, options, rowData){
  146. return F.is.array(value) ? JSON.stringify(value) : "";
  147. };
  148. })(FooTable);
  149. (function(F){
  150. /**
  151. * @summary Return the columns and rows as a properly formatted JSON object.
  152. * @memberof FooTable.Table#
  153. * @function toJSON
  154. * @param {boolean} [filtered=false] - Whether or not to exclude filtered rows from the result.
  155. * @returns {Object}
  156. */
  157. F.Table.prototype.toJSON = function(filtered){
  158. return this.use(F.Export).json(filtered);
  159. };
  160. /**
  161. * @summary Return the columns and rows as a properly formatted CSV value.
  162. * @memberof FooTable.Table#
  163. * @function toCSV
  164. * @param {boolean} [filtered=false] - Whether or not to exclude filtered rows from the result.
  165. * @returns {string}
  166. */
  167. F.Table.prototype.toCSV = function(filtered){
  168. return this.use(F.Export).csv(filtered);
  169. };
  170. })(FooTable);