Tablesorter -> Extracting filtered data to csv file

戏子无情 提交于 2019-12-06 14:46:29

问题


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 is not a function $('.export').on('click', function(){

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

Update: Solution

Both the answers were right ! It is sad that I could accept only one :(

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


回答1:


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>

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(',') )
        });
});



回答2:


jQuery v1.4 does not have an on() function, change it to use bind() instead.

$('.exportcsv').bind('click', function(){

Or update the version of jQuery you are using to version 1.7+



来源:https://stackoverflow.com/questions/15936538/tablesorter-extracting-filtered-data-to-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!