修复 header 插件在 Angular 数据表中不起作用
Fix header plugin not working in Angular datatables
我使用的是固定的稳定构建版本Stable release v2.1.2 header jquery plugin. I am trying to fix my table header. I have created the table using Angular datatables as mentioned over here.
在我的控制器中class我写了下面的代码,
app.controller("SampleController", function ($scope) {
$(document).ready( function () {
var table = $('#example').DataTable();
new $.fn.dataTable.FixedHeader( table );
});
});
我的HTML如下,
<table id="example" datatable="ng" dt-options="dtOptions" dt-column-defs="dtColumnDefs" fix-header>
<thead>
<tr>
<th>Name </th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in items">
<td>{{item.name}} </td>
</tr>
</tbody>
</table>
但是当我 运行 我的应用程序时,我收到以下错误,
TypeError: Cannot read property 'childNodes' of undefined
我也尝试使用以下指令,因为我知道 DOM 操作不应在 Controller 中完成,但我收到以下错误。
TypeError: Cannot set property '_oPluginFixedHeader' of undefined
更新:
我的指令
app.directive('fixHeader', function () {
return {
restrict: 'AE', //attribute or element
replace: true,
link: function ($scope, elem, attr) {
$scope.table = elem.find("#example");
console.log($scope.table);
var table= $scope.table.dataTable();
new $.fn.dataTable.FixedHeader(table);
}
};
});
有人可以提出更好的解决方案吗?
我正在使用 dataTables.fixedHeader.js 的 2.1.2 版本,我的错误出现在
行下方
var dt = $.fn.dataTable.Api ?
new $.fn.dataTable.Api( mTable ).settings()[0] :
mTable.fnSettings();
dt._oPluginFixedHeader = this; //error over here
angular-datatables
还为您的 Angular 应用程序提供 FixedHeader works with datatables. You need to add the files
angular-datatables.fixedheader.min.jsto your HTML. You also need to add the dependency
datatables.fixedheader` 插件.那么您的代码将如下所示。
HTML
<table datatable="" dt-options="dtOptions" dt-columns="dtColumns" class="row-border">
<thead>
<tr>
<th>Name </th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in items">
<td>{{item.name}} </td>
</tr>
</tbody>
</table>
代码
var app = angular.module('app', ['datatables', 'datatables.fixedheader'])
app.controller("SampleController", function($scope, DTOptionsBuilder, DTColumnBuilder) {
$scope.dtOptions = DTOptionsBuilder.fromSource('data.json')
.withPaginationType('full_numbers')
.withDisplayLength(25)
.withFixedHeader({
bottom: true
});
$scope.dtColumns = [
DTColumnBuilder.newColumn('id').withTitle('ID'),
DTColumnBuilder.newColumn('Name').withTitle('Name'),
DTColumnBuilder.newColumn('Address').withTitle('Address')
];
});
不需要添加指令,因为angular-datatables.fixedheader.min.js
(Reference Link)
里面已经创建了指令
更新:
需要在代码中进行以下更改。
- 将
FixedHeader
js代码&jquery代码替换为new $.fn.dataTable.FixedHeader($element.find('#example'))
- 调用将设置
FixedHeader
的方法
控制器
(function(angular) {
angular.module('showcase.angularWay', ['datatables'])
.controller('AngularWayCtrl', AngularWayCtrl);
function AngularWayCtrl($http, $element) {
var vm = this;
$http.get('data.json').then(function(res) {
vm.persons = res.data;
vm.setFixedHeader(); //call method to set glass.
});
vm.setFixedHeader = function(){
//var table = $element.find('#example').DataTable();
new $.fn.dataTable.FixedHeader($element.find('#example'))
};
}
})(angular);
您也可以将此代码移动到指令中,只需要删除作为额外 header.
附加的 header 列
我参考了 this link 以上更新的解决方案
我使用的是固定的稳定构建版本Stable release v2.1.2 header jquery plugin. I am trying to fix my table header. I have created the table using Angular datatables as mentioned over here.
在我的控制器中class我写了下面的代码,
app.controller("SampleController", function ($scope) {
$(document).ready( function () {
var table = $('#example').DataTable();
new $.fn.dataTable.FixedHeader( table );
});
});
我的HTML如下,
<table id="example" datatable="ng" dt-options="dtOptions" dt-column-defs="dtColumnDefs" fix-header>
<thead>
<tr>
<th>Name </th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in items">
<td>{{item.name}} </td>
</tr>
</tbody>
</table>
但是当我 运行 我的应用程序时,我收到以下错误,
TypeError: Cannot read property 'childNodes' of undefined
我也尝试使用以下指令,因为我知道 DOM 操作不应在 Controller 中完成,但我收到以下错误。
TypeError: Cannot set property '_oPluginFixedHeader' of undefined
更新:
我的指令
app.directive('fixHeader', function () {
return {
restrict: 'AE', //attribute or element
replace: true,
link: function ($scope, elem, attr) {
$scope.table = elem.find("#example");
console.log($scope.table);
var table= $scope.table.dataTable();
new $.fn.dataTable.FixedHeader(table);
}
};
});
有人可以提出更好的解决方案吗?
我正在使用 dataTables.fixedHeader.js 的 2.1.2 版本,我的错误出现在
行下方 var dt = $.fn.dataTable.Api ?
new $.fn.dataTable.Api( mTable ).settings()[0] :
mTable.fnSettings();
dt._oPluginFixedHeader = this; //error over here
angular-datatables
还为您的 Angular 应用程序提供 FixedHeader works with datatables. You need to add the files
angular-datatables.fixedheader.min.jsto your HTML. You also need to add the dependency
datatables.fixedheader` 插件.那么您的代码将如下所示。
HTML
<table datatable="" dt-options="dtOptions" dt-columns="dtColumns" class="row-border">
<thead>
<tr>
<th>Name </th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in items">
<td>{{item.name}} </td>
</tr>
</tbody>
</table>
代码
var app = angular.module('app', ['datatables', 'datatables.fixedheader'])
app.controller("SampleController", function($scope, DTOptionsBuilder, DTColumnBuilder) {
$scope.dtOptions = DTOptionsBuilder.fromSource('data.json')
.withPaginationType('full_numbers')
.withDisplayLength(25)
.withFixedHeader({
bottom: true
});
$scope.dtColumns = [
DTColumnBuilder.newColumn('id').withTitle('ID'),
DTColumnBuilder.newColumn('Name').withTitle('Name'),
DTColumnBuilder.newColumn('Address').withTitle('Address')
];
});
不需要添加指令,因为angular-datatables.fixedheader.min.js
(Reference Link)
更新:
需要在代码中进行以下更改。
- 将
FixedHeader
js代码&jquery代码替换为new $.fn.dataTable.FixedHeader($element.find('#example'))
- 调用将设置
FixedHeader
的方法
控制器
(function(angular) {
angular.module('showcase.angularWay', ['datatables'])
.controller('AngularWayCtrl', AngularWayCtrl);
function AngularWayCtrl($http, $element) {
var vm = this;
$http.get('data.json').then(function(res) {
vm.persons = res.data;
vm.setFixedHeader(); //call method to set glass.
});
vm.setFixedHeader = function(){
//var table = $element.find('#example').DataTable();
new $.fn.dataTable.FixedHeader($element.find('#example'))
};
}
})(angular);
您也可以将此代码移动到指令中,只需要删除作为额外 header.
附加的 header 列我参考了 this link 以上更新的解决方案