record count with server side processing

record count with server side processing

neburtonneburton Posts: 63Questions: 6Answers: 0
edited July 2010 in General
Hi,

I'm trying to get a record count with server side processing. It works fine client side.

I'm updating a span element with the count.

My code, which works fine with client side processing is below:

[code]
"fnDrawCallback": function() {
var oSettings = this.fnSettings();
var iTotalRecords = oSettings.fnRecordsTotal();
$("#rightsearchcount").html(iTotalRecords);
},
[/code]

Any advice please?

Replies

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Looks fine to me - does it not work for you?

    You could do it in one line if you wanted it to be smaller:

    [code]
    $("#rightsearchcount").html( this.fnSettings().fnRecordsTotal() );
    [/code]

    Allan
  • neburtonneburton Posts: 63Questions: 6Answers: 0
    Doesn't work with server side processing enabled????

    oSettings.fnRecordsTotal() is undefined on a drawcallback in this scenario.

    [code]
    searchTable = $('#searchtable').dataTable({
    "oLanguage":
    {
    "sSearch": "Query",
    "sProcessing": ""
    },
    "bJQueryUI": true,
    "aoColumns": [
    { "bVisible": false } ,
    null,
    { "sClass": "contact" },
    { "sClass": "main" },
    null,
    null
    ],
    "asStripClasses": [ 'strip1', 'strip2' ],
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "json_search2.asp",
    "fnDrawCallback": function() {
    var oSettings = this.fnSettings();
    var iTotalRecords = oSettings.fnRecordsTotal();
    $("#rightsearchcount").html(iTotalRecords);
    },
    "fnRowCallback": function( nRow, aData, iDisplayIndex )
    {
    var uid = aData[0];
    $(nRow).addClass('UID'+uid);
    $(nRow).bind("click", function(e)
    {
    loadcontact(uid);
    });
    return nRow;
    }





    }).fnSetFilteringDelay(500);
    [/code]
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    This seems to work for me:

    [code]
    $(document).ready(function() {
    $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "../examples_support/server_processing.php",
    "fnDrawCallback": function () {
    console.log( this.fnSettings().fnRecordsTotal() );
    }
    } );
    } );
    [/code]
    Does your returned JSON correctly include "iRecordsTotal"? If you filter the table, what does the "info" string say - it also gets it's value from fnRecordsTotal.

    Allan
  • neburtonneburton Posts: 63Questions: 6Answers: 0
    Alan,

    Thanks for your assistance. There was a type in my json. Working now.
  • kaaliskaalis Posts: 6Questions: 0Answers: 0
    Hello, i've been searching this forum but couldn't find my answer how to refresh table when filtered record count is zero.

    [code]
    "bServerSide": true,
    "fnDrawCallback": function ( oSettings ) {
    var record_count = this.fnSettings().fnRecordsTotal();
    //console.log(record_count);
    if(record_count==0) { //no records to show
    $('.filter_refresh').val('0'); //resets all filters
    //oTable.fnDraw(); //redraws table but I can't exit loop... Browser freezes
    }
    },
    "sAjaxSource": "ajax_source.php",
    "sServerMethod": "POST",
    [/code]

    Thanks for great DataTables script ;)
  • kaaliskaalis Posts: 6Questions: 0Answers: 0
    edited March 2012
    bump :)
    I'm using latest version.
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    fnDraw is the correct method to call to refresh the table - you just need to keep a private flag to tell your fnDrawCallback function if it should call fnDraw or not.

    Allan
  • kaaliskaalis Posts: 6Questions: 0Answers: 0
    edited March 2012
    hm, don't think I undertood the meaning of private flag. I tried this already before posting in this topic.
    [code]var draw = true; //before var oTable = ...
    if(draw==true) { //in fnDrawCall back
    oTable.fnDraw();
    draw = false;
    }[/code]
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    That looks just about perfect to me (and what I had in mind by a "private flag") - however you just need to modify the logic a little bit to set your draw flag as needed when the filtering condition is met. I think you are very much along the right lines there.

    Allan
This discussion has been closed.