0

I have trouble to get count datatables, i need to chect that table is emtpy or not, this my code :

var table = $("#tbl_tmppo");
        var target = table.attr('data-table');

        var oTable = table.on( 'processing.dt', function ( e, settings, processing ) {
            if (processing) {
                $(this).find('tbody').addClass('load1 csspinner');
            } else{
                $(this).find('tbody').removeClass('load1 csspinner');
            };
        } ).DataTable({
            "bServerSide": true,
            "ajax": host+'datatable/'+target,
            "columnDefs": [{
                "targets": [ -1 ],
                "className": "dt-body-left",
            }]
        });

i'm kinda stuck in here, i wanna check if i dont have data on Otable, i need do something, if not i need to something else. Anyone can help me??

Update : I was using like oTable.Data().length, but it always show 0 length to me even i have 2 record on that table. if i use oTable.Data().count() is show error not undifined.

CLOSED. Tq for kcp code

oTable.on('draw', function () {
    /* Now you can count rows */
    var count = table.data().count()-1;

});

so i can get count my data. Tq kcp!!! U're awsome!!

2 Answers 2

2

You can use dataFilter to get length of rows.

DataTable({
    "bServerSide": true,
    ajax: {
        url: host+'datatable/'+target,
        dataFilter: function(data){
            var json = jQuery.parseJSON( data );
            /* Here you have your results */
            json.recordsTotal = json.recordsTotal;
            json.recordsFiltered = json.recordsFiltered;
            json.data = json.list;

            return data;
        }
    }
    "columnDefs": [{
       "targets": [ -1 ],
       "className": "dt-body-left",
     }]
})

@edit: For non-backend data you can use draw event. For example:

oTable.on('draw', function () {
    /* Now you can count rows */
    var count = table.find("tr").size();
});

@edit2: You can also use data().count() method of DataTable.API

oTable.on('draw', function () {
        /* Now you can count rows */
        var count = table.data().count();
});
Sign up to request clarification or add additional context in comments.

4 Comments

ok i'm gonna try that. Hope work, i will give u information if is work or not. :)
well, honestly i've not yet response from backend, cause i dont know how to callback totalrecords on table. and sorry for my languange
Oh.. I was thinking you using backend. Let me a moment for edit an answer.
Dude!! Tq very much is wrok, well kinda work, i just use var count = table.find("tr").size() -1; to get my row data. tq verymuch dude!!!! i will make approved u code. Tq again :D
-1
function CountRows() {
    var totalRowCount = 0;
    var rowCount = 0;
    var table = document.getElementById("tblCustomers");
    var rows = table.getElementsByTagName("tr")
    for (var i = 0; i < rows.length; i++) {
        totalRowCount++;
        if (rows[i].getElementsByTagName("td").length > 0) {
            rowCount++;
        }
    }
    var message = "Total Row Count: " + totalRowCount;
    message += "\nRow Count: " + rowCount;
    alert(message);
}

1 Comment

can u make format codes like my code?? Casue is kinda diferent for me

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.