0
votes

I'm using DataTables to display data returned from an ajax call. DataTables returns the data, but without dividing it into pages and at the bottom, it says, "Showing 0 to 0 of 0 entries (filtered from NaN total entries)" and the buttons at the bottom say, "Previous 1 2 3 4 5 ... NaN Next". So something isn't right, but I'm not sure what.

The data is returned from a PHP page with a sql call in this format:

[{hospital_name: "Advanced Surgical", raw_description: "Removal of breast lesion",…},…]
[0 … 99]
0: {hospital_name: "Advanced Surgical", raw_description: "Removal of breast lesion",…}
full_payer_name: "Aetna Auto: Medical OP"
hospital_name: "Advanced Surgical"
plan_type: "Commercial"
price: "$2,968.40"
raw_description: "Removal of breast lesion"
...(136 records in the same format as 0 above)

Here is my DataTables call:

$('#results').DataTable( {
                "processing": true,
                "serverSide": true,
                searching: false,
                "ajax": {
                    "url": "./php/getDataForCode.php",
                    "type": "POST",
                    "data": {code: $code}, //a variable that I pass to the query
                    "dataSrc": "" 
                },
                columns: [
                { title: "Hospital", data: "hospital_name" },
                { title: "Description", data: "raw_description" },
                { title: "Insurer", data: "full_payer_name" },
                { title: "Insurer type", data: "plan_type"},
                { title: "You Pay", data: "price" }
                ]
        } );

Can anyone tell me what I'm doing wrong to parse the data?

1
You are using the serverSide: true option. So, is your server-side code handling pagination (and filtering and sorting)? When you use this option, DataTables no longer handles any of these functions. Instead it delegates all of these to the server (to allow very large data sets to be handled). See here for some background notes, with links to documentation and examples. - andrewJames
Do you need serverSide: true? Do you have thousands/millions of rows of data? - andrewJames
@andrewjames: You just solved the problem!! I thought I had to use serverSide: true in order to use ajax. Please submit this comment as an answer so I can mark it. Thank you!! - LauraNMS

1 Answers

1
votes

In your case, if you do not have an excessively large volume of data, you do not need to use serverSide: true. Set it to false, or just remove it entirely (the default is false).

The server-side approach is intended for those cases where DataTables and your browser will not be able to handle the entire data set via a single Ajax fetch - for example, if there is too much data for reasonable performance.

On the other hand, using Ajax in a DataTable is a separate option. Using Ajax is one common way to populate your table (whether or not you use server-side processing). You can see links to examples on this "Data Sources" page.