0

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" }
        ]
    });
2
  • 1
    To improve your question please specify what is going wrong with your current solution, e.g. do you get any errors or see other symptoms of failure? Commented Oct 1, 2015 at 19:05
  • 1
    Done... thanks for the heads up. Commented Oct 1, 2015 at 19:21

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

hmm seems to be outputting 0 all the time?
@KirstyWhite, my example on jsFiddle works for HTML sourced data. If you have Ajax sourced data, you need to put this code in fnInitComplete callback.
@KirstyWhite, see this example with fnInitComplete which also works. Regarding your update: use this instead of oTable.
See updated question not sure if my code is laid out correct for this
@KirstyWhite, that's because you cannot initialize the table twice with dataTable(). Put fnInitComplete into the original table definition.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.