dataTable Constructor vs DataTable API differences?

dataTable Constructor vs DataTable API differences?

InnovaMattInnovaMatt Posts: 13Questions: 7Answers: 0

Hello,

Been using this excellent plugin for a good year as I've been learning on the job, but it's only when I've come to try to use the ajax.reload method that I've learned of the difference between the dataTable constructor & DataTable API. I've been using the former for constructing all of my tables until now and I'm struggling to get my head around what and how much I need to adjust to allow me to use the ajax.reload() method.

Here's a fairly typical example of the dataTables I've got; what do I need to change to use DataTable's ajax.reload() with it?

dT_SupCon = $('#dT_SupCon').dataTable({ // ##### Contacts dataTable #####
            "pageLength": 5,
            "bAutoWidth": true,
            "bLengthChange": false,
            "bFilter": false,
            "language": {
                "emptyTable": "No contacts have been created for " + supName + "."
            },
            "serverSide": true,
            "ajax": {
                "url": "/ajax/contacts.php",
                "type": "POST",
                "dataSrc": function (json) {
                    // console.dir(json.data); // Log array to console
                    return json.data
                },
                "data": function(d) {
                    d.orgID = supID;
                    d.mode = "dts"; // Populate Table from Supplier perspective
                }
            },
            "columnDefs": [
                {"orderable": false, "targets": "no-sort"},
                {className: "fit", "targets": [0,1,2]},
                {className: "fit text-center", "targets": [3,4]}
            ],
            "fnDrawCallback": function (oSettings) {
                var pagination = $(this).closest('.dataTables_wrapper').find('.dataTables_paginate');
                pagination.toggle(this.api().page.info().pages > 1);
                var pagedetails = $(this).closest('.dataTables_wrapper').find('.dataTables_info');
                pagedetails.toggle(this.api().page.info().pages > 0);
            }
        }); // Contacts dataTable

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,147Questions: 26Answers: 4,918
    Answer ✓

    This doc explains accessing the API:
    https://datatables.net/manual/api#Accessing-the-API

    You could use this:

    $('#dT_SupCon').DataTable().ajax.reload();
    

    or this to get an API instance:

    var table - $('#dT_SupCon').DataTable();
    
    table.ajax.reload();
    

    Depending on where you declare table, or whatever variable name you want to use, you can just use the variable to access the API without redeclaring it.

    Or you can get the API instance when you init Datatables, assuming that you aren't using the jQuery object:

    dT_SupCon = $('#dT_SupCon').DataTable({ // ##### Contacts dataTable #####
    

    And you the variable dT_SupCon to access the API.

    Not sure if this is answering your question.

    Kevin

This discussion has been closed.