Jquery datatables row count

Hi guys, i am tyring to print the row count of datatables in the footer very first column.

$(document).ready(function() {

//data table
var table = $('#example').DataTable( {	
    "dom": 'Bfrtip',
	"buttons" : [
					{
						extend: 'print',
						exportOptions: {
							columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15,16 ]
						}
					},
					{
						extend: 'excel',
						exportOptions: {
							columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15,16 ]
						}	
					},
					{
						extend: 'pageLength'
					}	
				],
	"aaSorting": [[0, 'desc']],		
	"pageLength": 100,
	"processing": false,
	"bInfo": false,
    
	
	"footerCallback": function ( row, data, start, end, display ) {
		var api = this.api(), data;
		$( api.column( 0 ).footer() ).html(table.data().length);

	},
		
	
	"ajax":"data.php"		
});

});

now when page loads it shows error

Uncaught TypeError: Cannot read property ‘data’ of undefined
at jQuery.fn.init.footerCallback (view.php:352)
at jquery.dataTables.min.js:75
at Function.map (jquery.js:451)
at r (jquery.dataTables.min.js:75)
at N (jquery.dataTables.min.js:30)
at S (jquery.dataTables.min.js:31)
at ga (jquery.dataTables.min.js:48)
at e (jquery.dataTables.min.js:92)
at HTMLTableElement. (jquery.dataTables.min.js:92)
at Function.each (jquery.js:362)

and if i remove this part datatable works perfectly. but i need the row count.

		"footerCallback": function ( row, data, start, end, display ) {
			var api = this.api(), data;
			$( api.column( 0 ).footer() ).html(table.data().length);

		},

wouldn’t that be this.data().length in this context?

1 Like

… or just data.length for that matter. You’d have to remove the var data declaration though, which is overwriting the parameter of the same name (and doesn’t hold any value and isn’t used anywhere).

2 Likes

Worked !!!

$( api.column( 1 ).footer() ).html(data.length);

Thank you to both of you for the help guys!!!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.