2017-10-18 134 views
0

我已经创建了一个Datatable - 并且当UI显示排序图标时,单击它不会重新排列列。Jquery Datatables - UI排序不起作用

数据来自Springboot后端。

enter image description here

的Javascript

$(document).ready(function() { 

var table = $('#salesTable').DataTable({ 
    processing: 'true', 
    serverSide: 'true', 
    ajax: {url: '/getsales', dataSrc: ''}, 

    "columnDefs": [ 
     { "data": "salesno", "render": function (data, type, row) { return '<a href=/sales/' + data + '>' + data + '</a>'; }, "targets": 0, }, 
     { "data" : "start_date", "targets" : 1 }, 
     { "data": "names", "targets" : 2 }, 
     { "data": "address", "targets" : 3 }, 
     { "data": "cmfEntry", "render": function (data, type, row) { 
       return data === true ? '<div align = "center"><span class="glyphicon glyphicon-ok"></span></div>' : 
        '<div align = "center"><span class="glyphicon glyphicon-remove"></span></div>' }, "targets": 4 } 
     ] 
    }); 
}); 

HTML

<table id="salesTable" class="display table table-striped" width="100%"> 
     <thead> 
     <tr> 
      <th class="col-xs-2">Sales No</th> 
      <th class="col-xs-2">Date</th> 
      <th class="col-xs-2">Names</th> 
      <th class="col-xs-5">Address</th> 
      <th class="col-xs-1">CMFSales</th> 
     </tr> 
     </thead> 
     <tbody> 
     </tbody> 
    </table> 

共有5列和排序对任何人不工作的。

浏览器的控制台中没有错误消息。

UPDATE

http://live.datatables.net/kovuvisa/1/edit

这里有一个数据表小提琴。请注意,只要我注释掉ajax数据源,就会进行排序。 sort:[]数组似乎不是必需的 - 但我已经添加了它。

+0

嗨,如果你需要帮助,我建议添加dataSrc模式。基地如果很长。 – 2017-10-18 22:59:58

+0

你能解释更多吗? –

+0

帮助人们测试与你相同的上下文,但我想你必须在你的数据表配置中添加'sort:[true,true,false]' – 2017-10-18 23:47:50

回答

0

添加排序指令的dataTable配置阵列用相同的细胞,你列与价值布尔如果想要的或没有排序sort: [true, true, true, true, true]

$(document).ready(function() { 

var table = $('#salesTable').DataTable({ 
    processing: 'true', 
    serverSide: 'true', 
    sort: [true, true, true, true, true], 
    ajax: {url: '/getsales', dataSrc: ''}, 

    "columnDefs": [ 
     { "data": "salesno", "render": function (data, type, row) { return '<a href=/sales/' + data + '>' + data + '</a>'; }, "targets": 0, }, 
     { "data" : "start_date", "targets" : 1 }, 
     { "data": "names", "targets" : 2 }, 
     { "data": "address", "targets" : 3 }, 
     { "data": "cmfEntry", "render": function (data, type, row) { 
       return data === true ? '<div align = "center"><span class="glyphicon glyphicon-ok"></span></div>' : 
        '<div align = "center"><span class="glyphicon glyphicon-remove"></span></div>' }, "targets": 4 } 
     ] 
    }); 
});