tableExport.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. (function ($) {
  2. $.fn.extend({
  3. tableExport: function (options) {
  4. var defaults = {
  5. separator: ',',
  6. ignoreColumn: [],
  7. tableName: 'yourTableName',
  8. type: 'csv',
  9. pdfFontSize: 14,
  10. pdfLeftMargin: 20,
  11. escape: 'true',
  12. htmlContent: 'false',
  13. consoleLog: 'false'
  14. };
  15. var options = $.extend(defaults, options);
  16. var el = this;
  17. if (defaults.type == 'csv' || defaults.type == 'txt') {
  18. // Header
  19. var tdData = "";
  20. $(el).find('thead').find('tr').each(function () {
  21. tdData += "\n";
  22. $(this).filter(':visible').find('th').each(function (index, data) {
  23. if ($(this).css('display') != 'none') {
  24. if (defaults.ignoreColumn.indexOf(index) == -1) {
  25. tdData += '"' + parseString($(this)) + '"' + defaults.separator;
  26. }
  27. }
  28. });
  29. tdData = $.trim(tdData);
  30. tdData = $.trim(tdData).substring(0, tdData.length - 1);
  31. });
  32. // Row vs Column
  33. $(el).find('tbody').find('tr').each(function () {
  34. tdData += "\n";
  35. $(this).filter(':visible').find('td').each(function (index, data) {
  36. if ($(this).css('display') != 'none') {
  37. if (defaults.ignoreColumn.indexOf(index) == -1) {
  38. tdData += '"' + parseString($(this)) + '"' + defaults.separator;
  39. }
  40. }
  41. });
  42. //tdData = $.trim(tdData);
  43. tdData = $.trim(tdData).substring(0, tdData.length - 1);
  44. });
  45. //output
  46. if (defaults.consoleLog == 'true') {
  47. console.log(tdData);
  48. }
  49. var base64data = "base64," + $.base64.encode(tdData);
  50. window.open('data:application/' + defaults.type + ';filename=exportData;' + base64data,'filename.csv');
  51. } else if (defaults.type == 'sql') {
  52. // Header
  53. var tdData = "INSERT INTO `" + defaults.tableName + "` (";
  54. $(el).find('thead').find('tr').each(function () {
  55. $(this).filter(':visible').find('th').each(function (index, data) {
  56. if ($(this).css('display') != 'none') {
  57. if (defaults.ignoreColumn.indexOf(index) == -1) {
  58. tdData += '`' + parseString($(this)) + '`,';
  59. }
  60. }
  61. });
  62. tdData = $.trim(tdData);
  63. tdData = $.trim(tdData).substring(0, tdData.length - 1);
  64. });
  65. tdData += ") VALUES ";
  66. // Row vs Column
  67. $(el).find('tbody').find('tr').each(function () {
  68. tdData += "(";
  69. $(this).filter(':visible').find('td').each(function (index, data) {
  70. if ($(this).css('display') != 'none') {
  71. if (defaults.ignoreColumn.indexOf(index) == -1) {
  72. tdData += '"' + parseString($(this)) + '",';
  73. }
  74. }
  75. });
  76. tdData = $.trim(tdData).substring(0, tdData.length - 1);
  77. tdData += "),";
  78. });
  79. tdData = $.trim(tdData).substring(0, tdData.length - 1);
  80. tdData += ";";
  81. //output
  82. //console.log(tdData);
  83. if (defaults.consoleLog == 'true') {
  84. console.log(tdData);
  85. }
  86. var base64data = "base64," + $.base64.encode(tdData);
  87. window.open('data:application/sql;filename=exportData;' + base64data);
  88. } else if (defaults.type == 'json') {
  89. var jsonHeaderArray = [];
  90. $(el).find('thead').find('tr').each(function () {
  91. var tdData = "";
  92. var jsonArrayTd = [];
  93. $(this).filter(':visible').find('th').each(function (index, data) {
  94. if ($(this).css('display') != 'none') {
  95. if (defaults.ignoreColumn.indexOf(index) == -1) {
  96. jsonArrayTd.push(parseString($(this)));
  97. }
  98. }
  99. });
  100. jsonHeaderArray.push(jsonArrayTd);
  101. });
  102. var jsonArray = [];
  103. $(el).find('tbody').find('tr').each(function () {
  104. var tdData = "";
  105. var jsonArrayTd = [];
  106. $(this).filter(':visible').find('td').each(function (index, data) {
  107. if ($(this).css('display') != 'none') {
  108. if (defaults.ignoreColumn.indexOf(index) == -1) {
  109. jsonArrayTd.push(parseString($(this)));
  110. }
  111. }
  112. });
  113. jsonArray.push(jsonArrayTd);
  114. });
  115. var jsonExportArray = [];
  116. jsonExportArray.push({header: jsonHeaderArray, data: jsonArray});
  117. //Return as JSON
  118. //console.log(JSON.stringify(jsonExportArray));
  119. //Return as Array
  120. //console.log(jsonExportArray);
  121. if (defaults.consoleLog == 'true') {
  122. console.log(JSON.stringify(jsonExportArray));
  123. }
  124. var base64data = "base64," + $.base64.encode(JSON.stringify(jsonExportArray));
  125. window.open('data:application/json;filename=exportData;' + base64data);
  126. } else if (defaults.type == 'xml') {
  127. var xml = '<?xml version="1.0" encoding="utf-8"?>';
  128. xml += '<tabledata><fields>';
  129. // Header
  130. $(el).find('thead').find('tr').each(function () {
  131. $(this).filter(':visible').find('th').each(function (index, data) {
  132. if ($(this).css('display') != 'none') {
  133. if (defaults.ignoreColumn.indexOf(index) == -1) {
  134. xml += "<field>" + parseString($(this)) + "</field>";
  135. }
  136. }
  137. });
  138. });
  139. xml += '</fields><data>';
  140. // Row Vs Column
  141. var rowCount = 1;
  142. $(el).find('tbody').find('tr').each(function () {
  143. xml += '<row id="' + rowCount + '">';
  144. var colCount = 0;
  145. $(this).filter(':visible').find('td').each(function (index, data) {
  146. if ($(this).css('display') != 'none') {
  147. if (defaults.ignoreColumn.indexOf(index) == -1) {
  148. xml += "<column-" + colCount + ">" + parseString($(this)) + "</column-" + colCount + ">";
  149. }
  150. }
  151. colCount++;
  152. });
  153. rowCount++;
  154. xml += '</row>';
  155. });
  156. xml += '</data></tabledata>'
  157. if (defaults.consoleLog == 'true') {
  158. console.log(xml);
  159. }
  160. var base64data = "base64," + $.base64.encode(xml);
  161. window.open('data:application/xml;filename=exportData;' + base64data);
  162. } else if (defaults.type == 'excel' || defaults.type == 'doc' || defaults.type == 'powerpoint') {
  163. //console.log($(this).html());
  164. var excel = "<table>";
  165. // Header
  166. $(el).find('thead').find('tr').each(function () {
  167. excel += "<tr>";
  168. $(this).filter(':visible').find('th').each(function (index, data) {
  169. if ($(this).css('display') != 'none') {
  170. if (defaults.ignoreColumn.indexOf(index) == -1) {
  171. excel += "<td>" + parseString($(this)) + "</td>";
  172. }
  173. }
  174. });
  175. excel += '</tr>';
  176. });
  177. // Row Vs Column
  178. var rowCount = 1;
  179. $(el).find('tbody').find('tr').each(function () {
  180. excel += "<tr>";
  181. var colCount = 0;
  182. $(this).filter(':visible').find('td').each(function (index, data) {
  183. if ($(this).css('display') != 'none') {
  184. if (defaults.ignoreColumn.indexOf(index) == -1) {
  185. excel += "<td>" + parseString($(this)) + "</td>";
  186. }
  187. }
  188. colCount++;
  189. });
  190. rowCount++;
  191. excel += '</tr>';
  192. });
  193. excel += '</table>'
  194. if (defaults.consoleLog == 'true') {
  195. console.log(excel);
  196. }
  197. var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:" + defaults.type + "' xmlns='http://www.w3.org/TR/REC-html40'>";
  198. excelFile += "<head>";
  199. excelFile += "<!--[if gte mso 9]>";
  200. excelFile += "<xml>";
  201. excelFile += "<x:ExcelWorkbook>";
  202. excelFile += "<x:ExcelWorksheets>";
  203. excelFile += "<x:ExcelWorksheet>";
  204. excelFile += "<x:Name>";
  205. excelFile += "{worksheet}";
  206. excelFile += "</x:Name>";
  207. excelFile += "<x:WorksheetOptions>";
  208. excelFile += "<x:DisplayGridlines/>";
  209. excelFile += "</x:WorksheetOptions>";
  210. excelFile += "</x:ExcelWorksheet>";
  211. excelFile += "</x:ExcelWorksheets>";
  212. excelFile += "</x:ExcelWorkbook>";
  213. excelFile += "</xml>";
  214. excelFile += "<![endif]-->";
  215. excelFile += "</head>";
  216. excelFile += "<body>";
  217. excelFile += excel;
  218. excelFile += "</body>";
  219. excelFile += "</html>";
  220. var base64data = "base64," + $.base64.encode(excelFile);
  221. window.open('data:application/vnd.ms-' + defaults.type + ';filename=exportData.doc;' + base64data);
  222. } else if (defaults.type == 'png') {
  223. html2canvas($(el), {
  224. onrendered: function (canvas) {
  225. var img = canvas.toDataURL("image/png");
  226. window.open(img);
  227. }
  228. });
  229. } else if (defaults.type == 'pdf') {
  230. var doc = new jsPDF('p', 'pt', 'a4', true);
  231. doc.setFontSize(defaults.pdfFontSize);
  232. // Header
  233. var startColPosition = defaults.pdfLeftMargin;
  234. $(el).find('thead').find('tr').each(function () {
  235. $(this).filter(':visible').find('th').each(function (index, data) {
  236. if ($(this).css('display') != 'none') {
  237. if (defaults.ignoreColumn.indexOf(index) == -1) {
  238. var colPosition = startColPosition + (index * 50);
  239. doc.text(colPosition, 20, parseString($(this)));
  240. }
  241. }
  242. });
  243. });
  244. // Row Vs Column
  245. var startRowPosition = 20;
  246. var page = 1;
  247. var rowPosition = 0;
  248. $(el).find('tbody').find('tr').each(function (index, data) {
  249. rowCalc = index + 1;
  250. if (rowCalc % 26 == 0) {
  251. doc.addPage();
  252. page++;
  253. startRowPosition = startRowPosition + 10;
  254. }
  255. rowPosition = (startRowPosition + (rowCalc * 10)) - ((page - 1) * 280);
  256. $(this).filter(':visible').find('td').each(function (index, data) {
  257. if ($(this).css('display') != 'none') {
  258. if (defaults.ignoreColumn.indexOf(index) == -1) {
  259. var colPosition = startColPosition + (index * 50);
  260. doc.text(colPosition, rowPosition, parseString($(this)));
  261. }
  262. }
  263. });
  264. });
  265. // Output as Data URI
  266. doc.output('datauri');
  267. }
  268. function parseString(data) {
  269. if (defaults.htmlContent == 'true') {
  270. content_data = data.html().trim();
  271. } else {
  272. content_data = data.text().trim();
  273. }
  274. if (defaults.escape == 'true') {
  275. content_data = escape(content_data);
  276. }
  277. return content_data;
  278. }
  279. }
  280. });
  281. })(jQuery);