Angular DataTable 未填充 DTInstance

Angular DataTable not populating DTInstance

渲染后我没有填充我的 DtInstance。任何人都遇到过这个问题。

<div ng-controller="InventoryTableController as vm">
          <table datatable="" dt-options="vm.dtOptions" dt-columns="vm.dtColumns"
                 dt-instance="vm.dtInstance"
                 class="display table table-bordered table-striped table-hover"></table>
      </div>

我能够通过这个 https://github.com/l-lin/angular-datatables/issues/365

修复它

问题是因为我像这样初始化了 dataHolder

vm.dtInstance = {};

当我把它改成vm.dtInstance = null;时它就修复了 甚至 vm.dtInstance = undefined 也行不通。

对我来说甚至 vm.dtInstance = null;不工作。 我最终浏览了指令的源代码,发现 dt-instance 也可以是 setter 函数。这解决了我的问题。

vm.setDTInstance = function(dtInst){
    vm.myTable = dtInst;
};

只需在此处添加我的 2 美分。

对我来说,问题已通过线程中的@rosshays 解决方案解决:https://github.com/l-lin/angular-datatables/issues/345

2017 年 12 月 4 日更新: 根据@trainoasis 的建议,我将解决方案复制到这里。

In your controller

$scope.nested = {};
$scope.nested.dtInstance = {}

And in your HTML

<table
    datatable=""
    class="table table-striped table-hover table-bordered table-condensed"
    dt-options="dtOptions"
    dt-columns=dtColumns
    dt-instance="nested.dtInstance">
</table>

我正在使用

.withOption('serverSide', true).withFnServerData(getDataFromServer)

我遇到了同样的问题,但已通过以下代码解决:

控制器

$scope.dtInstance = {};
$scope.dtIntanceCallback = function (instance) {
    $scope.dtInstance = instance;
}
$scope.dtRebind = function () {
    $scope.dtInstance.DataTable.draw()
}

HTML

<table datatable dt-instance="dtInstanceCallback"></table>
<button ng-click="dtRebind">Rebind</button>

触发此操作的另一种方法是使用 <... ng-repeat="table in tables"> <table datatables="ng" dt-instance="dtInstance" ... > 遍历 table 的序列。当有多个 table 时,这样 dtInstance 可能不会引用您期望的 table 也就不足为奇了。

但是,当您知道集合中始终只有一个 table 时,您可能会期望 dtInstance 指的是那个实例。但事实并非如此。在我的案例中,解决方案是摆脱额外的层并简单地使用 table.