3

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 comes in below line

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

    dt._oPluginFixedHeader = this; //error over here
Parth Doshi
  • 4,200
  • 15
  • 79
  • 129

1 Answers1

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

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
  • hi Pankaj. Have you tried this with datatable = "ng". Since you have written datatable =" " in your code that means it is jquery datatable. I need it to work using angular way – Parth Doshi Mar 06 '15 at 04:15
  • When I tried it. I get another clone of my header which is fixed. The normal table header is still there. – Parth Doshi Mar 06 '15 at 04:17
  • 3
    Here an [example](http://codepen.io/l-lin/pen/xbawbm?editors=101) using the "Angular way". – l-lin Mar 06 '15 at 11:22
  • @l-lin: Even i tried the same but for some reason it creates a clone of my datatable header and hence although my header gets fixed but i can another portion of my table header moving across the page. – Parth Doshi Mar 08 '15 at 11:42
  • 1
    yes it is working with your example. If any problems. I will let u know – Parth Doshi Mar 15 '15 at 11:59
  • @ParthDoshi now the bounty also gets end :( – Pankaj Parkar Mar 16 '15 at 11:41
  • @pankajparkar: In your plunker, i see the header is fixed but it is also cloned. How to remove the extra header ? Also the sorting does not work in your plunker. Also, I will start a new bounty and provide you the points for the same since you have helped me solve this – Parth Doshi Mar 16 '15 at 16:30
  • hi can you email me the fix regarding the cloned header..my email is androidparth@gmail.com – Parth Doshi Mar 17 '15 at 16:32
  • @ParthDoshi I going to work on it, but I don't think so you would care if i solve your issue, You really don't care who helped you.. – Pankaj Parkar Mar 17 '15 at 17:10
  • no it is not like that. i really appreciate your work. i think there is some misunderstanding. i am sorry if i did a mistake – Parth Doshi Mar 17 '15 at 17:11
  • let it be..As I'm always ready to help..I'll let you know if i can fix your mentioned issue – Pankaj Parkar Mar 17 '15 at 17:14
  • hi @pankajparkar: did u get any solution for the cloning of header? – Parth Doshi Mar 19 '15 at 16:35
  • I didn't get a time look at it.I'll look at it afterwards – Pankaj Parkar Mar 19 '15 at 17:17