繁体   English   中英

排序在JQuery数据表中不起作用

[英]Ordering not working in JQuery datatables

我有一个用AJAX调用绘制在页面上的数据表。 我需要对内容进行排序,以便默认情况下最新条目位于顶部。 但是,即使参数已更改,顺序似乎也没有任何变化,并且记录是随机列出的。

var InitiateViewOption = null;
var deviceId = null;
var deviceType = null;
var fromTime = null;
var toTime = null;
var keys = null;

function drawTable(from, to) {
    var device = $("#device-details");
    deviceId = device.data("deviceid");
    deviceType = device.data("devicetype");
    keys = device.data("attributes").split(",");
    fromTime = from * 1000;
    toTime = to * 1000;
    if ( $.fn.dataTable.isDataTable( '#stats-table' ) ) {
        var table = $('#stats-table').DataTable();
        table.clear().draw();
        table.ajax.reload();
    }
    else {
        $("#stats-table").datatables_extended({
            serverSide: true,
            processing: true,
            searching: false,
            ordering: true,
            pageLength: 100,
            order: [[ 0, 'desc' ]],
            ajax: {
                url: "/devicemgt/api/stats/paginate",
                data: buildAjaxData
            }
        });
    }
}

function buildAjaxData (){
    var settings = $("#stats-table").dataTable().fnSettings();

    var obj = {
        //default params
        "draw" : settings.iDraw,
        "start" : settings._iDisplayStart,
        "length" : settings._iDisplayLength,
        "columns" : "",
        "order": "",
        "deviceType" : deviceType,
        "deviceId" : deviceId,
        "from": fromTime,
        "to"  : toTime,
        "attributes" : JSON.stringify(keys)
    };

    return obj;
}

尽管进行ordering : true确实会启用表标题附近​​的图标,并且order: [[ 0, 'desc' ]]根据降序设置图标,但记录的实际顺序没有变化,每个新记录都在似乎随机附加在表格中的某处。

在此处输入图片说明

也根据此链接上的答案尝试了aaSorting : []选项,但没有更改

更新:确定未进行订购的原因。 问题出在.datatables_extended({函数中。一旦我将其更改为.DataTable({然后将serverSide: false ,然后提供要进行排序的列,它就开始工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM