table.ajax.reload() vs $('#TableID').DataTable().ajax.reload();

table.ajax.reload() vs $('#TableID').DataTable().ajax.reload();

aziegler3aziegler3 Posts: 21Questions: 3Answers: 0

I have a series of tables that need to be reloaded on a click.
The code is very simple:
releaseGeneralInfoTable.ajax.reload();
releasePhysicalInfoTable.ajax.reload();
requestFormProgramTable.ajax.reload();
requestFormResearchTable.ajax.reload();
parasitoidRequestTable.ajax.reload();
all the tables go to the same database, have very similar js and PHP scripts.
But one of them would not reload.
I tried multiple tricks and double-checked everything to no avail. The ajax part of the js script is identical, and it works in the first load. It just won't go with table.ajax.reload();
Then I replaced "table" with $('#TableID').DataTable()
Bam! it works.
What is the rational on this?

Answers

  • kthorngrenkthorngren Posts: 20,299Questions: 26Answers: 4,769

    You will want to read about Accessing the API.

    It just won't go with table.ajax.reload();

    Do you get errors in the browser's console?

    As long as the variable table contains an instance of the Datatables API, ie var table = $('#TableID').DataTable();, and is accessible within the scope you are trying to use it there is no difference between table.ajax.reload(); and $('#TableID').DataTable().ajax.reload();.

    Kevin

  • aziegler3aziegler3 Posts: 21Questions: 3Answers: 0

    The console shows no errors.
    Yes. there is a var table = $('#TableID').DataTable()
    But one works and the other does not.

    Very puzzling.

  • kthorngrenkthorngren Posts: 20,299Questions: 26Answers: 4,769
    edited November 2020

    Without seeing your code it would be hard to say. Can you post a link to your page or a test case showing the issue?
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Make sure you have var table = $('#TableID').DataTable() not var table = $('#TableID').dataTable(). Notice the case of the D in DataTable(). Make sure the jQuery selector, $('#TableID'), is correct. Make sure you haven't reassigned table to something else. Make sure the table.ajax.reload(); statement is executed.

    Kevin

  • aziegler3aziegler3 Posts: 21Questions: 3Answers: 0

    kthorgren, thanks for taking the time to discuss this.
    It is too complicated (and behind a firewall) to share or post a copy.
    However, the table is created with

    (function($){

    $(document).ready(function() {

    var releaseTreesTable = $('#APHIS_RELEASE_TREES').DataTable( {
        dom: 'Bfrtip',
        searching: false,
        responsive: true,
        paging: false,
        ajax: {
            url: '/RELEASE_TREES_filter.php',
            data: function(d){
                d.site_id = site_id;
            }
        },
        columns: [  
            {
                data: "OBJECTID"
            },                      
                      etc
                      etc
            {
                data: "COMMENTS",
                searchable: false
            }
        ],
        //select: true,
        lengthChange: false,
        buttons: [
            { extend: 'edit',   editor: editor }
        ]
    } );
    

    } );

    }(jQuery));

    There are five tables constructed very similarly.
    They all are reloaded with
    tablename.ajax.reload();.

    only one needs
    $('#TableID').DataTable().ajax.reload();.

  • colincolin Posts: 15,143Questions: 1Answers: 2,586

    That's definitely not expected, but without seeing the issue, it's hard to progress. This example here is using ajax, could you modify that, please, to mimic what your code is doing so that we can see.

    Colin

This discussion has been closed.