Tablesorter - >将过滤后的数据提取到csv文件 [英] Tablesorter -> Extracting filtered data to csv file
问题描述
我想将通过tablesorter插件过滤的记录写入csv格式的外部文件。我正在通过此答案 / 145346 / mottie> Mottie ,Tablesorter插件的创建者。在FireBug错误控制台中,我收到错误消息
I want to write the records filtered ,through tablesorter plugin, to a external file in a csv format. I was following this answer by Mottie , creater of Tablesorter plugin. In FireBug error console, I'm getting error that says
TypeError:$(...)。on不是函数
$( '.export')。on('click',function(){
这是我的文件,使用tablesorter来提取csv格式的记录,
This is my file the uses tablesorter to extract the records in csv format,
<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<%
ArrayList<ArrayList<String>> resultsetlist = (ArrayList<ArrayList<String>>) request.getAttribute("SearchRecordsList");
%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>Research Records</title>
<!-- jQuery -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script>
<!-- Demo stuff -->
<link class="ui-theme" rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/cupertino/jquery-ui.css">
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7/jquery-ui.min.js"></script>
<link href="js/tablesorter/docs/css/prettify.css" rel="stylesheet">
<script src="js/tablesorter/docs/js/prettify.js"></script>
<script src="js/tablesorter/docs/js/docs.js"></script>
<!-- Tablesorter: required -->
<link rel="stylesheet" href="js/tablesorter/css/theme.blue.css">
<script src="js/tablesorter/js/jquery.tablesorter.js"></script>
<script src="js/tablesorter/js/jquery.tablesorter.widgets.js"></script>
<script>
$(function() {
$('table').tablesorter({
theme : 'blue',
widgets: ['zebra', 'filter' ]
});
});
$('.exportcsv').on('click', function(){
var csv = [];
// find only visible rows; we're ignoring filtered/hidden rows
$('table').find('tbody tr:visible').find('td').each(function(){
alert("Value of text" + $(this).text());
csv.push( $(this).text());
});
// do what you want with the csv data here
$('textarea').val( csv.join(',') )
});
</script>
<link rel="stylesheet" type="text/css" href="stylesheet1.css">
<title>JSP Page</title>
</head>
<body>
<table class="tablesorter" id="tablesorter-id-variable">
<thead>
<tr>
<%
int index = 0;
String s = "null";
Iterator itrcol = resultsetlist.iterator();
if (itrcol.hasNext()) {
ArrayList<String> col_record = (ArrayList<String>) itrcol.next();
for (index = 0; index < col_record.size(); index++) {
s = col_record.get(index);
%>
<th>
<% out.println(s);%>
</th>
<%
} // End of -for-
%>
</tr>
<%
} //end if
%>
</thead>
<tbody>
<tr>
<%
Iterator itr = resultsetlist.iterator();
itr.next();
while (itr.hasNext()) {
ArrayList<String> each_record = (ArrayList<String>) itr.next();
for (index = 0; index < each_record.size(); index++) {
s = each_record.get(index);
%>
<td>
<% out.println(s);%>
</td>
<%
} // End of -for-
%>
</tr>
<%
} //end while
%>
</tbody>
</table>
<button class="exportcsv">export csv</button><br>
<textarea cols="40" rows="10"></textarea>
</body>
</html>
上述代码中可能出现的错误是什么?在此先感谢:)
what could be possible error in the above code ? Thanks in advance :)
更新:解决方案
答案都是正确的!很遗憾我只能接受一个:(
Both the answers were right ! It is sad that I could accept only one :(
问题是我使用的是Jquery版本,其版本与1.4一样久。所以将它升级到最新的google cdn's - 1.8,解决了问题。感谢答案:)
The problem was that I was using a Jquery version that is as old as 1.4. So upgrading it to latest google cdn's - 1.8 , solved the issue. Thanks to the answers :)
推荐答案
使用新版本的jQuery,因为$()。 on仅在jQuery 1.7+中可用,并且您使用的是jQuery 1.4。
Use a new version of jQuery, since $().on is available only in jQuery 1.7+ and you are using jQuery 1.4.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
您可以将它用于安全性,许多javascript库使用$作为默认值。在这个.ready()中,$指的是jQuery对象。
You can use it for security, many javascript libraries use $ as default. Inside this .ready(), $ refers to jQuery object.
jQuery(document).ready(function($) {
$(function() {
$('table').tablesorter({
theme : 'blue',
widgets: ['zebra', 'filter' ]
});
});
$('.exportcsv').on('click', function(){
var csv = [];
// find only visible rows; we're ignoring filtered/hidden rows
$('table').find('tbody tr:visible').find('td').each(function(){
alert("Value of text" + $(this).text());
csv.push( $(this).text());
});
// do what you want with the csv data here
$('textarea').val( csv.join(',') )
});
});
这篇关于Tablesorter - >将过滤后的数据提取到csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!