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

时间:2018-09-06 21:21:13

标签: javascript jquery datatables-1.10

我在数据表中添加了自定义排序功能。单击表头时,它可以正常工作。但是,在pageLoad上,我希望使用与自定义排序功能相同的逻辑,按该列对表进行排序。 它似乎没有这样做。 这是我的代码段

$(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;
  }
});

请注意,单击表头而不是页面加载时会触发添加到该函数的console.log

CD所提供的

数据表版本为1.10.19

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您仅声明了它们。在document.ready()的最后一行中调用order

答案 1 :(得分:0)

问题在于初始化自定义插件之前先初始化数据表。在声明数据表之前移动了插件代码后,问题已解决。