Skip to content

Instantly share code, notes, and snippets.

@mysiar
Created October 1, 2018 20:11
Show Gist options
  • Save mysiar/37929423da543bb5e68342fa9635551f to your computer and use it in GitHub Desktop.
Save mysiar/37929423da543bb5e68342fa9635551f to your computer and use it in GitHub Desktop.
Api-Platform - DataTable

api-platform/api-platform#845 (comment)

Just to let you know: it seems the easiest way was to reorder the JSON response on the client side, using the dataFilter setting in jQuery.ajax():

$('#mytable').DataTable({
        'serverSide': true,
        'ajax': {
            'url': '/api/my_api',
            'headers': { 'Accept': "application/ld+json" },
            'dataFilter' : function (data) {
                var json = JSON.parse(data);
                json.recordTotal = json['hydra:totalItems'];
                json.recordsFiltered = json['hydra:totalItems'];
                json.data = json['hydra:member'];

                return JSON.stringify(json);
            }
        }
);

This means converting the JSON back to an object and then to a string again, though, but it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment