【问题标题】:AngularJS Datatable Showing No Data Available in TableAngularJS 数据表显示表中没有可用数据
【发布时间】:2015-06-30 22:28:30
【问题描述】:

好的,所以我是 AngularJS 的新手,但对编码并不陌生。在大多数情况下,我已经了解了 Angular 的所有内容,但是在过去的几天里,我一直被困在一件事上,我一直在拔头发。希望你们中的一个人能告诉我我做错了什么。

我正在使用 AngularJS 和 Jquery 数据表。从控制器加载的数据很好,甚至将其显示为一行,但它位于 Jquery Box 上方......在 Jquery Box 内它告诉我“没有可用数据”它还说 Showing 0 to 0 of 0 entries 即使 1 行应该会出现。

以下是表格的 HTML 外观:

<table ng-if="ConductorTypes" class="table table-striped table-hover" datatable-setup id="ConductorTypes" cellspacing="0" width="100%">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <tr data-ng-repeat="conductorType in ConductorTypes">
                        <td>
                            {{ conductorType.id }}
                        </td>
                        <td>
                            {{ conductorType.name }}
                        </td>
                        <td></td>
                    </tr>
                </tbody>
</table>

我的控制器如下所示:

'use strict';
app.controller('ConductorTypesController', ['$scope', 'stagingService', function ($scope, stagingService) {

    $scope.ConductorTypes = [];

    stagingService.getConductorTypes().then(function (results) {

        $scope.ConductorTypes = results.data;

    }, function (error) {
        alert(error.data.message);
    });

}]);

现在我还设置了指令,它看起来像这样:

app.directive('datatableSetup', function () {
        return {
            restrict: 'E, A, C',
            link: function (scope, element, attrs) {
                var table = element.dataTable({
                    "aoColumnDefs": [{
                        'bSortable': true,
                        'aTargets': [-1]
                    }],
                    "oLanguage": {
                        "oPaginate": {
                            "sPrevious": "Previous",
                            "sNext": "Next"
                        }
                    },
                    "iDisplayLength": 10,
                    "aLengthMenu": [
                      [5, 10, 25, 50, -1],
                      [5, 10, 25, 50, "All"]
                    ],
                    "sDom": '<"dt-panelmenu testbutton"><"dt-panelmenu clearfix"Tfr>t<"dt-panelfooter clearfix"ip>',
                    "oTableTools": {
                        "sSwfPath": "http://cdn.datatables.net/tabletools/2.2.2/swf/copy_csv_xls_pdf.swf"
                    }
                });
            }
        }
    }
);

我做错了什么?为什么这不填充 Jquery 数据表。任何帮助我将不胜感激。

【问题讨论】:

  • 您的“element.dataTable”很可能在控制器填充它之前运行
  • 您应该使用 datatables 指令。 l-lin.github.io/angular-datatables。没有必要重新发明轮子

标签: javascript jquery angularjs datatables


【解决方案1】:

感谢@Kevin F,我最终添加了一个超时,他给了我在加载后加载它的想法。

【讨论】:

  • 在哪里添加超时?你能用工作代码更新答案吗?
  • @query ` $scope.$on('$viewContentLoaded', function (event) { if ($state.current.name == 'tables') { // 延迟 $timeout(function ( ) { $scope.getDataTable(); }, 900); } });` 希望这会有所帮助
猜你喜欢
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2020-04-10
  • 1970-01-01
  • 1970-01-01
  • 2021-04-17
  • 2019-06-19
  • 2018-06-08
相关资源
最近更新 更多