【发布时间】: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