最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Fix header plugin not working in Angular datatables - Stack Overflow

matteradmin3PV0评论

I am using the stable build version Stable release v2.1.2 of the fixed header jquery plugin. I am trying to fix my table header. I have created the table using Angular datatables as mentioned over here.

In my controller class I have written the following code,

app.controller("SampleController", function ($scope) {

    $(document).ready( function () {
        var table = $('#example').DataTable();
        new $.fn.dataTable.FixedHeader( table );
    });

});

My HTML is as follows,

 <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>

However when i run my application, i get the following error,

TypeError: Cannot read property 'childNodes' of undefined

I also tried using the following directive as I am aware that DOM manipulations should not be done in Controller but I get the following error.

TypeError: Cannot set property '_oPluginFixedHeader' of undefined

UPDATE:

my directive

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); 
        }
    };
});

Please can someone suggest a better solution?

I am using version 2.1.2 of dataTables.fixedHeader.js and my error es in below line

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

    dt._oPluginFixedHeader = this; //error over here

I am using the stable build version Stable release v2.1.2 of the fixed header jquery plugin. I am trying to fix my table header. I have created the table using Angular datatables as mentioned over here.

In my controller class I have written the following code,

app.controller("SampleController", function ($scope) {

    $(document).ready( function () {
        var table = $('#example').DataTable();
        new $.fn.dataTable.FixedHeader( table );
    });

});

My HTML is as follows,

 <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>

However when i run my application, i get the following error,

TypeError: Cannot read property 'childNodes' of undefined

I also tried using the following directive as I am aware that DOM manipulations should not be done in Controller but I get the following error.

TypeError: Cannot set property '_oPluginFixedHeader' of undefined

UPDATE:

my directive

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); 
        }
    };
});

Please can someone suggest a better solution?

I am using version 2.1.2 of dataTables.fixedHeader.js and my error es in below line

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

    dt._oPluginFixedHeader = this; //error over here
Share Improve this question edited Mar 6, 2015 at 6:45 Parth Doshi asked Mar 5, 2015 at 17:03 Parth DoshiParth Doshi 4,20815 gold badges81 silver badges130 bronze badges 2
  • could you please upload your code or create fiddle with problem? – Pankaj Parkar Commented Mar 13, 2015 at 16:48
  • Please provide a full code to clarify the exact issue. – ProllyGeek Commented Mar 14, 2015 at 23:45
Add a ment  | 

1 Answer 1

Reset to default 3

The angular-datatables also provides plugin to FixedHeader works with datatables. You need to add the filesangular-datatables.fixedheader.min.jsto your HTML. You also need to add the dependencydatatables.fixedheader` to your Angular app. then your code will look like below.

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>

CODE

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')
    ];
});

No need of adding directive, because there is already directive created inside angular-datatables.fixedheader.min.js (Reference Link)

Update:

Below changes need to be done in code.

  1. Replace FixedHeader js code & jquery code to new $.fn.dataTable.FixedHeader($element.find('#example'))
  2. Call that method which will set FixedHeader

Controller

(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);

You can also move this code to directive, only you need to remove header column which is appended as extra header.

I referred this link for above updated solution

Working Plunkr

Post a comment

comment list (0)

  1. No comments so far