Kirsty White
Kirsty White

Reputation: 1220

count the number of values jQuery Datatables

When I navigate to my jQuery DataTable I would like to display how many Users are pending activation. Normally I would use fnGetData with (this) however as I am not trying to do this on a click event and I am simply just trying to count the number in the entire table I'm not sure how to do it:

UPDATE: SOLUTION

$(document).ready(function () {
    var oTable = $('#example2').dataTable({
            fnInitComplete: function (oSettings, json) {

                //store table data 
                var data = this.fnGetData();

                var pendingCount = 0;
                for (var i = 0; i < data.length; i++) {
                    if (data[i][5] != null && data[i][5] == '1') {
                        pendingCount++;
                    }
                }
                $(".panel-footer #pending").val(pendingCount);
                //pass count to html
                //alert(pendingCount);
            },
        "sAjaxSource": "AjaxHandler",
        "aoColumns": [
                        { "sName": "Id" },
                        { "sName": "ProfileId" },
                        { "sName": "Type" },
                        { "sName": "Name" },
                        { "sName": "Email" },
                        { "sName": "PendingActivation" }
        ]
    });

Upvotes: 0

Views: 283

Answers (1)

Gyrocode.com
Gyrocode.com

Reputation: 58860

SOLUTION

You have some errors in your logic while calculating total count, see the corrected code below:

var data = oTable.fnGetData();

var pendingCount = 0;
for(var i = 0; i< data.length; i++){
   if (data[i][5] != null && data[i][5] == '1') { 
       pendingCount++;
   }        
}

//pass count to html
$(".panel-footer #pending").val(pendingCount);

DEMO

See this jsFiddle for demonstration.

Upvotes: 1

Related Questions