日期没有在DataTables

时间:2017-12-08 19:16:16

标签: datatables

我的DataTables中的日期没有按照它应该的方式排序?我正在使用AJAX,这是使用的有效负载:

{"data": [["5/24/2017","<a href='bulletin_edit.aspx?id=19'>0</a>","Uncategorized","Owners","New bulletin","No expiration","<button type='button' class='btn btn-xs btn-primary' onclick='archiveCircular(19);'>Archive</button>","<button type='button' class='btn btn-xs btn-info' onclick='showBBDetails(19);'>Show</button>"],["3/2/2016","<a href='bulletin_edit.aspx?id=13'>0</a>","Advisory","Everyone","New system update","No expiration","<button type='button' class='btn btn-xs btn-primary' onclick='archiveCircular(13);'>Archive</button>","<button type='button' class='btn btn-xs btn-info' onclick='showBBDetails(13);'>Show</button>"],["3/2/2016","<a href='bulletin_edit.aspx?id=12'>0</a>","Advisory","Everyone","First day of access","No expiration","<button type='button' class='btn btn-xs btn-primary' onclick='archiveCircular(12);'>Archive</button>","<button type='button' class='btn btn-xs btn-info' onclick='showBBDetails(12);'>Show</button>"],["11/4/2015","<a href='bulletin_edit.aspx?id=8'>0</a>","Advisory","Everyone","Door fixing in the lobby","No expiration","<button type='button' class='btn btn-xs btn-primary' onclick='archiveCircular(8);'>Archive</button>","<button type='button' class='btn btn-xs btn-info' onclick='showBBDetails(8);'>Show</button>"],["11/4/2015","<a href='bulletin_edit.aspx?id=7'>0</a>","Uncategorized","Everyone","Facade painting today","No expiration","<button type='button' class='btn btn-xs btn-primary' onclick='archiveCircular(7);'>Archive</button>","<button type='button' class='btn btn-xs btn-info' onclick='showBBDetails(7);'>Show</button>"],["5/4/2015","<a href='bulletin_edit.aspx?id=6'>0</a>","Advisory","Everyone","Repainting of Balconies","No expiration","<button type='button' class='btn btn-xs btn-primary' onclick='archiveCircular(6);'>Archive</button>","<button type='button' class='btn btn-xs btn-info' onclick='showBBDetails(6);'>Show</button>"],["5/4/2015","<a href='bulletin_edit.aspx?id=5'>0</a>","Uncategorized","Everyone","Insect Fuming again","No expiration","<button type='button' class='btn btn-xs btn-primary' onclick='archiveCircular(5);'>Archive</button>","<button type='button' class='btn btn-xs btn-info' onclick='showBBDetails(5);'>Show</button>"],["5/4/2015","<a href='bulletin_edit.aspx?id=4'>0</a>","Uncategorized","Everyone","Annual Meeting 2015","No expiration","<button type='button' class='btn btn-xs btn-primary' onclick='archiveCircular(4);'>Archive</button>","<button type='button' class='btn btn-xs btn-info' onclick='showBBDetails(4);'>Show</button>"]]}

表格没有问题。但是,如果我尝试对第一个日期单元格进行排序,则会将其全部错误(附上屏幕截图)。

这是初始化代码:

var uriActive = "ajax/bulletinpost/get_active.aspx";
tActive = $('#tblActive').DataTable({
    "ajax": uriActive,
    dom: 'Bfrtip',
    buttons: [{ extend: 'excelHtml5', title: 'Data export' }, { extend: 'pdfHtml5', title: 'Data export' }],
    "deferRender": true,
    "lengthMenu": [[50, 100, 150, 200, 250, -1], [50, 100, 150, 200, 250, "All"]],
    "iDisplayLength": 50,
    "order": [],
    "columnDefs": [{ "targets": 'no-sort', "orderable": false }, { "searchable": false, "targets": [1,6,7] }],
    "fnDrawCallback": function (oSettings) {
        $('[data-toggle="popover"]').popover({ 'trigger': 'hover', 'placement': 'top' });
        $('.editable').editable({ mode: 'popup' });
    }
});

日期格式为mm/dd/yyyy。任何人都有任何想法?

1 个答案:

答案 0 :(得分:2)

默认情况下,只有Date.parse()可解析的字符串才会被识别为日期。 mm/dd/yyyy不是可解析的日期格式。具有某种形式的“奇数”日期格式的列将需要执行解析本身,例如通过使用插件。

但实际上并没有关注它。只需在render回调中解析日期,并将解析后的日期作为整数值返回:

"columnDefs": [
  { targets: 0, 
    type: 'num',
    render: function(data,type) {
      if (type == 'sort') return Date.parse( data.replace('/', '-').valueOf() )
      return data
    }
  },   
  { "targets": 'no-sort', "orderable": false }, 
  { "searchable": false, "targets": [1,6,7] }
],

现在按日期对日期进行排序 - &gt;的 http://jsfiddle.net/t34h6yuj/

注意:显然,您应该在data回调中对render执行某种形式的完整性检查。至少检查服务器是否返回了null值。