Reference the "active" table on init of multiple tables

Reference the "active" table on init of multiple tables

lougarcialougarcia Posts: 2Questions: 1Answers: 0

In most jQuery methods, I can reference the "active" element using jQuery(this)

for example:

$('.many-buttons')on('click', function(e){
     
    $.ajax({
        url: 'link/to/some/page?'.jQuery(this).data('custom_param')
    });
});

Why can't I do something like this in DataTables?:

$('.enhanced_tables').DataTable({
    serverSide: true,
    ajax: {
        url: 'link/to/some/page',
        data: function(d) {
            d.custom_param = $(this).data('custom_param') /* this references the whole Window object */
        }
    }
});

Any way to fix or any function in DataTables I'm not aware of that can do just this?

Thank you.

Answers

  • lougarcialougarcia Posts: 2Questions: 1Answers: 0

    So...

    I got this working by using this:

    $('.enhanced_tables').DataTable({
        serverSide: true,
        ajax: {
            url: 'link/to/some/page',
            data: function(d, x) {
                d.custom_param = x.oInit.custom_param;
            }
        }
    });
    

    I'm pretty sure this is not the way to do it.

    If you know the proper way, please let me know.

    Thank you.

  • allanallan Posts: 63,350Questions: 1Answers: 10,443 Site admin

    The ajax.data function is one of the few callbacks where you can't use $(this) in DataTables to get the table node as a jQuery object.

    Your work around is probably about as good as it gets at the moment if you need to be able to send parameters via the init object rather than just scoping them locally.

    Allan

This discussion has been closed.