如何按日期降序对数据表进行排序

时间:2016-06-14 13:59:52

标签: jquery html datatables

我希望使用基于我的一行日期和时间的默认排序的数据表来显示记录。时间按降序排列。请帮我编辑那个

的jquery结构

enter image description here

15 个答案:

答案 0 :(得分:4)

我有同样的问题。我使用date-eu排序插件以DD / MM / YY格式对日期进行排序,并且我包含了以下JS文件:

<script src="//cdn.datatables.net/plug-ins/1.10.11/sorting/date-eu.js" type="text/javascript"></script>

这对我有用。

$('#exemple').DataTable({
    "order": [[ 3, "desc" ]], //or asc 
    "columnDefs" : [{"targets":3, "type":"date-eu"}],
});

另请阅读stackoverflow上的这篇文章:Sorting date in datatable

答案 1 :(得分:2)

router:

  static-resources:

    default:

      enabled: true

      mapping: /pr/{id}

      paths:

        - classpath:web/public

以yyyyMMddHHmm格式设置日期。这将是您可排序的时间戳。然后使用无显示隐藏格式化的日期。这实际上是对joan16v答案的进一步解释。

答案 2 :(得分:1)

请参阅此笔:https://codepen.io/arnulfolg/pen/MebVgx

它使用//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js和//cdn.datatables.net/plug-ins/1.10.12/sorting/datetime -moment.js用于排序数据表

要默认对表格进行排序,请使用:

$.fn.dataTable.moment('DD/MM/YY');
$('#example').DataTable({ 
       "order": [[ 3, "desc" ]] 
    }); 

答案 3 :(得分:1)

只需将datasets = [modDF] for i_dataset, dataset in enumerate(datasets): X, y = dataset # normalize dataset for easier parameter selection X = StandardScaler().fit_transform(X) Traceback (most recent call last): File "<ipython-input-18-013c2a6bef49>", line 5, in <module> X = StandardScaler().fit_transform(X) File "C:\Users\rs\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\base.py", line 553, in fit_transform return self.fit(X, **fit_params).transform(X) File "C:\Users\rs\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\preprocessing\data.py", line 639, in fit return self.partial_fit(X, y) File "C:\Users\rs\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\preprocessing\data.py", line 663, in partial_fit force_all_finite='allow-nan') File "C:\Users\rs\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\utils\validation.py", line 496, in check_array array = np.asarray(array, dtype=dtype, order=order) File "C:\Users\rs\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\core\numeric.py", line 538, in asarray return array(a, dtype, copy=False, order=order) ValueError: could not convert string to float: 'Price' 直接添加到"type":"date"之类的列中即可。

答案 4 :(得分:0)

数据表中的默认排序:

$(document).ready(function() { 
    $('#example').DataTable({ 
        "order": [[ 3, "desc" ]] 
    }); 
});

您可以使用 asc 升序。 3 表示 4th 列将被默认订购。

答案 5 :(得分:0)

我得到了按日期排序的解决方案。只需将类型添加为“日期”,然后在目标中,您就可以通过带有datatable选项的列号(计数从0开始)。并使用列号和格式类型设置“顺序”。参见下面的代码,

columnDefs: [ { type: 'date', 'targets': [4] } ],
order: [[ 4, 'desc' ]]

答案 6 :(得分:0)

            Here the code:


           jQuery.extend(jQuery.fn.dataTableExt.oSort, {
             "date-uk-pre": function ( a ) {
              var ukDatea = a.split('-');
              return (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
           },


            "date-uk-asc": function ( a, b ) {
                return ((a < b) ? -1 : ((a > b) ? 1 : 0));
             },

            "date-uk-desc": function ( a, b ) {
               return ((a < b) ? 1 : ((a > b) ? -1 : 0));
              }
            }); 

答案 7 :(得分:0)

最简单的方法是在该列的每个TD标签中的日期之前添加一个隐藏的时间戳,例如:

<td class="sorting_1">
    <span style="display:none;">1547022615</span>09/01/2019  09:30
</td>

使用默认的字符串排序方式,时间戳将按所需的方式对列进行排序,并且在浏览器中呈现时不会显示。

答案 8 :(得分:0)

我知道这是一个旧线程。但您基本上可以使用“ aaSorting”

$('#exemple').DataTable({

    "aaSorting": [[3,'desc']],
});

答案 9 :(得分:0)

//在此处工作代码

$('#table').DataTable({
   columnDefs: [ { type: 'date', 'targets': [3] } ],
   order: [[ 3, 'desc' ]],          
});

答案 10 :(得分:0)

这是我的答案:

suspend

更多详细信息,请参见html5部分:https://datatables.net/manual/data/

答案 11 :(得分:0)

尝试一下,它对我有用

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<script src="https://cdn.datatables.net/plug-ins/1.10.21/sorting/datetime-moment.js"></script>
<script>
    $(document).ready(function () {
        $.fn.dataTable.moment( 'DD/MM/YYYY HH:mm' );
        $('#example').DataTable({"order": [[ 3, "desc" ]]});
    });
</script>

答案 12 :(得分:0)

我可以实现的完美解决方案是这样:

  1. 如果您使用AJAX在PHP文件中生成数据,只需用以下方式在日期中加上一行:
$rows[] =
    [
    "name" => $name,
    "date" => [
        "display" => $date, // Ex: '31.12.2020'
        "timestamp" => strtotime($date),    // Timestamp value for ordering, Ex: 1609372800
    ]
]
  1. 然后将这样的行输出到JSON:
{
"name": "Vasya Pupkin",
"date": {
        "display": "31.12.2020",
        "timestamp": "1609372800"
    },
}
  1. 通过如下编辑JavaScript TadaTables对象“日期”列来完成:
{
    "data": "date",
    render: {
        _: 'display',
        sort: 'timestamp'
    }
},
  1. 仅此而已!现在,带有日期的列已完美排序。

答案 13 :(得分:0)

这个问题已经很老了,答案中提到的大多数插件已经过时或已经停止工作(我已经尝试了所有)。

这是当前有效的方法。

添加扩展名:

$.fn.dataTable.ext.order['date-time'] = function (settings, col) {
    return this.api().column(col, { order: 'index' }).nodes().map(function (td, i) {
        var val = $(td).text().trim();    // Get datetime string from <td>
        return moment(val, "DD/MM/YYYY hh:mm:ss a").format("X"); 
    });
}

然后,对于您的数据表:

$('#example').DataTable({
    "columns": [
        null,
        null,
        null,
        { "orderDataType": "date-time" }, // date-time is a custom key created in the above ext
        null,
        null,
        null,
        null,
        null,
        null
     ]
});

更新: 您可以使用以下方法简化上述操作:

$('#example').DataTable({
    "columnDefs": [
        { "orderDataType": "date-time", "targets": [3] }
    ]
});

"targets": []数组可以包含要将日期时间排序应用于的所有列的索引(来自)。

注意:我使用过moment.js,您可以使用任何其他方法来创建有效的date / datetime对象。此外,所使用的引用是用于dom-sort插件的,因此,同样的方法也可以用于对具有复杂dom结构的列进行排序。

参考:https://datatables.net/examples/plug-ins/dom_sort

答案 14 :(得分:0)

如前所述 date-eu.js 库可以工作,但对我来说它需要修改代码:

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"date-eu-pre": function ( date ) {
    date = date.replace(" ", "");

    if ( !date ) {
        return 0;
    }

    var year;
    var eu_date = date.split(/[\.\-\/]/);


    if((eu_date[0] == undefined) || (eu_date[1] == undefined) || (eu_date[2] == undefined) ){
        eu_date[0] = 0;
        eu_date[1] = 0;
        eu_date[2] = 0;
    }

    //console.log(eu_date);

    /*year (optional)*/
    if ( eu_date[2] ) {
        year = eu_date[2];
    }
    else {
        year = 0;
    }

    /*month*/
    var month = eu_date[1];
    if ( month.length == 1 ) {
        month = 0+month;
    }

    /*day*/
    var day = eu_date[0];
    if ( day.length == 1 ) {
        day = 0+day;
    }

    return (year + month + day) * 1;
},

"date-eu-asc": function ( a, b ) {
    return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},

"date-eu-desc": function ( a, b ) {
    return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}

} );

相关问题