我想在DataTables ColumnDefs中创建自定义变量。怎么样?

时间:2018-04-27 06:34:59

标签: javascript json datatables

我得到的Json如下:

[  
{  
  "id":1,
  "repDate":{  
     "offset":{  
        "totalSeconds":7200,
        "id":"+02:00",
        "rules":{  
           "fixedOffset":true,
           "transitions":[  

           ],
           "transitionRules":[  

           ]
        }
     },
     "nano":880042000,
     "year":2018,
     "monthValue":4,
     "dayOfMonth":25,
     "hour":12,
     "minute":58,
     "second":53,
     "month":"APRIL",
     "dayOfWeek":"WEDNESDAY",
     "dayOfYear":115
  },
  "hashrate":5114926.0
},
...more entries
]

我必须以特定格式显示日期:yyyy.mm.dd - hh.mm.ss.nnn,所以我想创建一个自定义变量,但我不确定在哪里。这是我的JS函数,用于检索json并设置DataTables。我尝试在columnDefs中创建一个字符串,但这不起作用。

   var table;

$(document).ready(function() {

    table = $('#main-table').DataTable({
            ajax: {
                url: '/refresh',
                dataSrc:''
            },
           paging: true,
           lengthChange: false,
           pageLength: 20,
           stateSave: true,
           info: true,
           searching: false,
           "columnDefs": [
                 {
                 "className": "text-center",
                 "targets": 0,
                 "data": "id",
                 },
                 {
                 "className": "text-center",
                 "targets": 1,
                 "data": "repDate.year" + "." + "repDate.monthValue" + "." + "repDate.dayOfMonth",
                 },
                 {
                 "className": "text-center",
                 "targets": 2,
                 "data": "hashrate",
                 }
           ],
           "aoColumns": [
             { "orderSequence": [ "asc", "desc" ] },
             { "orderSequence": [ "asc", "desc" ] },
             { "orderSequence": [ "desc", "asc" ] }
           ],
           "order": [[ 0, "asc" ]]
    });
});

setInterval(function(){
table.ajax.reload(null, false);
}, 8000);

1 个答案:

答案 0 :(得分:0)

感谢您的回答。在render选项后,似乎function效果很好。这是一个更正的代码段:

"columnDefs": [
                               {
                                   "className": "text-center",
                                   "targets": 0,
                                   "data": "id",
                               },
                               {
                                   "className": "text-center",
                                   "targets": 1,
                                   "data" : function(data){

                                   var seconds = data.repDate.second < 10 ? seconds = "0" + data.repDate.second : seconds = data.repDate.second;
                                   var minutes = data.repDate.minute < 10 ? minutes = "0" + data.repDate.minute : minutes = data.repDate.minute;
                                   var months = data.repDate.monthValue < 10 ? months = "0" + data.repDate.monthValue : months = data.repDate.monthValue;
                                   var days = data.repDate.dayOfMonth < 10 ? days = "0" + data.repDate.dayOfMonth : days = data.repDate.dayOfMonth;

                                   return data.repDate.year + "-" + months + "-" + days + "   " + data.repDate.hour + ":" + minutes + ":" + seconds;
                                   },
                               },
                               {
                                   "className": "text-center",
                                   "targets": 2,
                                   "data": function(data){
                                   return data.hashrate/1000.0;
                                   },
                               }
                           ]