dt-instance directive not working for controllerAs
vm.dtInstance is not set after the table is rendered -- all other vm.<whatever'> work
html
<div class="vers1 v1-booklets-booklets" ng-controller="BookletsBookletsListCtrl as vm">
<table id="v1-booklets-booklets" datatable="" dt-options="vm.dtOptions" dt-columns="vm.dtColumns" class="{{vm.dtClasses}}" dt-instance="vm.dtInstance"></table>
<div class="hide-remove"><action-icon-set id="TAB-Booklets" class="dataTables_actionIcons" data-entity-id="41" data-entity-type="booklet" actions="refreshTable,newBooklet"/></div>
</div>
javascript
vm.dtInstance.reloadData();
TypeError: Cannot read property 'reloadData' of undefined at booklets-booklets-list.js:94 at Scope.parent.$get.Scope.$emit (angular.js:14793) at angular-action-icons.js:117
I have to use the old method -- and I get warned
angular.js:11699 "DTInstances.getLast()" and "DTInstances.getList()" are deprecated! Use the "dt-instance" to provide the datatables instance. See https://l-lin.github.com/angular-datatables/#/manipulatingDTInstances for more information.
Try to use Fetching DataTable Instances.
You can implement it:
angular.module('myApp', []).controller('myController', [function () {
vm.dtInstance = {};
vm.dtOptions = ...;
vm.dtColumns = ...;
vm.dtIntanceCallback = function (instance) {
vm.dtInstance = instance;
}
// You should be able to get the table instance
console.log(vm.dtInstance);
}]);
And in your html...
<table
datatable
dt-options="vm.dtOptions"
dt-columns="vm.dtColumns"
dt-instance="vm.dtInstanceCallback"
class="row-border hover">
</table>
Hope this helps...
I figured out how to request the dev branch -- and now all injections of DTInstances are flagged as errors
but neither the dt-instance nor the callback works -- I'd rather use the variable based directive...
the callback never gets called...
in my html
<div class="vers1">
<div class="vers1 v1-branches-branches" ng-controller="BranchesBranchesListCtrl as vm">
<table id="v1-branches-branches" datatable="" dt-options="vm.dtOptions" dt-columns="vm.dtColumns" class="{{vm.dtClasses}}" dt-instance="vm.dtInstanceCallback"></table>
<div class="hide-remove"><action-icon-set id="TAB-Branches" class="dataTables_actionIcons" data-entity-id="71" data-entity-type="branch" actions="refreshTable,newBranch"/></div>
</div>
</div>
in the javascript [controller] the callback never gets called -- but the table comes up and everything else works fine!
'use strict';
(function () {
function controllerFn ($scope, $log, $compile, $rootScope, $http, $resource, locker, ENV, DTColumnBuilder, actionIcons, tablesSettings, errorHandling, bookletsInMemory) {
var vm = this; // jshint ignore: line
vm.dataPromise = function() {
return $resource(ENV.backend+'/booklet').query(postProcess,errorHandling.handleServerError).$promise;
};
// [...]
vm.dtColumns = [
DTColumnBuilder.newColumn('id').withTitle('Id').notVisible().withClass('dt-cell-right booklet-id'),
DTColumnBuilder.newColumn('scriptName').withTitle('Name').withClass('dt-cell-right booklet-name'),
DTColumnBuilder.newColumn('actions').withTitle('Actions').withClass('dt-cell-right booklet-actions').withOption('sWidth', '60px').notSortable().renderWith(tablesSettings.asEntityIcons('booklet')),
];
function initCompleteFn ( settings, json ) {
$('#'+vm.actionBtns).prependTo( $('#'+vm.tableId+'_wrapper') );
// find our datatable instance -- will be obsolete soon
// DTInstances.getList().then(function(instanceList){
// if (instanceList.hasOwnProperty(vm.tableId)) {
// vm.dtInstance = instanceList[vm.tableId];
// }
// });
}
vm.dtOptions = DTOptionsBuilder.fromFnPromise(vm.dataPromise)
.withOption('createdRow', (createdRow || $.noop) )
.withOption('bAutoWidth', false)
.withOption('bPaginate', false)
.withOption('deferRender', true)
.withScroller()
.withOption('scrollY', '350')
.withOption('scrollX', false)
.withOption('initComplete', (initCompleteFn || $.noop) )
.withOption('rowCallback', vm.rowCallback);
vm.rowCallback = function(nRow, aData ) { //, iDisplayIndex, iDisplayIndexFull) {
// Unbind first in order to avoid any duplicate handler (see https://github.com/l-lin/angular-datatables/issues/87)
$('td', nRow).unbind('click');
$('td', nRow).bind('click', function() {
$scope.$apply(function() {
vm.rowClicked(aData);
});
});
return nRow;
};
vm.dtInstance = {};
vm.dtInstanceCallback = function(_dtInstance){
$log.debug(_dtInstance);
vm.dtInstance = _dtInstance;
};
// [...]
}
angular.module('build1App')
.controller('BookletsBookletsListCtrl', controllerFn);
})();
I'm going to have to go back to the master branch and suffer the warnings until the next update I guess
I had the same problem. I looked at the code and found the problem:
function _setDTInstance(dtInstance) {
if (angular.isFunction($scope.dtInstance)) {
$scope.dtInstance(dtInstance);
} else if (angular.isDefined($scope.dtInstance)) {
$scope.dtInstance = dtInstance;
}
}
The code checks to see if the variable is defined, before setting it. In order to fix it, you need to initialize your vm.dtInstance variable to null. After that, it works for me.
Seems like the code shouldn't care if the variable is defined or not.
finally had a chance to come back to this and this worked - master branch - 0.4.3
I think it should be revisited. I'm dealing with this now exactly as described above. The dt-instance directive does not work even if I specify the variable in my controller and instantiate it to a new object or set it to null. I don't know where it gets clobered but at the point that the _setDTInstance is called, no matter what I do in my controller, scope always has an undefined dtInstance variable. I can understand testing for valid callback but otherwise it should be injected into scope. If injection of DTInstance is meant to be automatic, then as a consumer of the API I should not have to know any of the internal details. Otherwise injection should be done on demand by setting a table option or a promise that we can resolve post rendering. I will try to reproduce this in plnkr later on and/or update my comment but this should be addressed. Thanks.
Hi,
Is there a resolution to this? No matter what I do I alway get an error...
'vm.dtInstance.reloadData is not a function'.
Thanks Delordson
:+1:
I can't reproduce your issue. Can you make a plnkr or something alike that reproduce this issue?
Yes this does not work for me either!
Doesn't work for me either. Anyone found a solution?
This is the solution:
js: vm.dtInstance = {};
vm.dtInstanceCallback = function(_dtInstance) { vm.dtInstance = _dtInstance; vm.dtInstance.reloadData(); //or something else.... }
html: dt-instance="vm.dtInstanceCallback"
Great dashawk dtInstance callback Worked for me !!! Thank you
solution for 'vm.dtInstance.reloadData is not a function'.
if(!angular.equals(vm.dtInstance, {})){ vm.dtInstance.reloadData(); }
reason for error: vm.dtInstance = {};