How to toggle columns on multiple tables

How to toggle columns on multiple tables

mjmbmjmb Posts: 3Questions: 1Answers: 0
edited November 2020 in Free community support

I have started using Datatables and found this link (https://datatables.net/examples/api/show_hide.html) that allows you to dynamically show and hide columns. I have two tables (with the same columns) on a page and want to have one toggle menu to manage the columns. Is this possible?

This is the code I have below:

$('a.toggle-vis').on( 'click', function (e) {
    e.preventDefault();

    // Get the column API object
    var column = $('.datatable-toggle').DataTable().column( $(this).attr('data-column') );

    // Toggle the visibility
    column.visible( ! column.visible() );

  } );

So far the toggle only works on the first table. I guess this is because I need loop through the tables as suggested here: https://stackoverflow.com/questions/36941420/multiple-data-table-toggle-column. Could someone help?

Thanks in advance!

Answers

This discussion has been closed.