How to sort datatables with date in descending order

前端 未结 14 1954
我寻月下人不归
我寻月下人不归 2020-12-29 11:11

I wish to show the records using datatables with default ordering based on one of my rows with date & time in descending order. Please help me in editing the jquery stru

14条回答
  •  离开以前
    2020-12-29 11:41

    Perfect solution that I could implement is this:

    1. If you generate data with AJAX at PHP file just make the line with date this way:
    $rows[] =
        [
        "name" => $name,
        "date" => [
            "display" => $date, // Ex: '31.12.2020'
            "timestamp" => strtotime($date),    // Timestamp value for ordering, Ex: 1609372800
        ]
    ]
    
    1. Then row would be output to JSON like this:
    {
    "name": "Vasya Pupkin",
    "date": {
            "display": "31.12.2020",
            "timestamp": "1609372800"
        },
    }
    
    1. Finish by editing your JavaScript TadaTables object "date" column like this:
    {
        "data": "date",
        render: {
            _: 'display',
            sort: 'timestamp'
        }
    },
    
    1. That's all! Now column with date is perfectly sorted.

提交回复
热议问题