Problem Exporting data from the client and export to CSV


#1

Hi,
What I’m trying to do here is to export data from the client (table) and export it to CSV, i tried other export packages and it seems that they all exported from the server, my problem with this approach is my collection has a relationship with other collections which has a value id, when I exported this data, id is being exported not the actual value even if I’ve set-up my publish and subscription for al the template that needs this data with tracker.autorun.

Here’s table on the client:

Here’s the exported file:

Can someone help me how to achieve this?

Thanks in advance!


#2

Hi, try this : http://jsfiddle.net/terryyounghk/kpegu/


#3

Hi @nxcong,

Thank you for the effort!

I was actually finding another solution outside meteor and you perfectly pulled it off! I will try this solution and I will get back to you!

Thanks so much! :smile:


#4

Hi @nxcong,

i’ve tried the code above but it seems that there’s an issue in selecting table rows and columns like this:

var $rows = $table.find("tr:has(td)")

i get an Uncaught TypeError: tr:has(td) is not a function

here’s my function based on the link that you provided:

function exportTableToCSV($table, filename){
  var $rows = $table.find("tr:has(td)"),
  // Temporary delimiter characters unlikely to be typed by keyboard
  // This is to avoid accidentally splitting the actual contents
  tmpColDelim = String.fromCharCode(11), // vertical tab character
  tmpRowDelim = String.fromCharCode(0), // null character

  // actual delimiter characters for CSV format
  colDelim = '","',
  rowDelim = '"\r\n"',

  // Grab text from table into CSV formatted string
  csv = '"' + $rows.map(function (i, row) {
    var $row = $(row),
    $cols = $row.find('td');
    return $cols.map(function (j, col) {
      var $col = $(col),
      text = $col.text();
      return text.replace('/"/g', '""'); // escape double quotes
    }).get().join(tmpColDelim);
  }).get().join(tmpRowDelim)
    .split(tmpRowDelim).join(rowDelim)
    .split(tmpColDelim).join(colDelim) + '"',

    // Data URI
    csvData = 'data:application/octetstream' + encodeURIComponent(csv);
      $(this)
        .attr({
        'download': filename,
        'href': csvData,
        'target': '_blank'
      });
}

my event handler:

"click #exportEnrollee": function(event) {
    exportTableToCSV([$('#data>table'), 'export.csv']);
    },

my table:

<div id="data">
    <table id="enrollmentsTable" class="table table-striped tablesorter">
....