API to get Ajax URL

API to get Ajax URL

PetahPetah Posts: 12Questions: 0Answers: 0
edited September 2011 in General
Is there anyway to get the URL that would be used when doing an Ajax update (server side tables), including all the data on sorting, filtering, etc.

The reason I ask is because I have implemented server side export for very large tables (million+ rows) but currently it just exports all the data. If I could get the URL the would normally be used, then PHP could do the filtering and sorting for the export too.

I am using Table Tools for the export buttons:

[code]
TableTools.BUTTONS.si_xls = $.extend({}, TableTools.BUTTONS.xls, {
sAction: 'text',
sToolTip: 'Export to a Microsoft Excel XLS file (use CSV if the table is large)',
fnClick: function(button) {
var source = $(button).parents('.dataTables_wrapper').find('table:first').dataTable().fnSettings().oInit.sAjaxSource;
window.location = SiteIgnite.URL.add_get(source, 'export', 'xls');
}
});
[/code]

So I need to replace the source var with the full Ajax URL with all the sorting and filtering GET parameters

Replies

  • GregPGregP Posts: 499Questions: 10Answers: 0
    Your script on the server side should be able to retrieve the full set of parameters and just send them right back.

    PHP has a function for getting the full query string. Just tack this into the JSON that you send back, and then in your success function, pass it into a JavaScript variable for using in the export function.

    I know that's not a fully-fleshed-out idea, but hopefully it helps. ;-)
  • allanallan Posts: 63,262Questions: 1Answers: 10,423 Site admin
    I presume that it is this like that you want to optimise a bit:

    [code]
    var source = $(button).parents('.dataTables_wrapper').find('table:first').dataTable().fnSettings().oInit.sAjaxSource;
    [/code]

    What you can do is use the fact that the TableTools callback functions are executed with the TableTools instance scope, and (a bit of insider knowledge...) there is a parameter attached to that called "s.dt" which is the DataTables settings object for that table. So what you end up with is:

    [code]
    var source = this.s.dt.sAjaxSource;
    [/code]

    I know its a little contrived to know this at the moment - I'm working on making the TableTools documentation more complete (like FixedColumns, which has this kind of information in it: http://datatables.net/docs/FixedColumns/2.0.1/ ), but that's possibly the cleanest way to do it :-)

    Actually - sorry - I just read your post again... I'm going to leave the above since it is relevant, but actually answer your question here... :-)

    What you can do to get the full set of information that DataTables would send to the server is call the internal function _fnAjaxParameters() - it is exposed by the oApi parameter. So:

    [code]
    var data = this.s.dt.oApi._fnAjaxParameters( this.s.dt );
    [/code]

    Again, this isn't something that you would expect to find from the documentation since it's an internal function (and thus might change a bit from version to version - but I think it very unlikely to change before 2.0!), but that should get you the information you need to make the Ajax call.

    Regards,
    Allan
  • PetahPetah Posts: 12Questions: 0Answers: 0
    Thanks Allan,

    I noticed that function is relatively new, as I had to upgrade and I was only 1 point behind ;)

    Here is the code I used if any future visitors need it:

    [code]
    TableTools.BUTTONS.si_csv = $.extend({}, TableTools.BUTTONS.csv, {
    sAction: 'text',
    sToolTip: 'Export to comma separated values (CSV) (compatible with Microsoft Excel)',
    fnClick: function(button) {
    var source = this.s.dt.sAjaxSource;
    source = SiteIgnite.URL.add_get(source, 'export', 'csv');
    var data = this.s.dt.oApi._fnAjaxParameters(this.s.dt);
    $.each(data, function() {
    source = SiteIgnite.URL.add_get(source, this.name, this.value);
    });
    window.location = source;
    }
    });
    [/code]

    Also thanks for the this.s.dt shortcut :D
This discussion has been closed.