How to call the destroy function of angular datatables?

前端 未结 1 2023
借酒劲吻你
借酒劲吻你 2021-01-14 12:32

I have a controller and I want to call the destroy function of Jquery Datatables in the controller in a watch method:

      $scope.$watch(\'model.SelectedWai         


        
相关标签:
1条回答
  • 2021-01-14 12:40

    With dtInstance you have access to the dataTables API :

    $scope.dtInstance = {};
    

    add dtInstance as declaration to the table

    <table datatable dt-instance="dtInstance" dt-options="dtOptions" dt-columns="dtColumns">
    

    Now you can destroy the dataTable with

    $scope.dtInstance.DataTable.destroy();
    

    angular dataTables have a extended ngDestroy() cleaning up bindings made by itself :

    $scope.dtInstance.DataTable.ngDestroy();
    

    There is still some style (and a little bit more garbage left) in the headers, so remove them too (here on a table with the id #table) :

    $scope.destroy = function() {
        $scope.dtInstance.DataTable.ngDestroy();
        var i, ths = document.querySelectorAll('#table th');
           for (i=0;i<ths.length;i++) {
              ths[i].removeAttribute('style'); 
           }
        }
    }
    

    demo -> http://plnkr.co/edit/fQ9YjsbNBNzyYuuvpk6T?p=preview

    If you have multiple angular dataTables, use multiple dtInstances and different table id's.

    0 讨论(0)
提交回复
热议问题