Datatable server side ajax on demand

101 views Asked by At

How to setup server side processing and not to load the datatable right away but on a button?

With this setup:

var usersTable = $('#users').DataTable({
    responsive: true,
    processing: true,
    serverSide: true,
    autoWidth: false,
    ajax: 'https://example.com/users',
    ..............................

the datatable is loaded straight away and I want to load it on click

Is this possible somehow?

1

There are 1 answers

0
Nikolay Shindarov On BEST ANSWER

How about:

<button id="my_button" value="click to load DB table!" />

<script>
$( document ).ready(function() {
    $('#my_button').click(function(){
        load_table();
    });

    function load_table() {
        var usersTable = $('#users').DataTable({
            responsive: true,
            processing: true,
            serverSide: true,
            autoWidth: false,
            ajax: 'https://example.com/users',
            ..............................
        });
    }
});
</script>