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

问题描述 投票:0回答:2

我在datatable中添加了自定义排序功能。单击表标题时,它可以正常工作。但是,在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

数据表版本是CDN提供的1.10.19

任何帮助是极大的赞赏。

javascript jquery datatables-1.10
2个回答
0
投票

你只宣布了它们。在document.ready()的最后一行调用order


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.