Datatables AJAX

Datatables AJAX

derapederape Posts: 10Questions: 0Answers: 0
edited August 2011 in General
Hey there,

I know there are probably a lot threads about this topic, but I need some clarification.
AJAX initially has nothing to do with server side processing of the data right?
So what I want to do is, get the data from my server pass it into the datatable and use all the functionality (sorting, searching, etc).
Is the only difference between server side and non-server side is that when I'm doing it server side, only a subset of my data is in my DOM and if I'm doing it non-server side the whole data is in my DOM, correct?

Regards

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    1) correct. the AJAX source in DataTables is just getting a JSON data to use for the aaData array, but allowing it to be stored in an external file, accessed by url.

    2) correct. AJAX sourced data pulls in all your data at once, then you use client-side searching, sorting, pagination, etc. server-side sourced DataTables puts these functions on the server side in the case that your data would be too large to effectively deal with on the client side using javascript.
  • derapederape Posts: 10Questions: 0Answers: 0
    Okay cool, if I just want to use Ajax I don't necessarily need to pull that from a file right?
    So for example I've got something like this

    [code]
    $(document).ready(function () {
    $('#jugendamtTable').dataTable({
    "bProcessing": true,
    "sAjaxSource": "/MyAjaxURL"
    });
    });
    [/code]

    What I read so far is that I need to return a JSON String. What exactly does my service need to return? Just something like this?

    [code]
    { "aaData": [
    ["data1","data2","data3", ...]
    ...
    ]
    }
    [/code]
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    yes, I think your format is correct. here is a full example of an ajax-source file: http://www.datatables.net/release-datatables/examples/ajax/sources/arrays.txt

    if you don't want to pull from a file, you don't need AJAX. you could put the data in an array on the client side javascript (this could be generated by a PHP script if you wish).
    http://www.datatables.net/release-datatables/examples/data_sources/js_array.html
  • derapederape Posts: 10Questions: 0Answers: 0
    I'm using the ASP.Net Framework so I'm not sure what's the best way to get the data into the table...
    I also could read it from a URL which returns a string could I?
    Are there any reasons what I should do? (performance or whatever)
  • derapederape Posts: 10Questions: 0Answers: 0
    I was thinking about the storage in a java-script array.
    Isn't that a bad idea, because u would have the data in the array and redundant in the table also...
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    If you are doing updates, database is recommended.
  • derapederape Posts: 10Questions: 0Answers: 0
    well I do, but I still wont need server side processing right.
    I have trouble setting up the method which should return the data.
    Does it have the same format as the array in the arrays.txt file, or just JSON format?
  • derapederape Posts: 10Questions: 0Answers: 0
    I'll answer my own question
    It has to be the arrays.txt format :-)
  • derapederape Posts: 10Questions: 0Answers: 0
    edited August 2011
    In case someone wants to know here's the ASP.Net MVC (3) implementation:

    HTML
    [code]

    $(document).ready(function () {
    $('#yourTable').dataTable({
    "bProcessing": true,
    "sAjaxSource": "/YourController/YourAction",
    });
    });






    Header1


    Header1






    [/code]

    Make sure you have the correct table headers, or specify them in the javascript initializer

    C# Code
    [code]
    public class YourController: Controller
    {

    public String YourAction()
    {
    // get your data

    // build string like that
    // { "aaData": [
    // ["Data1","Data2"],
    // ["Data1","Data2"]
    // ] }

    return dataString;
    }
    }
    [/code]
This discussion has been closed.