使用tablesorter jquery插件对HTML表进行排序

时间:2021-02-03 03:30:57

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,

我使用javascript将json从cartodb转换为HTML表。现在我尝试使用Table sorter Jquery Plugin制作可排序的表格。我在我的代码中使用了Tablesorter但是我无法在结果中得到任何排序。我的代码是,

<!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>

使用tablesorter jquery插件对HTML表进行排序

1 个解决方案

#1


0  

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

首先,你的jQuery UI链接有一个错误 - 你错过了http:。

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

其次,你的脚本在第57行有一个语法错误 - 还有一个额外的}。

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

最后,您需要像这样调用.tablesorter()(有关更多详细信息,请参阅我的最终评论):

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

$(“#excelDataTable> table”)。tablesorter();

#1


0  

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

首先,你的jQuery UI链接有一个错误 - 你错过了http:。

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

其次,你的脚本在第57行有一个语法错误 - 还有一个额外的}。

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

最后,您需要像这样调用.tablesorter()(有关更多详细信息,请参阅我的最终评论):

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

$(“#excelDataTable> table”)。tablesorter();