简体   繁体   English

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

[英]Ordering not working in JQuery datatables

I have a datatable being drawn on a page with an AJAX call. 我有一个用AJAX调用绘制在页面上的数据表。 I need to order the contents such that by default the latest entry is at the top. 我需要对内容进行排序,以便默认情况下最新条目位于顶部。 But even though the parameters have been changed there seems to be no change in the ordering at all and the records are listed at random. 但是,即使参数已更改,顺序似乎也没有任何变化,并且记录是随机列出的。

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;
}

Although making ordering : true does enable the icon near the table headers and the order: [[ 0, 'desc' ]] sets the icon according to the descending order there is no change in the actual order of the records with each new record being seemingly appended at random somewhere in the table. 尽管进行ordering : true确实会启用表标题附近​​的图标,并且order: [[ 0, 'desc' ]]根据降序设置图标,但记录的实际顺序没有变化,每个新记录都在似乎随机附加在表格中的某处。

在此处输入图片说明

Tried the aaSorting : [] option according to the answer on this link as well but with no change 也根据此链接上的答案尝试了aaSorting : []选项,但没有更改

UPDATE: Found out the reason behind the ordering not taking place. 更新:确定未进行订购的原因。 The issue was in the .datatables_extended({ function. Once I changed that to .DataTable({ then made serverSide: false and then gave the columns for the ordering to take place it started working. 问题出在.datatables_extended({函数中。一旦我将其更改为.DataTable({然后将serverSide: false ,然后提供要进行排序的列,它就开始工作。

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

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