datatables.fnReloadAjax.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * By default DataTables only uses the sAjaxSource variable at initialisation time,
  3. * however it can be useful to re-read an Ajax source and have the table update.
  4. * Typically you would need to use the fnClearTable() and fnAddData() functions,
  5. * however this wraps it all up in a single function call.
  6. *
  7. * @param {[type]} oSettings [description]
  8. * @param {[type]} sNewSource [description]
  9. * @param {[type]} fnCallback [description]
  10. * @param {[type]} bStandingRedraw [description]
  11. * @return {[type]} [description]
  12. */
  13. $.fn.dataTableExt.oApi.fnReloadAjax = function ( oSettings, sNewSource, fnCallback, bStandingRedraw )
  14. {
  15. if ( sNewSource !== undefined && sNewSource !== null ) {
  16. oSettings.sAjaxSource = sNewSource;
  17. }
  18. // Server-side processing should just call fnDraw
  19. if ( oSettings.oFeatures.bServerSide ) {
  20. this.fnDraw();
  21. return;
  22. }
  23. this.oApi._fnProcessingDisplay( oSettings, true );
  24. var that = this;
  25. var iStart = oSettings._iDisplayStart;
  26. var aData = [];
  27. this.oApi._fnServerParams( oSettings, aData );
  28. oSettings.fnServerData.call( oSettings.oInstance, oSettings.sAjaxSource, aData, function(json) {
  29. /* Clear the old information from the table */
  30. that.oApi._fnClearTable( oSettings );
  31. /* Got the data - add it to the table */
  32. var aData = (oSettings.sAjaxDataProp !== "") ?
  33. that.oApi._fnGetObjectDataFn( oSettings.sAjaxDataProp )( json ) : json;
  34. for ( var i=0 ; i<aData.length ; i++ )
  35. {
  36. that.oApi._fnAddData( oSettings, aData[i] );
  37. }
  38. oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
  39. that.fnDraw();
  40. if ( bStandingRedraw === true )
  41. {
  42. oSettings._iDisplayStart = iStart;
  43. that.oApi._fnCalculateEnd( oSettings );
  44. that.fnDraw( false );
  45. }
  46. that.oApi._fnProcessingDisplay( oSettings, false );
  47. /* Callback user function - for event handlers etc */
  48. if ( typeof fnCallback == 'function' && fnCallback !== null )
  49. {
  50. fnCallback( oSettings );
  51. }
  52. }, oSettings );
  53. };