TableSorter:如何将结果导出到 csv? [英] TableSorter : how to export results to csv?

查看:33
本文介绍了TableSorter:如何将结果导出到 csv?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TableSorter 是一个很棒的 jquery 脚本,可以用很多选项对 html 表格进行排序.

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

但我不知道如何添加一个简单的导出到 csv"按钮(或链接)来获取包含我的表格记录的文件(没有特殊格式).

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.

提前感谢您的帮助!

泰德

推荐答案

它实际上并不复杂,只是因为所有选项看起来令人生畏.输出小部件 可以输出 csv、tsv、任何其他分隔的 (空格、分号等)值、javascript 数组或 JSON.

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:

  • 将 csv 输出到弹出窗口
  • 只包含最后一个标题行
  • 只包含过滤的行(所以如果过滤器小部件没有被使用,则所有行)
  • 将只输出表格单元格文本(忽略 HTML)

您只需要此代码(演示):

HTML

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

脚本

$(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>

脚本

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).

这篇关于TableSorter:如何将结果导出到 csv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆