Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Add the possibility to provide an object or a callback function to fetch the DT instance #307

Closed
l-lin opened this issue May 8, 2015 · 7 comments
Milestone

Comments

@l-lin
Copy link
Owner

l-lin commented May 8, 2015

Instead of using DTInstances service (which caused more headaches than expected 😞 ), one solution is to create another directive dt-instance in which it will be possible to provide a variable that angular-datatables will populate with the DT directive instance.
One example:

<div ng-controller="ShowCaseCtrl as showCase">
    <table datatable
           dt-options="showCase.dtOptions"
           dt-instance="showCase.dtInstance">
    </table>
</div>
angular.controller('ShowCaseCtrl ', ShowCaseCtrl );
function ShowCaseCtrl(DTOptionsBuilder) {
    var vm = this;
    vm.dtInstance = {}; // This will be set automatically with the two-way data binding
    vm.dtOptions = DTOptionsBuilder.fromSource('data.json');
}

The user should also have the possibility to set a callback function instead of a variable:

<div ng-controller="ShowCaseCtrl as showCase">
    <table datatable
           dt-options="showCase.dtOptions" 
           dt-instance="showCase.dtInstanceCallback">
    </table>
</div>
angular.controller('ShowCaseCtrl ', ShowCaseCtrl );
function ShowCaseCtrl(DTOptionsBuilder) {
    var vm = this;
    vm.dtInstances = [];
    vm.dtOptions = DTOptionsBuilder.fromSource('data.json');
    vm.dtInstanceCallback = dtInstanceCallback;

    function dtInstanceCallback(dtInstance) {
        vm.dtInstances.push(dtInstance);
    }
}

Note: This enhancement will completely replace the DTInstances service. So expect the removal of the DTInstances service in the v0.5.0+ versions!!!

@MowWow
Copy link

MowWow commented May 8, 2015

Sorry as i'm not familiar with github, where can i find the new code version containing this bug fix ?

Big thanks!

@MowWow
Copy link

MowWow commented May 8, 2015

Forget my comment i found the new code thx

@terry-herckenrath
Copy link

I found this thread when I ran into this issue trying to include multiple DataTables on one page.

I followed your advice regarding adding the dt-instance attribute to my table, but it did not fix the problem for me.

After some digging through the code I found out why it wasn't working for me: I'm using the Ajax Renderer rather than the Angular Renderer and your fix was only applied to the Angular Renderer.

I replicated the changes in the Ajax Renderer and it now works fine for me.

Original code (line 1162 in angular-datatables.js v0.4.3):

        function rerender() {
            _oTable.destroy();
            DTRendererService.showLoading(_$elem);
            render(_$elem);
        }

Modified code:

        function rerender() {
            _oTable.ngDestroy();
            DTRendererService.showLoading(_$elem);
            $timeout(function() {
                var result = DTRendererService.hideLoadingAndRenderDataTable(_$elem, renderer.options);
                _oTable = result.DataTable;
                dtInstance = DTInstances.register(dtInstance, result);
            }, 0, false);
        }

@l-lin
Copy link
Owner Author

l-lin commented Aug 28, 2015

It should work even with the Ajax renderer. See this example.

@1antares1
Copy link

Thankfully l-lin!... Sincerely.

@Rocky-L
Copy link

Rocky-L commented May 8, 2017

I followed lin's code and trying to use

vm.dtInstace.reloadData()

I've got this warning and the data is not reloaded.

The Angular Renderer does not support reloading data. You need to do it directly on your model

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants