我,在我的一生中,似乎无法让 tablesorter 的下载到 CSV 功能发挥作用。我认为我的设置可能有问题,因此我创建了一个简单的测试表,但仍然遇到了同样的问题。
根据official documentation ,我需要 tablesorter 2.8 或更高版本(我使用的是 2.25.3)和 jQuery 1.7 或更高版本(我正在引入 jQuery 1.12.0)。我按照莫蒂自己的简单设置从 this question但我运气不好。
下面是我的测试代码。我必须错过一些明显的东西,但是,在盯着它几个小时之后,我没有看到它。
<head>
<title>Table to CSV</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.25.4/js/jquery.tablesorter.combined.min.js"></script>
</head>
<body>
<button class="download">Download CSV</button>
<table class="tablesorter">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Number</th>
<th>Food</th>
</tr>
</thead>
<tbody>
<tr>
<td>Billy</td>
<td>Bob</td>
<td>4</td>
<td>Pizza</td>
</tr>
<tr>
<td>Jill</td>
<td>Jackson</td>
<td>23</td>
<td>Tacos</td>
</tr>
<tr>
<td>Robert</td>
<td>Roy</td>
<td>6</td>
<td>Hamburger</td>
</tr>
</tbody>
</table>
<script>
$( document ).ready(function(){
var $table = $("table");
$table.tablesorter({
widgets: ["output"]
});
$('.download').click(function(){
$table.trigger("outputTable");
console.log("Download clicked.");
});
});
</script>
</body>
编辑:用 cloudflare 替换了我自己的 [本地] tabelsorter 脚本 src。
最佳答案
我想你只是错过了 widget-output.js
文件:
https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.25.4/js/widgets/widget-output.min.js
应该使用 <script>
加载它在表排序器之后。点击this demo的“外部资源”部分查看您需要的所有文件(不包括 jQuery)。
关于jquery - Tablesorter 输出到 CSV 不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35711501/