【问题标题】:jQuery DataTables sorting is not workingjQuery DataTables 排序不起作用
【发布时间】:2015-03-04 18:47:09
【问题描述】:

使用 jQuery 2.1.3 和 DataTables 1.10.5,当我单击列上方的向上和向下箭头时,我的表格不会排序。从我从文档中可以看出,这是最简单的例子,应该可以工作。我似乎无法弄清楚为什么不是。

我的 HTML/JavaScript

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

        <title>Simple Example</title>

        <link rel="stylesheet" media="screen" href="/assets/css/bootstrap.min.css">
        <link rel="stylesheet" media="screen" href="/assets/css/bootstrap-theme.min.css">
        <link rel="stylesheet" media="screen" href="/assets/css/jquery.dataTables.css">
    </head>
    <body>
        <table id="table-guid" class="display compact" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>Column-1</th>
                    <th>Column-2</th>
                    <th>Column-3</th>
                </tr>
            </thead>

            <tfoot>
                <tr>
                    <th>Column-1</th>
                    <th>Column-2</th>
                    <th>Column-3</th>
                </tr>
            </tfoot>
            <tbody>
                <tr id="A">
                    <td>A-1</td>
                    <td>A-2</td>
                    <td>A-3</td>
                </tr>
                <tr id="B">
                    <td>B-1</td>
                    <td>B-2</td>
                    <td>B-3</td>
                </tr>
                <tr id="C">
                    <td>C-1</td>
                    <td>C-2</td>
                    <td>C-3</td>
                </tr>
            </tbody>
        </table>

        <script src="/assets/js/jquery-2.1.3.min.js"></script>
        <script src="/assets/js/bootstrap.min.js"></script>
        <script src="/assets/js/jquery.dataTables.min.js"></script>
        <script>
        $(document).ready(function ()
        {
            var table = $('#table-guid').DataTable();
        });
        </script>
    </body>
</html>

【问题讨论】:

  • 您在控制台中看到任何错误吗?我刚刚尝试了bin 并为我工作:)
  • 您的资产文件夹中有jquery.dataTables.min.cssjquery.dataTables.css。您似乎对所有文件都使用了缩小版本
  • 控制台没有错误,我使用的是所有库的缩小版。
  • 您的问题来自您的 td.我不知道为什么,但是如果您将 A-1 替换为其他可以工作的东西 JsFiddle
  • 即使您将 A-1 替换为 A-11,它也可以正常工作

标签: javascript jquery datatables datatables-1.10


【解决方案1】:

您必须定义列数据的类型。 这里的问题似乎来自连字符,它正在工作without it

解决方案JsFiddle

$('#table-guid').dataTable( {
    "columnDefs": [
        { "type": "numeric-comma", targets: "_all" }
    ]
});

这里是documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 2012-08-19
    • 2018-11-16
    • 2016-08-31
    • 1970-01-01
    相关资源
    最近更新 更多