|
364 | 364 | 'datatables.options'
|
365 | 365 | ]).directive('datatable', [
|
366 | 366 | 'DT_DEFAULT_OPTIONS',
|
367 |
| - '$timeout', |
368 | 367 | '$DTBootstrap',
|
369 | 368 | 'DTRendererFactory',
|
370 |
| - function (DT_DEFAULT_OPTIONS, $timeout, $DTBootstrap, DTRendererFactory) { |
| 369 | + function (DT_DEFAULT_OPTIONS, $DTBootstrap, DTRendererFactory) { |
371 | 370 | return {
|
372 | 371 | restrict: 'A',
|
373 | 372 | scope: {
|
|
376 | 375 | dtColumnDefs: '=',
|
377 | 376 | datatable: '@'
|
378 | 377 | },
|
379 |
| - link: function ($scope, $elem) { |
380 |
| - DTRendererFactory.showLoading($elem); |
381 |
| - // Build options |
382 |
| - var isNgDisplay = $scope.datatable && $scope.datatable === 'ng', options; |
383 |
| - if (angular.isDefined($scope.dtOptions)) { |
384 |
| - options = {}; |
385 |
| - angular.extend(options, $scope.dtOptions); |
386 |
| - // Set the columns |
387 |
| - if (angular.isArray($scope.dtColumns)) { |
388 |
| - options.aoColumns = $scope.dtColumns; |
389 |
| - } |
390 |
| - // Set the column defs |
391 |
| - if (angular.isArray($scope.dtColumnDefs)) { |
392 |
| - options.aoColumnDefs = $scope.dtColumnDefs; |
393 |
| - } |
394 |
| - // Integrate bootstrap (or not) |
395 |
| - if (options.integrateBootstrap) { |
396 |
| - $DTBootstrap.integrate(options); |
397 |
| - } else { |
398 |
| - $DTBootstrap.deIntegrate(); |
399 |
| - } |
| 378 | + compile: function (tElm) { |
| 379 | + var _staticHTML = tElm[0].innerHTML; |
| 380 | + return function postLink($scope, $elem, iAttrs, ctrl) { |
| 381 | + ctrl.showLoading($elem); |
| 382 | + ctrl.render($elem, ctrl.buildOptions(), _staticHTML); |
| 383 | + }; |
| 384 | + }, |
| 385 | + controller: [ |
| 386 | + '$scope', |
| 387 | + function ($scope) { |
| 388 | + this.showLoading = function ($elem) { |
| 389 | + DTRendererFactory.showLoading($elem); |
| 390 | + }; |
| 391 | + this.buildOptions = function () { |
| 392 | + // Build options |
| 393 | + var options; |
| 394 | + if (angular.isDefined($scope.dtOptions)) { |
| 395 | + options = {}; |
| 396 | + angular.extend(options, $scope.dtOptions); |
| 397 | + // Set the columns |
| 398 | + if (angular.isArray($scope.dtColumns)) { |
| 399 | + options.aoColumns = $scope.dtColumns; |
| 400 | + } |
| 401 | + // Set the column defs |
| 402 | + if (angular.isArray($scope.dtColumnDefs)) { |
| 403 | + options.aoColumnDefs = $scope.dtColumnDefs; |
| 404 | + } |
| 405 | + // Integrate bootstrap (or not) |
| 406 | + if (options.integrateBootstrap) { |
| 407 | + $DTBootstrap.integrate(options); |
| 408 | + } else { |
| 409 | + $DTBootstrap.deIntegrate(); |
| 410 | + } |
| 411 | + } |
| 412 | + return options; |
| 413 | + }; |
| 414 | + this.render = function ($elem, options, staticHTML) { |
| 415 | + var isNgDisplay = $scope.datatable && $scope.datatable === 'ng'; |
| 416 | + // Render dataTable |
| 417 | + DTRendererFactory.fromOptions(options, isNgDisplay).render($scope, $elem, staticHTML); |
| 418 | + }; |
400 | 419 | }
|
401 |
| - // Render dataTable |
402 |
| - DTRendererFactory.fromOptions(options, isNgDisplay).render($scope, $elem); |
403 |
| - } |
| 420 | + ] |
404 | 421 | };
|
405 | 422 | }
|
406 | 423 | ]).directive('dtRows', [
|
|
858 | 875 | 'datatables.options'
|
859 | 876 | ]).factory('DTRendererFactory', [
|
860 | 877 | '$timeout',
|
| 878 | + '$compile', |
861 | 879 | 'DTLoadingTemplate',
|
862 | 880 | 'DT_DEFAULT_OPTIONS',
|
863 |
| - function ($timeout, DTLoadingTemplate, DT_DEFAULT_OPTIONS) { |
| 881 | + function ($timeout, $compile, DTLoadingTemplate, DT_DEFAULT_OPTIONS) { |
864 | 882 | var $loading = angular.element(DTLoadingTemplate.html), _showLoading = function ($elem) {
|
865 | 883 | $loading.show();
|
866 | 884 | $elem.after($loading);
|
|
915 | 933 | var NGRenderer = function (options) {
|
916 | 934 | return {
|
917 | 935 | options: options,
|
918 |
| - render: function ($scope, $elem) { |
| 936 | + render: function ($scope, $elem, staticHTML) { |
919 | 937 | var _this = this, expression = $elem.find('tbody').html(),
|
920 | 938 | // Find the resources from the comment <!-- ngRepeat: item in items --> displayed by angular in the DOM
|
921 | 939 | // This regexp is inspired by the one used in the "ngRepeat" directive
|
|
927 | 945 | parentScope.$watchCollection(ngRepeatAttr, function () {
|
928 | 946 | if (oTable && alreadyRendered && !_isDTOldVersion(oTable)) {
|
929 | 947 | oTable.ngDestroy();
|
| 948 | + // Re-compile because we lost the angular binding to the existing data |
| 949 | + $elem.html(staticHTML); |
| 950 | + $compile($elem.contents())(parentScope); |
930 | 951 | }
|
931 | 952 | // This condition handles the case the array is empty
|
932 | 953 | if (firstCall) {
|
|
0 commit comments