fnReloadAjax
fnReloadAjax
On my site there is an event that updates the info in the underlying tables and after that even I want to refresh my tables.
After searching I see that fnReloadAjax appears to be what I want, but I'm not sure how to use it.
Can someone please post an example using fnReloadAjax to refresh an existing datatable on an event other than .ready (like in a button click or something)?
After searching I see that fnReloadAjax appears to be what I want, but I'm not sure how to use it.
Can someone please post an example using fnReloadAjax to refresh an existing datatable on an event other than .ready (like in a button click or something)?
This discussion has been closed.
Replies
[code]
var oTable;
$(document).ready( function () {
oTable = $('#example).dataTable();
$('#click_me').click( function () {
oTable.fnReloadAjax();
} );
} );
[/code]
This will reload the table whenever the element with the id 'click_me' is click upon.
Allan
One last question.
How would I call fnReloadAjax from another javascript function?
The call above is actually being done from a function - it's just an anonymous function in typical jQuery stylee...
Something more common might be like this:
function my_func () {
oTable.fnReloadAjax();
}
Allan
In my ready statement I have:
$('#styles').dataTable( {
"bStateSave": true,
"bProcessing": true,
"bPaginate": false,
"bLengthChange": false,
"bFilter": false,
"bSort": false,
"bInfo": false,
"aoColumns": [
{ "sClass": "stylename" },
{ "sClass": "center", "sTitle": "Level" },
{ "sClass": "center", "bVisible": false },
{ "sClass": "center" },
{ "sClass": "center" },
{ "sClass": "center" },
{ "sClass": "center" },
{ "sClass": "center" },
{ "sClass": "center" },
{ "sClass": "center" }
],
"sAjaxSource": '/ajax.php?action=get_styles'
} );
And the table is initialized and looks great.
In another javascript function (not in the ready event) I have:
function isUpdated() {
document.getElementById("outputstyle").innerHTML = 'Updated';
var oTable;
oTable = $('#styles').dataTable();
oTable.fnReloadAjax();
}
And when this is executed i get:
Error: oTable.fnReloadAjax is not a function
Regards,
Allan
I'm not sure what I'm doing wrong with this.
Here is my code:
[code]
var oTable;
$(document).ready(function() {
oTable = $('#styles').dataTable( {
"bStateSave": true,
"bProcessing": true,
"bPaginate": false,
"bLengthChange": false,
"bFilter": false,
"bSort": false,
"bInfo": false,
"aoColumns": [
{ "sClass": "stylename" },
{ "sClass": "center", "sTitle": "Level" },
{ "sClass": "center", "bVisible": false },
{ "sClass": "center" },
{ "sClass": "center" },
{ "sClass": "center" },
{ "sClass": "center" },
{ "sClass": "center" },
{ "sClass": "center" },
{ "sClass": "center" }
],
"sAjaxSource": '/ajax.php?action=get_styles&id=121'
} );
} );
function test() {
oTable.fnReloadAjax();
}
[/code]
When I click on a button that fires test() I get:
oTable.fnReloadAjax is not a function.
* Edited by Allan to add syntax highlighting. Use [ code ] and [ /code ] (without the spaces)
You can see the following for how plug-ins might be applied (although this is sorting and type detection, the same principle applies): http://datatables.net/examples/example_sorting_plugin.html
Allan
I'm sorry, but I do not think I understand.
I reviewed the example you provided but am not sure what must be done.
To accomplish this I will need to create my own function named fnReloadAjax and in that function I must add the javascript to delete all rows?
Again, sorry for being so dense on this.
After reading and re-reading each of the examples it finally became clear to me and is now working.
Thank you so much for all the help!!!
[code]
<?PHP
include("includes/dbconnect.php");
include("includes/functions.php");
?>
@import "cssTable/demo_page.css";
@import "cssTable/demo_table.css";
<!-- Only needed when a form that needs validated is used -->
<!-- Only needed when a table that needs sorted is included -->
function addContact(){
//add contact code goes here... (forum wouldn't allow me to post the full length code)
}
$.fn.dataTableExt.oApi.fnReloadAjax = function ( oSettings, sNewSource, fnCallback ){
if ( typeof sNewSource != 'undefined' ){
oSettings.sAjaxSource = sNewSource;
}
this.oApi._fnProcessingDisplay( oSettings, true );
var that = this;
oSettings.fnServerData( oSettings.sAjaxSource, null, function(json) {
/* Clear the old information from the table */
that.oApi._fnClearTable( oSettings );
/* Got the data - add it to the table */
for ( var i=0 ; i
But why do we have to define our own fnRefreshAjax function? I would have assumed that since DataTable advertises an Ajax feature that can be used to refresh data, the fnRefreshAjax would be part of the core library instead of being an extension?
The reason that it's not included in the standard DataTables library, is that by far the majority of users of DataTables would not use this function - hence it would just be dead weight most of the time. As such, it's an optional add on (as with all the other plug-ins, of which there is no shortage these days :-) ) that you can use if you have the need.
Regards,
Allan
I've tried various versions of the fnReloadAjax function that I have found in forum posts but I have not been able to get any of them to work. For example using the version above from stevemc's post, when fnServerData is invoked on initialization an alert with "aoData=" + aoData gives "aoData=" and everything works fine. Then when the fnReloadAjax function is called the alert in fnServerData is "aoData=null", which causes a webpage error (Error: 'null' is null or not an object) when trying to push data into it from user selections.
It seems to me that it is a pretty common use case to simply refresh the page by reloading data using the original ajax source settings. For example a page with various search filters that are pushed into aoData for the function call allows the user to refine the data displayed in the table without having to reload the entire page. I can get the refresh to work with server-side processing but I need to have all rows on the page for processing prior to doing some DB updates.
Any ideas how I can get rid of the null issue to try to get fnAjaxReload to work?
Thanks,
Jim
Disregard the last post. I now see that I totally misunderstood the customization of the fnReloadAjax function and it's obvious why aodata is null since it is being set to null in the above version of the function.
Regards,
Jim
[code]
$.fn.dataTableExt.oApi.fnReloadAjax = function ( oSettings, sNewSource, fnCallback, bStandingRedraw )
{
if ( typeof sNewSource != 'undefined' && sNewSource != null )
{
oSettings.sAjaxSource = sNewSource;
}
this.oApi._fnProcessingDisplay( oSettings, true );
var that = this;
var iStart = oSettings._iDisplayStart;
this.fnDraw();
}
[/code]
fnDraw() expects a boolean as parameter, not an instance of the DataTable object. This same line appears here: http://datatables.net/plug-ins/api#fnReloadAjax
Regards,
Allan
my table initialisation looks like this:
queueTable = $('#pendDataTable').dataTable( {
"sPaginationType": "full_numbers",
"bServerSide": true,
"bProcessing": true,
"aoColumns": [ null, null, null, null, null, null, {"sType": "currency"}, { "sType": "currency"} , {fnRender:fnCreateSelect},{ "bVisible": false },{ "bVisible": false },{ "bVisible": false },{ "bVisible": false },{ "bVisible": false },{ "bVisible": false },{ "bVisible": false },{ "bVisible": false },{ "bVisible": false },{ "bVisible": false },{ "bVisible": false }],
"bJQueryUI": true,
"sAjaxSource": "/eor/pend"
} );
for server side processing, call fnDraw (refresh the whole table) or fnUpdate (refresh a cell or row) to re-query your server side script
Does someone has a modification to fnReloadAjax() to allow for the reload to maintain the pagination, filtering and sorting?
@thebrowser: I thought there was one knocking around somewhere, but I can't see it at the moment. It's basically a combination of the fnStandingRedraw and fnReloadAjax plug-in that you are looking for (i.e. save paging location before the redraw). Sorting and filtering should already be maintained.
Allan
[code]$.fn.dataTableExt.oApi.fnReloadAjax = function ( oSettings, sNewSource, fnCallback, bStandingRedraw )[/code]
Thus the following code does the trick:
[code]oTable.fnReloadAjax(null, null, true);[/code]
As a reminder, it would be good for new users of the fnReloadAjax plugin if you could update the plugin page and replace:
[code]
that.fnDraw( that );
by
that.fnDraw();
[/code]
I had completely forgotten about the standing redraw option being added to fnReloadAjax! A useful option that :-).
And thanks for the reminder about the call to fnDraw - I've just modified the plug-in to do the correct thing.
Regards,
Allan
[code]
jQuery(document).ready(function() {
jQuery('#dynamic').html( '' );
availTable = jQuery('#example').dataTable( {
"bPaginate": false,
"bFilter": false,
"bInfo": false,
"aaData": aDataSet,
"aoColumns": [
{ "mDataProp": "description", "sTitle": "Available Roles", "bSortable": false }
],
"oLanguage": { "sEmptyTable": emptyMsg }
} );
.........
[/code]
These are my settings
var Otable = $("table.datatable2").dataTable2({
"sPaginationType": "full_numbers",
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "aaData.php",
"fnServerData": fnDataTablesPipeline
});
Otable.fnReloadAjax();
I am using the code listed on page http://datatables.net/plug-ins/api#fnReloadAjax
I have placed test alerts within the fnReloadAjax function and I noticed the code never makes it past the line of code 'this.oApi._fnServerParams( oSettings, aData );'
There is no javascript/console error message