How to use Datatables server-side processing with Google Apps Script

旧巷老猫 提交于 2019-12-08 03:41:55

问题


I'm trying to use datatables in server-side processing mode whereby data is loaded in batches, asynchronously. This is very useful for massive datasets. The documentation is clear enough (https://datatables.net/examples/data_sources/server_side.html), however, I'm struggling to work out how to implement it within Google Apps Script and its HTMLService.

What I'm currently using (which is working) is datatables loading all the data in at once:

$(document).ready(function() {
  google.script.run.withSuccessHandler(loadLogList).getLogList();
});

function loadLogList(data) {
  if (data) {
    for (var i = 0; i < data.length; i++) {
      htmlString += "<tr><td>" + data[i][0] + "</td>";
      htmlString += "<td>" + data[i][1] + "</td>";
      htmlString += "<td>" + data[i][2] + "</td>";
      htmlString += "<td>" + data[i][3] + "</td></tr>";
    }
    $("#LogListBody").html(htmlString);
  }

  var table = $("#LogList").DataTable( {
    "columnDefs": [
      { "orderData":[ 0 ],   "targets": [ 1 ] },
      {
      "targets": [ 0 ],
      "visible": false,
      "searchable": false
      } ],
    "paging": true,
    "select": true
  });
}

The documentation suggests that datatables needs initialising like this:

$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "../server_side/scripts/server_processing.php"
    } );
} );

So somehow I need to provide "ajax" with "google.script.run". Any ideas? I then need to write a function on the server-side (Code.gs) to return json-formatted data. If anyone has sample code for this also I'd be very grateful.

Thanks


回答1:


Looking at the docs ajax can be configured in a few different way. One is by providing a custom function. https://datatables.net/reference/option/ajax

Edit based on more info from op. This is the very basic setup:

$('#example').dataTable( {
  "processing": true,
  "serverSide": true, 
  "ajax": function (data, callback, settings) {
    google.script.run.withSuccessHandler(callback).getLogList(settings);
  }
} );


来源:https://stackoverflow.com/questions/40505161/how-to-use-datatables-server-side-processing-with-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!