3

TableSorter is a great jquery script to sort html tables with many options.

But I don't know how to add a simple 'export to csv' button (or link) to get a file containing the records of my table (with no special formatting).

I know the Output Plugin but it seems far too complex to me.

Thanks by advance for your help !

Ted

Ted22
  • 75
  • 1
  • 6

1 Answers1

6

It's actually not complicated, it only looks intimidating because of all the options. The output widget can output csv, tsv, any other separated (space, semi-colon, etc) values, javascript array or JSON.

If you are just using basic functionality, the default settings will:

  • Output csv to a popup window
  • Only include the last header row
  • Only include filtered rows (so all rows if the filter widget isn't even being used)
  • Will only output the table cell text (ignores HTML)

All you would need is this code (demo):

HTML

<button class="download">Get CSV</button>
<table class="tablesorter">
    ....
</table>

Script

$(function () {
    var $table = $('table');

    $('.download').click(function(){
        $table.trigger('outputTable');
    });

    $table.tablesorter({
        theme: 'blue',
        widgets: ['zebra', 'output']
    });
});

I created another demo, showing all options, with a set of radio buttons which allow the user to choose between sending the output to a popup window, or downloading the file.

HTML

<label><input data-delivery="p" name="delivery" type="radio" checked /> popup</label>
<label><input data-delivery="d" name="delivery" type="radio" /> download</label>
<button class="download">Get CSV</button>

Script

var $table = $('table');

$('.download').click(function(){
    // get delivery type
    var delivery = $('input[name=delivery]:checked').attr('data-delivery');
    $table.data('tablesorter').widgetOptions.output_delivery = delivery;        
    $table.trigger('outputTable');
});

So, you can make it as simple or complex as you want (see the actual output widget demo which allows the user to set almost all the options).

Mottie
  • 84,355
  • 30
  • 126
  • 241
  • Thanks a lot, Mottie, it works perfectly. This Output widget is a great piece ! Now, is it possible to export not the entire content of cells but only a specific div, the same way sorting works ? – Ted22 Apr 28 '14 at 08:39
  • There isn't a method to select content from a specific div, but the [`output_dataAttrib` option](http://mottie.github.io/tablesorter/docs/example-widget-output.html#output_dataattrib) allows you to set the name of a data-attribute which can contain any overriding text to be sent to the output. Click on that link and look at the examples. – Mottie Apr 29 '14 at 05:39
  • Ok, it works with output_dataAttrib. Thanks Mottie ! – Ted22 Apr 29 '14 at 13:03
  • my $table.trigger(outputTable) is firing, and I can get an alert there, but no pop-up happens. What magic is going on in:$table.trigger('outputTable'); It seems some piece is missing. – Daniel Williams Apr 18 '16 at 18:20
  • Does the main demo work for you? Please check to make sure your browser isn't set or has an extension/addon that prevents popup windows from opening. – Mottie Apr 18 '16 at 18:24