I have this script to display data to datatables:
<script type="text/javascript">
$('#btnRoster').click(function (e) {
$('#dataTable').dataTable().fnDestroy();
e.preventDefault();
$.ajax({
url: '@Url.Action("GetRoster", "Home")',
type: 'GET',
dataType: "json",
success: function (response) {
var table = $('#dataTable').dataTable({
data: response,
columns: [
{ 'data': 'Id' },
{ 'data': 'Name' },
{ 'data': 'Project' },
{ 'data': 'Geo.GeoName' },
{ 'data': 'Tower.TowerName' },
{ 'data': 'Region.RegionName' },
{ 'data': 'WBS.WBSName' },
{ 'data': 'Process.ProcessName' },
{ 'data': 'PrimaryTeam.PrimaryTeamName' },
{ 'data': 'IsActive' },
{ 'data': 'Gender' },
{ 'data': 'Level' },
{ 'data': 'ShiftStart' },
{ 'data': 'ShiftEnd' },
{ 'data': 'Comment' },
{
"className": 'details-control',
"orderable": false,
"data": null,
"defaultContent": '<button class=btn btn-primary btn-block>Edit</button>'
}
],
"order": [[2, 'asc']],
columnDefs: [
{ "visible": false, "targets": 0 },
The above actually works, but when I want to check the row count of datatable using the below code, it returning 0. For testing purposes, I assign it to the tr click to get the row count.
<script type="text/javascript">
$(document).ready(function () {
var table = $('#dataTable').DataTable();
$("#dataTable").on("click", "tr", function () {
var data = table
.rows()
.data();
alert('The table has ' + data.length + ' records');
});
});
</script>
Why is it returning 0 where in fact the data is there?