How to set total records when using Ajax source and pipeline?

How to set total records when using Ajax source and pipeline?

vinay_kvvinay_kv Posts: 2Questions: 1Answers: 0

We are using DataTables 1.10. To make search and display more efficient,
we are using pipeline and ajax source with 5 pages to cache showing 10 rows per page. So, 50 rows are in cache.

-This is the server response
data: Array(50) [ {…}, {…}, {…}, … ]
draw: 0
error: null
recordsFiltered: 311
recordsTotal: 311

If serverSide is true, it shows correctly including pagination.
Showing 1 to 10 of 311 entries

To enable local filtering and TableTools (copy/export csv), serverside was set to false. The problem here was, even though recordsTotal and recordsFiltered were returned, the max and total in info and pagination were set to the size of cache - 50.

Showing 1 to 10 of 50 entries

Pagination shows 5 pages only.

Here are the dataTable options -

        var dtOptions = function(url) {
    return {
        columns:      columnInfo,
        ordering:      true,
        order:           [ [2, 'asc'] ],
        pageLength:  10,
        deferRender: true,
        dataElement: dataFilter,
        HTTP_TYPE: "POST",
        serverSide:     false,
        ajaxUrl:           url,
        pipeline:          true,  //Use dataTables pipeline ajax function for caching.
        pagesToCache: 5, //Number of pages to cache
        rowCallBack :  function( json ) {/*redacted*/},
        createdRow:    function(row, rowData, dataIndex) {/*redacted*/},
        drawCallback: function(settings) {/*redacted*/},
        language: {
            "info" : "Showing _START_ to _END_ of _TOTAL_ entries." ,
            "infoFiltered" : "(filtered from _MAX_ total records)",
            "zeroRecords" : "No records to display.",
            "search" : ""
        }
        ,
        infoCallback: function(settings, start, end, max, total, pre) {
            console.log(settings);
            console.log(start + ":" + end  + ":" + max + ":" + total);
            return (!isNaN(total))
            ? "Showing " + start + " to " + end + " of " + total + " entries"
               + ((total !== max) ? " (filtered from " + max + " total entries)" : "")
            : "Showing " + start + " to " + (start + this.api().data().length - 1) + " entries";
        }
    };
        };

Here is the fragment of the pipeline script. Even setting the iRecordsTotal in success function did not help.

            settings.sAjaxDataProp = conf.dataSrc;//This is a function
            settings.jqXHR = $.ajax( {
                "type":        conf.method,
                "url":           conf.url,
                "data":        request,
                "contentType": conf.contentType,
                "dataType":    "json",
                "cache":       false,
                "success":  function ( jsn_data ) {
                    if( !conf.serverSide ) {
                settings._iRecordsTotal = jsn_data.recordsTotal;
                settings._iRecordsDisplay = jsn_data.recordsFiltered;
            }
                    cacheLastJson = $.extend(true, {}, jsn_data);
                    if ( cacheLower != drawStart ) {
                        jsn_data.data.splice( 0, drawStart-cacheLower );
                    }
                    if ( requestLength >= -1 ) {
                        jsn_data.data.splice( requestLength, jsn_data.data.length );
                    }
            //usr_data has the list of search results, recordsTotal and recordsFiltered. 
            drawCallback( jsn_data );
                },
                "error" : function(xhr, error, thrown) {//...redacted
            },
                "async" : conf.async
            } );
            return settings.jqXHR;

Is there a way to set recordsTotal when using Ajax, pipeline with serverside off? Any advise or help is greatly appreciated.

  • V

Answers

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin

    If server-side processing is off, then the total records is just the number of records that has been populated into the table. There is no way to override that I'm afraid.

    Also worth pointing out that you should only need server-side processing with tens of thousands or more rows. With less than that, I'd say it is just introducing network latency.

    Allan

  • vinay_kvvinay_kv Posts: 2Questions: 1Answers: 0

    Thank you Allan. The total records in this example were from our dev environment. There are thousands to display in our higher environment. Getting this bulk of data is sometimes causing out of memory issues. That's why we were prototyping with serverside or ajax-pipeline.

    • V
This discussion has been closed.