Request for initDataTable() / destroyDataTable() event

Request for initDataTable() / destroyDataTable() event

Loren MaxwellLoren Maxwell Posts: 365Questions: 88Answers: 9
edited August 22 in DataTables 1.10

I've recently started to use the initEditor function to attach some events to all editors I use on my site, such as:

$(document).on( 'initEditor', function ( e, inst ) {
    // Attach an 'open' event to all Editors
    inst.on( 'open', function () {
        // Perform some formatting or whatever
    });
});

This has helped me with this question: https://datatables.net/forums/discussion/76507/on-open-event-for-all-editors

A similar initDataTable and a corresponding destroyDataTable would be amazing!:

$(document).on( 'initDataTable', function ( e, inst ) {
    // Attach an 'open' event to all DataTables
    inst.on( 'open', function () {
        // Perform some formatting or whatever
    });
});

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 19,470Questions: 26Answers: 4,612

    There is initComplete and init that are used when Datatables is initialized. Is that what you are looking for?

    Will let @allan comment on the destroyDataTable event.

    Kevin

  • Loren MaxwellLoren Maxwell Posts: 365Questions: 88Answers: 9

    @kthorngren -- thanks, Kevin -- initComplete and init will work but I'd have to add the common // Perform some formatting or whatever to each table:

        table_1.on( 'init', function () {
            // Perform some formatting or whatever
        });
        table_2.on( 'init', function () {
            // Perform some formatting or whatever
        });
        table_3.on( 'init', function () {
            // Perform some formatting or whatever
        });
    

    I'm hoping for a DataTables event on each DataTable creation so that i can then add those events to each new table.

    However mentioning -event does draw my attention to a copy and paste mistake in my example above.

    It should be:

    $(document).on( 'initDataTable', function ( e, inst ) {
        // Attach a common 'init' event to all DataTables on 'initDataTable'
        inst.on( 'init', function () {
            // Perform some formatting or whatever
        });
    });
    
  • kthorngrenkthorngren Posts: 19,470Questions: 26Answers: 4,612
    Answer ✓

    Possibly you can use a selector that covers all the Datatables similar to this example.
    https://www.datatables.net/examples/basic_init/multiple_tables.html

    You may need to add the ‘dt’ namespace for example ‘init.dt’.

    Kevin

  • allanallan Posts: 60,307Questions: 1Answers: 9,792 Site admin
    Answer ✓

    Both init and destroy bubble, so:

    $(document)
      .on('init.dt', function () {
        // ...
      })
      .on('destroy.dt', function () {
        // ...
      })
    

    Would do what you are looking for.

    Allan

  • Loren MaxwellLoren Maxwell Posts: 365Questions: 88Answers: 9

    Ah, awesome! Thanks, @kthorngren and @allan!

Sign In or Register to comment.