Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. (function($){
  2.  
  3. String.prototype.splitCSV = function(sep) {
  4. for (var thisCSV = this.split(sep = sep || ","), x = thisCSV.length - 1, tl; x >= 0; x--) {
  5. if (thisCSV[x].replace(/"s+$/, '"').charAt(thisCSV[x].length - 1) === '"') {
  6. if ((tl = thisCSV[x].replace(/^s+"/, '"')).length > 1 && tl.charAt(0) === '"') {
  7. thisCSV[x] = thisCSV[x].replace(/^s*"|"s*$/g, '').replace(/""/g, '"');
  8. } else if (x) {
  9. thisCSV.splice(x - 1, 2, [thisCSV[x - 1], thisCSV[x]].join(sep));
  10. } else thisCSV = thisCSV.shift().split(sep).concat(thisCSV);
  11. } else thisCSV[x].replace(/""/g, '"');
  12. } return thisCSV;
  13. };
  14.  
  15. $.fn.CSVToTable = function(csvFile, optiones) {
  16. var defaults = {
  17. tableClass: "CSVTable",
  18. theadClass: "",
  19. thClass: "",
  20. tbodyClass: "",
  21. trClass: "",
  22. tdClass: "",
  23. separator: ",",
  24. startLine: 0
  25. };
  26. var options = $.extend(defaults, optiones);
  27. return this.each(function() {
  28. var obj = $(this);
  29. var error = '';
  30. $.get(csvFile, function(data) {
  31. var tableHTML = '<table class="' + options.tableClass + '">';
  32. var lines = data.replace('r','').split('n');
  33. var printedLines = 0;
  34. var headerCount = 0;
  35. var headers = new Array();
  36. var columnCount = 9;
  37. $.each(lines, function(lineCount, line) {
  38.  
  39. if (lineCount >= options.startLine) {
  40. var items = line.splitCSV(options.separator);
  41. if (items.length > 1) {
  42. printedLines++;
  43.  
  44. (printedLines % 2) ? oddOrEven = 'odd' : oddOrEven = 'even';
  45. tableHTML += '<tr class="' + options.trClass + ' ' + oddOrEven + '">';
  46. $.each(items, function(itemCount, item) {
  47. var currentColumn = (itemCount % columnCount) + 1// add 1 because of zero index
  48. tableHTML += '<td class="column' + currentColumn + '">' + item + '</td>'; });
  49. tableHTML += '</tr>';
  50. }
  51. }
  52. });
  53. tableHTML += '</tbody></table>';
  54. if (error) {
  55. obj.html(error);
  56. } else {
  57. obj.fadeOut(500, function() {
  58. obj.html(tableHTML)
  59. }).fadeIn(function() {
  60. // trigger loadComplete
  61. setTimeout(function() {
  62. obj.trigger("loadComplete");
  63. },0);
  64. });
  65. }
  66. });
  67. });
  68. };
  69.  
  70. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement