简体   繁体   English

自定义排序函数未调用Jquery数据表的onLoad

[英]Custom sort function not called onLoad of Jquery datatable

I have added a custom sort functionality in datatable. 我在数据表中添加了自定义排序功能。 It works fine when the table header is clicked. 单击表头时,它可以正常工作。 However, On pageLoad I want the table to be sorted by that column using the same logic as provided in the custom sort function. 但是,在pageLoad上,我希望使用与自定义排序功能相同的逻辑,按该列对表进行排序。 It does not seem to do so. 它似乎没有这样做。 Here is my code snippet 这是我的代码片段

$(document).ready(function(){
  $("#stripedTableAcp").DataTable({
    "columnDefs": [
      { 
        "type": "acp_name",
        "targets": 3
      } 
    ],
    "order": [[ 3, "asc" ]],
    "pageLength": 100,
    "lengthMenu": [100, 500, 1000, 2000, 5000]
  });

  $.fn.dataTableExt.oSort["acp_name-desc"] = function (x, y)
  {
    console.log("1");
    x = jQuery(x).text();
    y = jQuery(y).text();

    temp = x.split("-");
    xPart1 = temp[0];
    xPart2 = parseInt(temp[1]);

    temp = y.split("-");
    yPart1 = temp[0];
    yPart2 = parseInt(temp[1]);

    if(xPart1 > yPart1)
    {
      return -1;
    }

    if(xPart2 > yPart2)
    {
      return -1;
    }

    return 1;
  };

  $.fn.dataTableExt.oSort["acp_name-asc"] = function (x, y)
  {
    console.log("2");
    x = jQuery(x).text();
    y = jQuery(y).text();

    temp = x.split("-");
    xPart1 = temp[0];
    xPart2 = parseInt(temp[1]);

    temp = y.split("-");
    yPart1 = temp[0];
    yPart2 = parseInt(temp[1]);

    if(xPart1 > yPart1)
    {
      return 1;
    }

    if(xPart2 > yPart2)
    {
      return 1;
    }

    return -1;
  }
});

Please note that the console.log added to the function is fired when the table header is clicked but not upon page load 请注意,单击表头而不是页面加载时会触发添加到该函数的console.log

datatable version is 1.10.19 as provided by the CDN CDN提供的datatable版本是1.10.19

Any help is greatly appreciated. 任何帮助是极大的赞赏。

You have declared them only. 您只声明了它们。 Call order in the last line of document.ready(). 呼叫顺序位于document.ready()的最后一行。

The problem was with initializing the datatable before initializing the custom plugins. 问题在于初始化自定义插件之前先初始化数据表。 Once the plugin code was moved before declaring the datatable, the problem was resolved. 在声明数据表之前移动了插件代码后,问题已解决。

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

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