From e014d9272faa506f4cc596243f7d701d2dd3d225 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Sat, 20 Apr 2013 09:10:46 +0100 Subject: [PATCH] New: ajax API methods: - Introducing several methods which will control the ajax aspects of DataTables through the API: - ajax.json() - get the last JSON returned from the server - ajax.reload() - reload from JSON source - ajax.url( [url] ) - get / set the Ajax URL - ajax.url( url ).load() - load data from new URL after a set - Note that this effectively replaces the old fnReloadAjax plug-in which was quite popular. - Not yet fully tested - further work required. --- media/js/jquery.dataTables.js | 121 +++++++++++++++++++++++++++++++++- media/src/DataTables.js | 1 + media/src/api/api.ajax.js | 120 +++++++++++++++++++++++++++++++++ 3 files changed, 241 insertions(+), 1 deletion(-) create mode 100644 media/src/api/api.ajax.js diff --git a/media/js/jquery.dataTables.js b/media/js/jquery.dataTables.js index 435242a6..f2de4a07 100644 --- a/media/js/jquery.dataTables.js +++ b/media/js/jquery.dataTables.js @@ -7383,7 +7383,6 @@ var _Api = DataTable.Api; - /** * Get the current page index. * @@ -7487,6 +7486,126 @@ } ); + }()); + + + + (/** @lends */function() { + + var _Api = DataTable.Api; + + var _reload = function ( settings ) { + if ( settings.oFeatures.bServerSide ) { + _fnDraw( settings ); + } + else { + // Trigger xhr + _fnBuildAjax( settings, [], function( json ) { + // xxx can this be reduced? + _fnClearTable( settings ); + + var data = _fnAjaxDataSrc( settings, json ); + for ( i=0 ; i 0 ) { + var xhr = ctx[0].jqXHR; + + if ( xhr ) { + return $.parseJSON( xhr.responseText ); + } + } + + // else return undefined; + } ); + + + /** + * Reload tables from the Ajax data source. + * + * @returns {DataTables.Api} this + */ + _Api.register( 'ajax.reload()', function () { + return this.tables( _reload ); + } ); + + + /** + * Get the current Ajax URL. Note that this returns the URL from the first + * table in the current context. + * + * @return {string} Current Ajax source URL + *//** + * Set the Ajax URL. Note that this will set the URL for all tables in the + * current context. + * + * @param {string} url URL to set. + * @returns {DataTables.Api} this + */ + _Api.register( 'ajax.url()', function ( url ) { + var ctx = this.context; + + if ( url === undefined ) { + // get + if ( ctx.length === 0 ) { + return undefined; + } + ctx = ctx[0]; + + return ctx.ajax ? + $.isPlainObject( ctx.ajax ) ? + ctx.ajax.url : + ctx.ajax : + ctx.sAjaxSource; + } + + // set + return this.tables( function ( settings ) { + if ( $.isPlainObject( settings.ajax ) ) { + settings.ajax.url = url; + } + else { + settings.ajax = url; + } + // No need to consider sAjaxSource here since DataTables gives priority + // to `ajax` over `sAjaxSource`. So setting `ajax` here, renders any + // value of `sAjaxSource` redundant. + } ); + } ); + + + /** + * Load data from the newly set Ajax URL. Note that this method is only + * available when `ajax.url()` is used to set a URL. Additionally, this method + * has the same effect as calling `ajax.reload()` but is provided for + * convenience when setting a new URL. + * + * @returns {DataTables.Api} this + */ + _Api.register( 'ajax.url().load()', function () { + // Same as a reload, but makes sense to present it for easy access after a + // url change + return this.tables( _reload ); + } ); + + }()); /** diff --git a/media/src/DataTables.js b/media/src/DataTables.js index 48f11af1..65ce5bd1 100644 --- a/media/src/DataTables.js +++ b/media/src/DataTables.js @@ -112,6 +112,7 @@ require('api.table.js'); require('api.draw.js'); require('api.page.js'); + require('api.ajax.js'); require('api.static.js'); /** diff --git a/media/src/api/api.ajax.js b/media/src/api/api.ajax.js new file mode 100644 index 00000000..54bb7d00 --- /dev/null +++ b/media/src/api/api.ajax.js @@ -0,0 +1,120 @@ + + +(/** @lends */function() { + +var _Api = DataTable.Api; + +var _reload = function ( settings ) { + if ( settings.oFeatures.bServerSide ) { + _fnDraw( settings ); + } + else { + // Trigger xhr + _fnBuildAjax( settings, [], function( json ) { + // xxx can this be reduced? + _fnClearTable( settings ); + + var data = _fnAjaxDataSrc( settings, json ); + for ( i=0 ; i 0 ) { + var xhr = ctx[0].jqXHR; + + if ( xhr ) { + return $.parseJSON( xhr.responseText ); + } + } + + // else return undefined; +} ); + + +/** + * Reload tables from the Ajax data source. + * + * @returns {DataTables.Api} this + */ +_Api.register( 'ajax.reload()', function () { + return this.tables( _reload ); +} ); + + +/** + * Get the current Ajax URL. Note that this returns the URL from the first + * table in the current context. + * + * @return {string} Current Ajax source URL + *//** + * Set the Ajax URL. Note that this will set the URL for all tables in the + * current context. + * + * @param {string} url URL to set. + * @returns {DataTables.Api} this + */ +_Api.register( 'ajax.url()', function ( url ) { + var ctx = this.context; + + if ( url === undefined ) { + // get + if ( ctx.length === 0 ) { + return undefined; + } + ctx = ctx[0]; + + return ctx.ajax ? + $.isPlainObject( ctx.ajax ) ? + ctx.ajax.url : + ctx.ajax : + ctx.sAjaxSource; + } + + // set + return this.tables( function ( settings ) { + if ( $.isPlainObject( settings.ajax ) ) { + settings.ajax.url = url; + } + else { + settings.ajax = url; + } + // No need to consider sAjaxSource here since DataTables gives priority + // to `ajax` over `sAjaxSource`. So setting `ajax` here, renders any + // value of `sAjaxSource` redundant. + } ); +} ); + + +/** + * Load data from the newly set Ajax URL. Note that this method is only + * available when `ajax.url()` is used to set a URL. Additionally, this method + * has the same effect as calling `ajax.reload()` but is provided for + * convenience when setting a new URL. + * + * @returns {DataTables.Api} this + */ +_Api.register( 'ajax.url().load()', function () { + // Same as a reload, but makes sense to present it for easy access after a + // url change + return this.tables( _reload ); +} ); + + +}()); +