javascript - jQuery DataTables button trigger after load results in an infinite loop -
i have datatable can have columns filtered based on user checks.
i want user able export see on datatable. have data-column
attribute on each checkbox lets me know column should shown or hidden based on whether or not checked.
i have initialized table this:
var table = $('.my-table').datatable({ pagelength:25, fixedheader: true, sscrollx: true, dom: '<"html5buttons"b>ltfgitp', buttons: [ {extend: 'copy'}, {extend: 'csv', title: 'testing', exportoptions: { columns: get_columns_to_export(), rows: { selected: true } }, 'customize': function(doc){ console.log("==csv doc=="); console.log(doc); } }, {extend: 'excel', title: 'testing', exportoptions: { columns: get_columns_to_export(), rows: { selected: true } }, 'customize': function(doc){ console.log("==excel doc=="); var sheet = doc.xl.worksheets['sheet1.xml']; console.log(sheet); } }, {extend: 'print', customize: function (win){ $(win.document.body).addclass('white-bg'); $(win.document.body).css('font-size', '10px'); $(win.document.body).find('table') .addclass('compact') .css('font-size', 'inherit'); } } ] });
the function get_columns_to_export
returns numbered list of columns visible, , works, on page load.
if unhide columns (so get_columns_to_export()
returns different array containing number), exported file contain original columns found on table load.
i looked how trigger button again, running issues datatables has infinite loop before js runs out of stack size.
from datatables documentation, i'm using button.trigger()
found here following:
$('.buttons-excel').on('click',function(){ table.button('.buttons-excel').trigger(); });
this causes infinite loop occur. i've tried adding return
statement after trigger action, reason ignores , goes directly original customize
function declared in original declaration of table
.
what can update datatables correct column numbers while not ending in infinite loop?
change exportoptions sections be
exportoptions: { columns: get_columns_to_export.bind(this), rows: { selected: true } },
datatables column selectors can take various options, among them string or function.
in original example, calling function get_columns_to_export() once @ compile time, , function returns initial state of columns being assigned column selector.
because columns dynamic, want use function form of column selector, called every time button action executed.
the infinite loop being triggered fact triggering button in click function called trigger function. rid of stop looping.
Comments
Post a Comment