【问题标题】:Datatables sort date (d/m/Y H:i A)数据表排序日期 (d/m/Y H:i A)
【发布时间】:2017-04-17 09:19:30
【问题描述】:

我还是数据表的新手。我的日期格式如下 01/10/2011 10:10 AM,我想对其进行排序。我已经阅读了可用的插件,但我仍然无法正确排序,因为它只根据日期而不是月份和年份进行排序。

<script type="text/javascript" src="datatables/datatables.net/js/jquery.dataTables.js"></script>
  <script type="text/javascript" src="cdn.datatables.net/plug-ins/1.10.13/sorting/date-euro.js"></script>
  <script type="text/javascript">

    $(document).ready(function() {
        $('#test').dataTable({
            "columnDefs": [{
            "type": "date-euro",
            targets: 1
          }]
        });
    });

  </script>
<?php $filetime  = date("d/m/Y H:i A",filemtime($dir.'/'.$basename)); ?>

<table id="test">
<thead>
<tr>
<th>Name</th>
<th>Date Modified</th>
</tr>
</thead>
<tbody>
<tr>
<td>Test</td>
<td><?php echo $filetime; ?></td>
</tr>
</tbody>

【问题讨论】:

标签: javascript php jquery html datatable


【解决方案1】:

您可以使用 HTML5 数据属性。 https://www.datatables.net/examples/advanced_init/html5-data-attributes.html

只需将 data-order 元素添加到您的 td 元素。

  <table class="table" id="exampleTable">
<thead>
<tr>
<th>Name</th>
<th>Date Modified</th>
</tr>
</thead>
<tbody>
<tr>
<td>Test</td>
<td data-order="<?php echo date( "Y-m-d H:i", strtotime( $filetime) ); // PHP:  2015-11-13 12:00; ?>"><?php echo $filetime; ?></td>
</tr>
</tbody>


<script>
    $(document).ready(function() {
        $('#exampleTable').DataTable();
    });
</script>

【讨论】:

  • 但它仍然只根据日期排序。不是月份和年份,我希望它是 d/m/Y H:i A
  • @Athira 你检查过这个例子吗? datatables.net/examples/advanced_init/…
  • 好的,我现在明白了。但我做了一些改变。我在没有将日期转换为 d/m/Y H:i A. 的情况下定义了 $filetime到时间。为其他用户的修订进行编辑。谢谢。
  • 但是移动设备支持数据排序吗?
【解决方案2】:

DataTables 似乎默认不包含 date-euro 类型,因此请确保在代码中的某处包含 the definition

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
    "date-euro-pre": function ( a ) {
        var x;

        if ( $.trim(a) !== '' ) {
            var frDatea = $.trim(a).split(' ');
            var frTimea = (undefined != frDatea[1]) ? frDatea[1].split(':') : [00,00,00];
            var frDatea2 = frDatea[0].split('/');
            x = (frDatea2[2] + frDatea2[1] + frDatea2[0] + frTimea[0] + frTimea[1] + frTimea[2]) * 1;
        }
        else {
            x = Infinity;
        }

        return x;
    },

    "date-euro-asc": function ( a, b ) {
        return a - b;
    },

    "date-euro-desc": function ( a, b ) {
        return b - a;
    }
} );

【讨论】:

    猜你喜欢
    • 2017-10-07
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 2014-04-06
    • 2014-03-12
    相关资源
    最近更新 更多