Capturing total number of records?

Capturing total number of records?

JonasNJJonasNJ Posts: 2Questions: 1Answers: 0

I am trying to capture the total number of records shown in a given DataTable. Unfortunately I cannot link to the site since it's on a secure platform.
I have searched the forum for answers on how to do it but none of the similar posts made any sense to me.
I have also tried the page.info() method (https://datatables.net/reference/api/page.info()) like this:

// Extract total number of records
        var showNumberOfRecords = function () {
            var currentTable = $('#example').DataTable();
            var info = currentTable.page.info();
            $('#table-info-total-records').html(
                'You have a total of '+info.recordsTotal+' employees'
            );
            console.log(currentTable);
            console.log(info);
        }

but that just returns an object with no records:

I noticed that the DataTable is showing the total number of records here:

and I found the responding code in datatables.bundle.js on line 15777:

but I don't know how to extract that data and don't even understand the syntax of the code...

The DataTable gets data from an ajax call, but Server-side processing is set to false, since searching and ordering ect. should be processed in the browser.

I hope I have explained the issue well enough and any help would be greatly appreciated.
I apologize for the lack of technical understanding on my part - I am but a novice developer :)

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,769
    Answer ✓

    Its possible that you are executing the showNumberOfRecords function before Datatables has completed initialization. The ajax request is asynchronous so the code keeps executing while waiting for the ajax response. Move the call to this function into the initComplete option.

    Kevin

  • JonasNJJonasNJ Posts: 2Questions: 1Answers: 0

    @kthorngren Kevin, my man, that did the trick. Thank you, I truly appreciate it.

    For anyone experiencing the same issue, here is my updated code:

    // Private functions
            var initDatatable = function () {
                dt = $("#" + param_dataTableID).DataTable({
                    searchDelay: 500,
                    processing: true,
                    serverSide: false,
                    stateSave: true,
                    ajax: {
                        url: param_apiURL,
                        dataSrc: 'data'
                    },
                    columns: param_columns,
                    columnDefs: param_columnDefs,
                    // Extract and show total number of records after initialization
                    initComplete: function(settings, json) {
                        var currentTable = $("#" + param_dataTableID).DataTable();
                        var info = currentTable.page.info();
                        $('#table-info-total-records').html(
                            'You have a total of '+info.recordsTotal+' employees'
                        );
                    }
                });
    
                table = dt.$;
    
                // Re-init functions on every table re-draw -- more info: https://datatables.net/reference/event/draw
                dt.on('draw', function () {
                    KTMenu.createInstances();
                });
            }
    
Sign In or Register to comment.