Search code examples
javascriptjquerytablesorter

Sorting HTML table using tablesorter jquery plugin


I converted the json from cartodb to HTML table using javascript. Now i am trying to use the Table sorter Jquery Plugin for making into a sortable tables. I used the Tablesorter in my code but i couldnot able to get any sorting in the result.Below is my code,

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script src="js/jquery.tablesorter.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" />
   <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.9/css/jquery.dataTables.css">
    <style>
        canvas {
            height: 400px;
            margin: 5px;
            width: 1500px;
        }

    </style>
</head>
<body>
    <div id="excelDataTable">
    </div>


    <script>
        $(function () {
                    data2 = "carto"; 
                    json_link = 'http://development.localhost.lan:8080/api/v2/sql?q=SELECT * FROM ' + data2;
                    $.getJSON(json_link, function (result) { 
                        var th_main = "";
                        for (var e in result['fields']) {
                            th_main = th_main + '<th>' + e + '</th>';
                        }
                        var table_header = '<thead>' +
                                  '<tr>' +
                                    th_main +
                                 '</tr>' +
                              '</thead>';
                        var body_main = "";
                        for (var i = 0; i < result['rows'].length; i++) {
                            var tr = "<tr>";
                            for (var ele in result['rows'][i]) {
                                tr = tr + '<td>' + result['rows'][i][ele] + '</td>';
                            }
                            var tr = tr + "</tr>";
                            body_main = body_main + tr;
                        }
                        var table_body = '<tbody>' + body_main + '</tbody>';
                        var table = "<table class='table table-bordered table-hover table-condensed table-striped'>" + table_header + table_body + "</table>";
                        $("#excelDataTable").html(table);
                        $(document).ready(function() { 
                            $("#excelDataTable").tablesorter(); 
                        } 
                                );



        });
    }  
                       }
            );
        });

</script>


</body>

enter image description here


Solution

  • There are a couple of things that might be causing your issue:

    First, there is an error in your link to jQuery UI - you're missing the http:.

    Second, your script has a syntax error on line 57 - there is an extra }.

    If you check the browser console it will tell you about these things.

    Finally, you need to call .tablesorter() like so (see my final comment for more details):

    $("#excelDataTable > table").tablesorter();