0

This is my datatable that works with a json file:

var table = $('.table').DataTable({
     "lengthMenu": [[10, 25, 50,100, -1], [10, 25, 50,100, "All"]],
     "pageLength": 10,
     "data":{{ output.data|raw }},

It works well like this.

To get the datatable faster I added this lines:

var table = $('.table').DataTable({
     "serverSide": true,
     "lengthMenu": [[10, 25, 50,100, -1], [10, 25, 50,100, "All"]],
     "pageLength": 10,
     "data":{{ output.data|raw }},
     "processing": true,

Now I get the error

DataTables warning: table id=DataTables_Table_0 - Invalid JSON response

The json comes directly from a json file with data.

4
  • 2
    If you set serverSide: true then you should not use data. Provide the URL to the JSON file in the ajax property. See the documentation Commented Feb 5, 2020 at 16:55
  • @RoryMcCrossan I removed data and added ajax with the correct link to the json file, but it is no datatable is loaded now :( Commented Feb 5, 2020 at 17:20
  • Check the console for errors Commented Feb 5, 2020 at 17:20
  • The error is TypeError: c is undefined Commented Feb 5, 2020 at 17:23

1 Answer 1

1

The documentation on the serverSide option explains:

DataTables has two fundamental modes of operation:

  • Client-side processing - where filtering, paging and sorting calculations are all performed in the web-browser.

  • Server-side processing - where filtering, paging and sorting calculations are all performed by a server.

serverSide: true means that the raw data, as a whole, does not exist on the client. The client only holds sorted and paginated views of the data that are sent from an HTTP endpoint made to communicate those pages to a client.

If you send all the data to the client, you probably don't intend to use server-side processing.

If your problem is that sorting and paginating the data is too slow (because your data consists of many hundreds or thousands of records), then you probably do want switch to server-side processing. The first step is to stop specifying a data option and instead use an ajax option with a target URL. Then you need to write a server-side endpoint at the URL that performs the necessary transformations when requested. See the DataTables example documentation on server side processing to get started.

Sign up to request clarification or add additional context in comments.

3 Comments

you mean when I use this "serverSide": true, "ajax": "data.json",it should work
@peace_love If data.json is a server-side endpoint that is prepared to alter its responses based on the parameters sent to it, then yes. Otherwise, no. When you say ajax": "data.json" you are telling the DataTables client to send a request like data.json?start=10&length=5&... and expects back a response like {"draw":4,"recordsTotal":57,"recordsFiltered":57,"data":[ ... ]} where the data field is a rendered, trimmed and sorted view of your data on the server. Have you written server-side code to do data processing? If not, you do not intend to use DataTable's server-side processing.
Ah, now I understand, very good explaination. my data.json starts like this: [{"id":536,"name":"Alfred",... So I have to add the format into the file right? and than it should work?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.