Skip to content

Commit

Permalink
Merge branch 'VanDalkvist-dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
l-lin committed Jul 11, 2014
2 parents c3f21cb + f1e5038 commit 60b14cb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 27 deletions.
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,29 @@ Declare dependencies on your module app like this:
angular.module('myModule', ['datatables']);
```

Usage
-----

See [github page](https://l-lin.github.io/angular-datatables).

Additional notes
----------------

This module does not support multiple datatables in the same page YET!
Each time a datatable is rendered, a message is sent to the parent scopes with the id of the table.

Usage
-----
For instance, for the given dataTable:

See [github page](https://l-lin.github.io/angular-datatables).
```html
<table id="foobar" datatable dt-options="dtOptions" dt-columns="dtColumns"></table>
```

You can catch the event like this in your parent directive or controller:

```js
$scope.$on('event:dataTableLoaded', function(event, loadedDT) {
// loadedDT === {"id": "foobar"}
});
```

License
================
Expand Down
27 changes: 16 additions & 11 deletions dist/angular-datatables.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,16 @@
$elem.show();
$loading.hide();
};
var _doRenderDataTable = function ($elem, options) {
var _renderDataTableAndEmitEvent = function ($elem, options, $scope) {
var oTable = $elem.DataTable(options);
$scope.$emit('event:dataTableLoaded', { id: $elem.attr('id') });
return oTable;
};
var _doRenderDataTable = function ($elem, options, $scope) {
// Add $timeout to be sure that angular has finished rendering before calling datatables
$timeout(function () {
_hideLoading($elem);
$elem.DataTable(options);
_renderDataTableAndEmitEvent($elem, options, $scope);
}, 0, false);
};
/**
Expand Down Expand Up @@ -258,7 +263,7 @@
return {
options: options,
render: function ($scope, $elem) {
_doRenderDataTable($elem, this.options);
_doRenderDataTable($elem, this.options, $scope);
}
};
};
Expand All @@ -274,7 +279,7 @@
render: function ($scope, $elem) {
var _this = this;
$scope.$on(DT_LAST_ROW_KEY, function () {
_doRenderDataTable($elem, _this.options);
_doRenderDataTable($elem, _this.options, $scope);
});
}
};
Expand All @@ -287,7 +292,7 @@
*/
var PromiseRenderer = function (options) {
var oTable;
var _render = function (options, $elem, data) {
var _render = function (options, $elem, data, $scope) {
options.aaData = data;
// Add $timeout to be sure that angular has finished rendering before calling datatables
$timeout(function () {
Expand All @@ -300,7 +305,7 @@
oTable.fnDraw();
oTable.fnAddData(options.aaData);
} else {
oTable = $elem.DataTable(options);
oTable = _renderDataTableAndEmitEvent($elem, options, $scope);
}
}, 0, false);
};
Expand All @@ -310,7 +315,7 @@
var _this = this;
var _loadedPromise = null;
var _whenLoaded = function (data) {
_render(_this.options, $elem, data);
_render(_this.options, $elem, data, $scope);
_loadedPromise = null;
};
var _startLoading = function (fnPromise) {
Expand Down Expand Up @@ -355,7 +360,7 @@
*/
var AjaxRenderer = function (options) {
var oTable;
var _render = function (options, $elem) {
var _render = function (options, $elem, $scope) {
// Set it to true in order to be able to redraw the dataTable
options.bDestroy = true;
// Add $timeout to be sure that angular has finished rendering before calling datatables
Expand All @@ -374,7 +379,7 @@
throw new Error('Reload Ajax not supported. Please use the plugin "fnReloadAjax" (https://next.datatables.net/plug-ins/api/fnReloadAjax) or use a more recent version of DataTables (v1.10+)');
}
} else {
oTable = $elem.DataTable(options);
oTable = _renderDataTableAndEmitEvent($elem, options, $scope);
}
}, 0, false);
};
Expand All @@ -392,12 +397,12 @@
$scope.$watch('dtOptions.sAjaxSource', function (sAjaxSource) {
_this.options.sAjaxSource = sAjaxSource;
_this.options.ajax = sAjaxSource;
_render(options, $elem);
_render(options, $elem, $scope);
});
$scope.$watch('dtOptions.reload', function (reload) {
if (reload) {
$scope.dtOptions.reload = false;
_render(options, $elem);
_render(options, $elem, $scope);
}
});
}
Expand Down
Loading

0 comments on commit 60b14cb

Please sign in to comment.