Datatables : How to get pagination working when table HTML is compiled with Angular after drawCallBack?

六眼飞鱼酱① 提交于 2019-12-25 05:37:09

问题


This is my drawCallBack function :

"fnDrawCallback": function( oSettings ) {
            console.log("dt redrawn");
            var selector = $("#example_table");
            console.log(selector);

            function recompileForAngular() {
                angular.element(document).injector().invoke(['$compile', function ($compile) {
                    // Create a scope.
                    var $scope = angular.element(document.body).scope();
                    // Specify what it is we'll be compiling.
                    var to_compile = $(selector).html();
                    // Compile the tag, retrieving the compiled output.
                    var $compiled = $compile(to_compile)($scope);
                    // Ensure the scope and been signalled to digest our data.
                    $scope.$digest();
                    // Replace the compiled output to the page.
                    $(selector).html($compiled);
                }]);
            }

            function init(recompile) {
                recompile();
            }

            init(recompileForAngular);
        },

This is working fine when data table is loaded, but upon clicking on another page (e.g. 2nd page), the table HTML is not getting refreshed with the new data returned from AJAX call.

It seems like angular is compiling old HTML which it gets from $("#example_table").html();.

What's the way to capture the event when the rendering is complete (so that I can re-compile new, freshly rendered HTML)?


回答1:


Changing the selector, targeting the table body solves this problem. var selector = $("#exampleTable tbody");



来源:https://stackoverflow.com/questions/42648788/datatables-how-to-get-pagination-working-when-table-html-is-compiled-with-angu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!