【问题标题】:Fix header plugin not working in Angular datatables修复标头插件在 Angular 数据表中不起作用
【发布时间】:2015-05-07 03:46:41
【问题描述】:

我正在使用固定标头 jquery plugin 的稳定版本稳定版本 v2.1.2。我正在尝试修复我的表头。我已经使用 here 中提到的 Angular 数据表创建了表格。

在我的控制器类中,我编写了以下代码,

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:无法读取未定义的属性“childNodes”

我也尝试使用以下指令,因为我知道不应在 Controller 中进行 DOM 操作,但我收到以下错误。

TypeError: 无法设置未定义的属性“_oPluginFixedHeader”

更新:

我的指令

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.js2.1.2 版本,我的错误出现在下面一行

 var dt = $.fn.dataTable.Api ?
        new $.fn.dataTable.Api( mTable ).settings()[0] :
        mTable.fnSettings();

    dt._oPluginFixedHeader = this; //error over here

【问题讨论】:

  • 能否请您上传您的代码或解决问题?
  • 请提供完整代码以阐明确切问题。

标签: javascript jquery angularjs datatable fixed-header-tables


【解决方案1】:

angular-datatables 还为您的 Angular 应用程序提供了 FixedHeader works with datatables. You need to add the filesangular-datatables.fixedheader.min.jsto your HTML. You also need to add the dependencydatatables.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.jsReference Link)内部已经创建了指令

更新:

需要在代码中进行以下更改。

  1. FixedHeaderjs代码和jquery代码替换为new $.fn.dataTable.FixedHeader($element.find('#example'))
  2. 调用将设置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);

您也可以将此代码移动到指令中,只需删除作为额外标题附加的标题列。

我参考了this link 以获得上述更新的解决方案

Working Plunkr

【讨论】:

  • 嗨,Pankaj。你有没有用 datatable = "ng" 试过这个。由于您在代码中编写了 datatable =" ",这意味着它是 jquery 数据表。我需要它使用角度方式工作
  • 当我尝试过时。我得到了我的标题的另一个克隆,它是固定的。正常的表头仍然存在。
  • 这里是example,使用“Angular 方式”。
  • @l-lin: 即使我尝试了相同的方法,但由于某种原因,它创建了我的数据表标题的克隆,因此虽然我的标题得到修复,但我可以在页面上移动我的表标题的另一部分.
  • 是的,它正在与您的示例一起使用。如果有任何问题。我会告诉你的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-16
  • 1970-01-01
  • 2014-05-03
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多