tableExport.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*The MIT License (MIT)
  2. Copyright (c) 2014 https://github.com/kayalshri/
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.*/
  18. (function($){
  19. $.fn.extend({
  20. tableExport: function(options) {
  21. var defaults = {
  22. separator: ',',
  23. ignoreColumn: [],
  24. tableName:'yourTableName',
  25. type:'csv',
  26. pdfFontSize:14,
  27. pdfLeftMargin:20,
  28. escape:'true',
  29. htmlContent:'false',
  30. consoleLog:'false'
  31. };
  32. var options = $.extend(defaults, options);
  33. var el = this;
  34. if(defaults.type == 'csv' || defaults.type == 'txt'){
  35. // Header
  36. var tdData ="";
  37. $(el).find('thead').find('tr').each(function() {
  38. tdData += "\n";
  39. $(this).filter(':visible').find('th').each(function(index,data) {
  40. if ($(this).css('display') != 'none'){
  41. if(defaults.ignoreColumn.indexOf(index) == -1){
  42. tdData += '"' + parseString($(this)) + '"' + defaults.separator;
  43. }
  44. }
  45. });
  46. tdData = $.trim(tdData);
  47. tdData = $.trim(tdData).substring(0, tdData.length -1);
  48. });
  49. // Row vs Column
  50. $(el).find('tbody').find('tr').each(function() {
  51. tdData += "\n";
  52. $(this).filter(':visible').find('td').each(function(index,data) {
  53. if ($(this).css('display') != 'none'){
  54. if(defaults.ignoreColumn.indexOf(index) == -1){
  55. tdData += '"'+ parseString($(this)) + '"'+ defaults.separator;
  56. }
  57. }
  58. });
  59. //tdData = $.trim(tdData);
  60. tdData = $.trim(tdData).substring(0, tdData.length -1);
  61. });
  62. //output
  63. if(defaults.consoleLog == 'true'){
  64. console.log(tdData);
  65. }
  66. var base64data = "base64," + $.base64.encode(tdData);
  67. window.open('data:application/'+defaults.type+';filename=exportData;' + base64data);
  68. }else if(defaults.type == 'sql'){
  69. // Header
  70. var tdData ="INSERT INTO `"+defaults.tableName+"` (";
  71. $(el).find('thead').find('tr').each(function() {
  72. $(this).filter(':visible').find('th').each(function(index,data) {
  73. if ($(this).css('display') != 'none'){
  74. if(defaults.ignoreColumn.indexOf(index) == -1){
  75. tdData += '`' + parseString($(this)) + '`,' ;
  76. }
  77. }
  78. });
  79. tdData = $.trim(tdData);
  80. tdData = $.trim(tdData).substring(0, tdData.length -1);
  81. });
  82. tdData += ") VALUES ";
  83. // Row vs Column
  84. $(el).find('tbody').find('tr').each(function() {
  85. tdData += "(";
  86. $(this).filter(':visible').find('td').each(function(index,data) {
  87. if ($(this).css('display') != 'none'){
  88. if(defaults.ignoreColumn.indexOf(index) == -1){
  89. tdData += '"'+ parseString($(this)) + '",';
  90. }
  91. }
  92. });
  93. tdData = $.trim(tdData).substring(0, tdData.length -1);
  94. tdData += "),";
  95. });
  96. tdData = $.trim(tdData).substring(0, tdData.length -1);
  97. tdData += ";";
  98. //output
  99. //console.log(tdData);
  100. if(defaults.consoleLog == 'true'){
  101. console.log(tdData);
  102. }
  103. var base64data = "base64," + $.base64.encode(tdData);
  104. window.open('data:application/sql;filename=exportData;' + base64data);
  105. }else if(defaults.type == 'json'){
  106. var jsonHeaderArray = [];
  107. $(el).find('thead').find('tr').each(function() {
  108. var tdData ="";
  109. var jsonArrayTd = [];
  110. $(this).filter(':visible').find('th').each(function(index,data) {
  111. if ($(this).css('display') != 'none'){
  112. if(defaults.ignoreColumn.indexOf(index) == -1){
  113. jsonArrayTd.push(parseString($(this)));
  114. }
  115. }
  116. });
  117. jsonHeaderArray.push(jsonArrayTd);
  118. });
  119. var jsonArray = [];
  120. $(el).find('tbody').find('tr').each(function() {
  121. var tdData ="";
  122. var jsonArrayTd = [];
  123. $(this).filter(':visible').find('td').each(function(index,data) {
  124. if ($(this).css('display') != 'none'){
  125. if(defaults.ignoreColumn.indexOf(index) == -1){
  126. jsonArrayTd.push(parseString($(this)));
  127. }
  128. }
  129. });
  130. jsonArray.push(jsonArrayTd);
  131. });
  132. var jsonExportArray =[];
  133. jsonExportArray.push({header:jsonHeaderArray,data:jsonArray});
  134. //Return as JSON
  135. //console.log(JSON.stringify(jsonExportArray));
  136. //Return as Array
  137. //console.log(jsonExportArray);
  138. if(defaults.consoleLog == 'true'){
  139. console.log(JSON.stringify(jsonExportArray));
  140. }
  141. var base64data = "base64," + $.base64.encode(JSON.stringify(jsonExportArray));
  142. window.open('data:application/json;filename=exportData;' + base64data);
  143. }else if(defaults.type == 'xml'){
  144. var xml = '<?xml version="1.0" encoding="utf-8"?>';
  145. xml += '<tabledata><fields>';
  146. // Header
  147. $(el).find('thead').find('tr').each(function() {
  148. $(this).filter(':visible').find('th').each(function(index,data) {
  149. if ($(this).css('display') != 'none'){
  150. if(defaults.ignoreColumn.indexOf(index) == -1){
  151. xml += "<field>" + parseString($(this)) + "</field>";
  152. }
  153. }
  154. });
  155. });
  156. xml += '</fields><data>';
  157. // Row Vs Column
  158. var rowCount=1;
  159. $(el).find('tbody').find('tr').each(function() {
  160. xml += '<row id="'+rowCount+'">';
  161. var colCount=0;
  162. $(this).filter(':visible').find('td').each(function(index,data) {
  163. if ($(this).css('display') != 'none'){
  164. if(defaults.ignoreColumn.indexOf(index) == -1){
  165. xml += "<column-"+colCount+">"+parseString($(this))+"</column-"+colCount+">";
  166. }
  167. }
  168. colCount++;
  169. });
  170. rowCount++;
  171. xml += '</row>';
  172. });
  173. xml += '</data></tabledata>'
  174. if(defaults.consoleLog == 'true'){
  175. console.log(xml);
  176. }
  177. var base64data = "base64," + $.base64.encode(xml);
  178. window.open('data:application/xml;filename=exportData;' + base64data);
  179. }else if(defaults.type == 'excel' || defaults.type == 'doc'|| defaults.type == 'powerpoint' ){
  180. //console.log($(this).html());
  181. var excel="<table>";
  182. // Header
  183. $(el).find('thead').find('tr').each(function() {
  184. excel += "<tr>";
  185. $(this).filter(':visible').find('th').each(function(index,data) {
  186. if ($(this).css('display') != 'none'){
  187. if(defaults.ignoreColumn.indexOf(index) == -1){
  188. excel += "<td>" + parseString($(this))+ "</td>";
  189. }
  190. }
  191. });
  192. excel += '</tr>';
  193. });
  194. // Row Vs Column
  195. var rowCount=1;
  196. $(el).find('tbody').find('tr').each(function() {
  197. excel += "<tr>";
  198. var colCount=0;
  199. $(this).filter(':visible').find('td').each(function(index,data) {
  200. if ($(this).css('display') != 'none'){
  201. if(defaults.ignoreColumn.indexOf(index) == -1){
  202. excel += "<td>"+parseString($(this))+"</td>";
  203. }
  204. }
  205. colCount++;
  206. });
  207. rowCount++;
  208. excel += '</tr>';
  209. });
  210. excel += '</table>'
  211. if(defaults.consoleLog == 'true'){
  212. console.log(excel);
  213. }
  214. 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'>";
  215. excelFile += "<head>";
  216. excelFile += "<!--[if gte mso 9]>";
  217. excelFile += "<xml>";
  218. excelFile += "<x:ExcelWorkbook>";
  219. excelFile += "<x:ExcelWorksheets>";
  220. excelFile += "<x:ExcelWorksheet>";
  221. excelFile += "<x:Name>";
  222. excelFile += "{worksheet}";
  223. excelFile += "</x:Name>";
  224. excelFile += "<x:WorksheetOptions>";
  225. excelFile += "<x:DisplayGridlines/>";
  226. excelFile += "</x:WorksheetOptions>";
  227. excelFile += "</x:ExcelWorksheet>";
  228. excelFile += "</x:ExcelWorksheets>";
  229. excelFile += "</x:ExcelWorkbook>";
  230. excelFile += "</xml>";
  231. excelFile += "<![endif]-->";
  232. excelFile += "</head>";
  233. excelFile += "<body>";
  234. excelFile += excel;
  235. excelFile += "</body>";
  236. excelFile += "</html>";
  237. var base64data = "base64," + $.base64.encode(excelFile);
  238. window.open('data:application/vnd.ms-'+defaults.type+';filename=exportData.doc;' + base64data);
  239. }else if(defaults.type == 'png'){
  240. html2canvas($(el), {
  241. onrendered: function(canvas) {
  242. var img = canvas.toDataURL("image/png");
  243. window.open(img);
  244. }
  245. });
  246. }else if(defaults.type == 'pdf'){
  247. var doc = new jsPDF('p','pt', 'a4', true);
  248. doc.setFontSize(defaults.pdfFontSize);
  249. // Header
  250. var startColPosition=defaults.pdfLeftMargin;
  251. $(el).find('thead').find('tr').each(function() {
  252. $(this).filter(':visible').find('th').each(function(index,data) {
  253. if ($(this).css('display') != 'none'){
  254. if(defaults.ignoreColumn.indexOf(index) == -1){
  255. var colPosition = startColPosition+ (index * 50);
  256. doc.text(colPosition,20, parseString($(this)));
  257. }
  258. }
  259. });
  260. });
  261. // Row Vs Column
  262. var startRowPosition = 20; var page =1;var rowPosition=0;
  263. $(el).find('tbody').find('tr').each(function(index,data) {
  264. rowCalc = index+1;
  265. if (rowCalc % 26 == 0){
  266. doc.addPage();
  267. page++;
  268. startRowPosition=startRowPosition+10;
  269. }
  270. rowPosition=(startRowPosition + (rowCalc * 10)) - ((page -1) * 280);
  271. $(this).filter(':visible').find('td').each(function(index,data) {
  272. if ($(this).css('display') != 'none'){
  273. if(defaults.ignoreColumn.indexOf(index) == -1){
  274. var colPosition = startColPosition+ (index * 50);
  275. doc.text(colPosition,rowPosition, parseString($(this)));
  276. }
  277. }
  278. });
  279. });
  280. // Output as Data URI
  281. doc.output('datauri');
  282. }
  283. function parseString(data){
  284. if(defaults.htmlContent == 'true'){
  285. content_data = data.html().trim();
  286. }else{
  287. content_data = data.text().trim();
  288. }
  289. if(defaults.escape == 'true'){
  290. content_data = escape(content_data);
  291. }
  292. return content_data;
  293. }
  294. }
  295. });
  296. })(jQuery);