How customizable is the datatables server side processing model

How customizable is the datatables server side processing model

srozgasrozga Posts: 4Questions: 2Answers: 0

We have an existing REST API to provide paginated, sorted JSON sets of objects for an app. We would like to utilize this same endpoint to populate a datatables.net instance and do server side processing. I noticed that the server side processing expects very specific names and behaviors and I was wondering how much flexibility there is in terms of adapting existing interfaces to datatables? I don't want to change what porperties, etc my service returns but I still need this functionality. What are my options?

One thought that occured is that underneath datatables maybe I can plug into the $.ajax().then promise and do some transformation there from my format to what datatables expects? Is that possible? Can one do something similar with requests?

Thanks

This question has an accepted answers - jump to answer

Answers

  • wjhumphreyswjhumphreys Posts: 52Questions: 9Answers: 5

    Can you give an example?

  • srozgasrozga Posts: 4Questions: 2Answers: 0

    Sure, at the most basic level our API supports Offset and Count arguments. Response returns a TotalCount, Count, Offset and Data array of rows.

  • wjhumphreyswjhumphreys Posts: 52Questions: 9Answers: 5
    edited June 2014 Answer ✓

    There are many ways you could do this and none are particularly right or wrong. If the parameter names from DataTables dont match your API parameters (which I assume you cannot change) Rather than mess with DataTables code which would be an update pain. I personally would create my own proxy API which simply received the DataTables parameters and converted them to what your API requires before calling your API and then the same in reverse.

    If you are using .net MVC lets say you could simply create

    Sorry for the one line format but I haven't worked out how to paste code on here so it keeps its format..............


    public class JQueryDataTablesContext { /// <summary> /// Draw counter. This is used by DataTables to ensure that the Ajax returns from server-side /// processing requests are drawn in sequence by DataTables (Ajax requests are asynchronous /// and thus can return out of sequence). This is used as part of the draw return parameter. /// </summary> public Int32 draw { get; set; } /// <summary> /// Paging first record indicator. This is the start point in the current data set /// (0 index based - i.e. 0 is the first record). /// </summary> public Int32 start { get; set; } /// <summary> /// Number of records that the table can display in the current draw. It is expected that /// the number of records returned will be equal to this number, unless the server has /// fewer records to return. Note that this can be -1 to indicate that all records should /// be returned (although that negates any benefits of server-side processing!). /// </summary> public Int32 length { get; set; } } public ActionResult MyProxy( JQueryDataTablesContext jQueryDataTablesContext ) { Int32 totalRowCount = 0; var myData = from f in CallTheAPI( jQueryDataTablesContext.start, jQueryDataTablesContext.length, out totalRowCount ) select new[] { new[] { f.ID }, new[] { f.WHATEVER } }; return Json( new { draw = jQueryDataTablesContext.draw, // Draw counter. This is used by DataTables to ensure that the Ajax returns from server-side processing requests are drawn in sequence by DataTable. recordsTotal = totalRowCount, // Total records, before filtering (i.e. the total number of records in the database) recordsFiltered = totalRowCount, // Total records, after filtering (i.e. the total number of records after filtering has been applied) data = myData // Return data. }, JsonRequestBehavior.AllowGet ); }
  • srozgasrozga Posts: 4Questions: 2Answers: 0

    That is the very approach I was thinking. But you are saying that the only way to do this client side would involve touching the datatables source, correct? Definitely don't wanna modify it.

  • wjhumphreyswjhumphreys Posts: 52Questions: 9Answers: 5
    edited June 2014

    I cant say this 100% but I would say yes from what Ive played with.

    I personally would want more control anyway over an API I couldn't change.

    This way you can add authentication if required and you have more control over the fields that are being returned.

This discussion has been closed.