Guest User

Untitled

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