Is there an easy way to use my own AJAX logic?

Is there an easy way to use my own AJAX logic?

timwistimwis Posts: 2Questions: 0Answers: 0
edited September 2012 in General
I'm trying to use DataTables to fetch my data from cloudmine.me (similar to iriscouch). It has its own JS library with a get() function where you provide parameters like query, limit, skip, etc. I can't seem to get DataTables able to interact with it.

It seems like DataTables can only interact with a very specific type of server side script that accepts parameters like iDisplayLength and iSortCol_0.

It looks like the first step is to override fnServerData, where I'd like to put something like cloudmine.get({sort:'column_name:dir', limit: x, skip: x}); but to get those 3 variables I'd have to swim through the aoData array and match certain pieces to others (like how I'd get the sort column name).

Apart from this, I really like DataTables. Is there an easier way to do this?

Replies

  • timwistimwis Posts: 2Questions: 0Answers: 0
    edited September 2012
    Looks like I got it working through a work-around. I'd still be interested to know if there's an easier way to do it.

    For anyone interested, here's what I did to convert the key/value array to a regular object

    [code]
    function unserializeArray(dataArray) {
    var dataObj = {};
    $.each(dataArray, function(key, val) {
    dataObj[this.name] = this.value;
    });
    return dataObj;
    }
    [/code]

    Then in my .dataTable init I do:

    [code]
    fnServerData: function(url, aoData, callback) {
    var data = unserializeArray(aoData);
    var options = {
    sort: data['mDataProp_' + data.iSortCol_0] + ':' + data.sSortDir_0,
    limit: data.iDisplayLength,
    skip: data.iDisplayStart,
    snippet: 'messages_array', // converts JSON result to an array of objects
    count: true
    };
    ws.search('[_type = "message"]', options)
    .on('result', function(data, response) {
    callback({
    aaData: data,
    iTotalRecords: response.count,
    iTotalDisplayRecords: response.count
    });
    });
    }
    [/code]

    It works.
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    > It seems like DataTables can only interact with a very specific type of server side script that accepts parameters like iDisplayLength and iSortCol_0.

    If you want to use server-side processing, then yes - the server-side script must implement an interface as described here: http://datatables.net/usage/server-side . However, for client-side processing with Ajax sourced data then no, virtual any JSON data source is directly supported through the use of mData: http://datatables.net/blog/Extended_data_source_options_with_DataTables .

    If you want to override the Ajax that DataTables does by default, then fnServerData is the right way to do :-)

    Allan
This discussion has been closed.