Determine number of rows in table
Determine number of rows in table
jdadwilson
Posts: 127Questions: 30Answers: 1
In my app I have several modules that the user can access to display various monthly data. When each module loads (onload) the form is initialized as is the dataTable with the following code...
[code]
function initForm() {
// Set dates for date navigation
$('#PrevYear').val(Date.today().add({years: -1}).toString('yyyy'));
$('#PrevMonth').val(Date.today().add({months: -1}).toString('MMM'));
$('#CurrMonth').val(Date.today().toString('yyyy-MM'));
$('#NextMonth').val(Date.today().add({months: 1}).toString('MMM'));
$('#NextYear').val(Date.today().add({years: 1}).toString('yyyy'));
// Set first and last day of the month
$('#form_BegDate').val(Date.today().moveToFirstDayOfMonth().toString('yyyy-MM-dd'));
$('#form_EndDate').val(Date.today().moveToLastDayOfMonth().toString('yyyy-MM-dd'));
// Parameter to pass for first and last day of month
var sBegDate = $('#form_BegDate').val();
var sEndDate = $('#form_EndDate').val();
var sParams = '?BegDate=' + sBegDate + '&EndDate=' + sEndDate;
// String for 'No records found for MMM YYYY'
var sRecords = 'No records found for ' + Date.parse(sBegDate).toString('MMM yyyy');
// Initialize the dataTable
oTable = $('#squarefeet').dataTable( {
'aoColumns': [
{ 'mDataProp': 'sqft_Project', 'sClass': 'udatal' },
{ 'mDataProp': 'sqft_Phase', 'sClass': 'udatac' },
{ 'mDataProp': 'sqft_Date', 'sClass': 'udatac' },
{ 'mDataProp': 'sqft_Amount', 'sClass': 'udatar' },
{ 'mDataProp': 'sqft_Rate', 'sClass': 'udatar' },
{ 'mDataProp': 'sqft_Complete', 'sClass': 'udatar' },
{ 'mDataProp': 'sqft_Comment', 'bVisible': false }
], // End of aoColumns
'bAutoWidth': false,
'bDestroy': true,
'bFilter': false,
'bInfo': false,
'bLengthChange': false,
'bPaginate': true,
'bProcessing': false,
'bServerSide': false,
'bSort': false,
'bVisible': true,
'iDisplayLength': 12,
'oLanguage': { 'oPaginate': { 'sPrevious': 'Prev' } },
'oTableTools': {
'sRowSelect': 'single',
'aButtons': false
},
'sAjaxSource': 'files_json/ajax_Squarefeet.php' + sParams,
'sDom': 'T<"clear">lfrtip',
'sPaginationType': 'full_numbers',
'fnInitComplete': function(oSettings, json) {
if ( this.fnGetData().length === 0 ) {
$('#sqftTable').hide();
$('#buttonPrt').hide();
$('#txt_NoRecords').text(sRecords);
$('#noRecords').show();
} else {
$('#noRecords').hide();
$('#sqftTable').show();
$('#buttonPrt').show();
if ( this.fnGetData().length <= 12 ) {
$('#squarefeet_paginate').hide();
}
}
} // End of fnInitComplete
}); // End of dataTable
} // End of init function
[/code]
Note that via the fnInitComplete function I check to determine the number of records returned from the AJAX call. This works fine.
At the top of the form I have calendar navigation buttons. Selecting a button changes the month and/or year (forward or backward) to allow for selecting new data via an AJAX call as follows...
[code]
function loadTable() {
$('#sqftTable').hide();
$('#noRecords').hide();
$('#NewDate').val(sBegDate);
$('#sqft_Date').val(sBegDate);
var sBegDate = $('#form_BegDate').val();
var sEndDate = $('#form_EndDate').val();
var sParams = '?BegDate=' + sBegDate + '&EndDate=' + sEndDate;
var sRecords = 'No records found for ' + Date.parse(sBegDate).toString('MMM yyyy');
oTable.fnReloadAjax('files_json/ajax_Squarefeet.php' + sParams, chkTableLength());
} // End of loadTable
[/code]
Note that in the fnReloadAjax call I am passing a chkTableLength function, which is as follows...
[code]
function chkTableLength() {
console.log('Len: ' + $('#sqftTable tbody tr').length);
if ( $('#sqftTable tbody tr').length === 0 ) {
$('#sqftTable').hide();
$('#buttonPrt').hide();
$('#txt_NoRecords').text(sRecords);
$('#noRecords').show();
} else {
$('#noRecords').hide();
$('#sqftTable').show();
$('#buttonPrt').show();
if ( $('#sqftTable tbody tr').length <= 12 ) {
$('#squarefeet_paginate').hide();
}
}
}
[/code]
The problem is that the value displayed in the console.log seems to be for the previous table and not the current table (i.e., the callBack function is not like the fnInitComplete function of the dataTable initialization.)
The whole point of this is to simply not show the table if the number of rows is zero and further to not show the pagination elements if the length is less/equal to the iDisplayLength parameter. (This has been submitted as a feature request.)
Any assistance on how to get the length of the current table in the chkTableLength function will be greatly appreciated.
TIA
jdadwilson
[code]
function initForm() {
// Set dates for date navigation
$('#PrevYear').val(Date.today().add({years: -1}).toString('yyyy'));
$('#PrevMonth').val(Date.today().add({months: -1}).toString('MMM'));
$('#CurrMonth').val(Date.today().toString('yyyy-MM'));
$('#NextMonth').val(Date.today().add({months: 1}).toString('MMM'));
$('#NextYear').val(Date.today().add({years: 1}).toString('yyyy'));
// Set first and last day of the month
$('#form_BegDate').val(Date.today().moveToFirstDayOfMonth().toString('yyyy-MM-dd'));
$('#form_EndDate').val(Date.today().moveToLastDayOfMonth().toString('yyyy-MM-dd'));
// Parameter to pass for first and last day of month
var sBegDate = $('#form_BegDate').val();
var sEndDate = $('#form_EndDate').val();
var sParams = '?BegDate=' + sBegDate + '&EndDate=' + sEndDate;
// String for 'No records found for MMM YYYY'
var sRecords = 'No records found for ' + Date.parse(sBegDate).toString('MMM yyyy');
// Initialize the dataTable
oTable = $('#squarefeet').dataTable( {
'aoColumns': [
{ 'mDataProp': 'sqft_Project', 'sClass': 'udatal' },
{ 'mDataProp': 'sqft_Phase', 'sClass': 'udatac' },
{ 'mDataProp': 'sqft_Date', 'sClass': 'udatac' },
{ 'mDataProp': 'sqft_Amount', 'sClass': 'udatar' },
{ 'mDataProp': 'sqft_Rate', 'sClass': 'udatar' },
{ 'mDataProp': 'sqft_Complete', 'sClass': 'udatar' },
{ 'mDataProp': 'sqft_Comment', 'bVisible': false }
], // End of aoColumns
'bAutoWidth': false,
'bDestroy': true,
'bFilter': false,
'bInfo': false,
'bLengthChange': false,
'bPaginate': true,
'bProcessing': false,
'bServerSide': false,
'bSort': false,
'bVisible': true,
'iDisplayLength': 12,
'oLanguage': { 'oPaginate': { 'sPrevious': 'Prev' } },
'oTableTools': {
'sRowSelect': 'single',
'aButtons': false
},
'sAjaxSource': 'files_json/ajax_Squarefeet.php' + sParams,
'sDom': 'T<"clear">lfrtip',
'sPaginationType': 'full_numbers',
'fnInitComplete': function(oSettings, json) {
if ( this.fnGetData().length === 0 ) {
$('#sqftTable').hide();
$('#buttonPrt').hide();
$('#txt_NoRecords').text(sRecords);
$('#noRecords').show();
} else {
$('#noRecords').hide();
$('#sqftTable').show();
$('#buttonPrt').show();
if ( this.fnGetData().length <= 12 ) {
$('#squarefeet_paginate').hide();
}
}
} // End of fnInitComplete
}); // End of dataTable
} // End of init function
[/code]
Note that via the fnInitComplete function I check to determine the number of records returned from the AJAX call. This works fine.
At the top of the form I have calendar navigation buttons. Selecting a button changes the month and/or year (forward or backward) to allow for selecting new data via an AJAX call as follows...
[code]
function loadTable() {
$('#sqftTable').hide();
$('#noRecords').hide();
$('#NewDate').val(sBegDate);
$('#sqft_Date').val(sBegDate);
var sBegDate = $('#form_BegDate').val();
var sEndDate = $('#form_EndDate').val();
var sParams = '?BegDate=' + sBegDate + '&EndDate=' + sEndDate;
var sRecords = 'No records found for ' + Date.parse(sBegDate).toString('MMM yyyy');
oTable.fnReloadAjax('files_json/ajax_Squarefeet.php' + sParams, chkTableLength());
} // End of loadTable
[/code]
Note that in the fnReloadAjax call I am passing a chkTableLength function, which is as follows...
[code]
function chkTableLength() {
console.log('Len: ' + $('#sqftTable tbody tr').length);
if ( $('#sqftTable tbody tr').length === 0 ) {
$('#sqftTable').hide();
$('#buttonPrt').hide();
$('#txt_NoRecords').text(sRecords);
$('#noRecords').show();
} else {
$('#noRecords').hide();
$('#sqftTable').show();
$('#buttonPrt').show();
if ( $('#sqftTable tbody tr').length <= 12 ) {
$('#squarefeet_paginate').hide();
}
}
}
[/code]
The problem is that the value displayed in the console.log seems to be for the previous table and not the current table (i.e., the callBack function is not like the fnInitComplete function of the dataTable initialization.)
The whole point of this is to simply not show the table if the number of rows is zero and further to not show the pagination elements if the length is less/equal to the iDisplayLength parameter. (This has been submitted as a feature request.)
Any assistance on how to get the length of the current table in the chkTableLength function will be greatly appreciated.
TIA
jdadwilson
This discussion has been closed.
Replies
Allan