15

I've been trying to get DataTables to work with my existing Ajax search function - which works by itself.

I have the following code:

        $('#SearchResults').dataTable({
            "bProcessing": true,
            "bServerSide": true,
            "bRetrieve": true,
            "sAjaxSource": "process.php?action=searchArtifact",
            "fnServerData": function (sSource, aoData, fnCallback){
                aoData.push({
                    "name": "searchName",
                    "value": $('#ArtifactSearch').attr('value')
                });
                $.ajax({
                    "dataType": "json", 
                    "type": "POST", 
                    "url": sSource, 
                    "data": aoData, 
                    "success": fnCallback
                });

            }
        });

The PHP is returning a valid JSON object (using JSON_FORCE_OBJECT):

{"0":{"ARTIFACT_ID":"4E2FE3BCE356C","ARTIFACT_NAME":"123","ARTIFACT_TYPE":"UI","ARTIFACT_LABEL":"Test_Int_EAS_123","ARTIFACT_LOCATION":"Int","ARTIFACT_DOMAIN":"ABC","ARTIFACT_AUTHOR":null,"REGISTERED_EMAIL":"[email protected]","REGISTERED_DATE":"27-07-2011","REGISTERED_TIME":"11:09:00"}

I can see this all fine in FireBug, but my empty table is not being populated with this data.

Any ideas?

@Kyle: Errr - thats it. I guess I don't have one? This is my first attempt (struggle) with DataTables and I'm just copying from the documentation: http://www.datatables.net/usage/callbacks#fnServerData

@MarcB: Added that - but still no data displayed. Thanks for the help

4
  • Would you add your fnCallback function your post, please? Commented Aug 11, 2011 at 17:58
  • success: function(data) { fnCallback(data); } to explictly pass over the returned data? Commented Aug 11, 2011 at 18:16
  • can you show us the PHP source code that return the result? Because I think it didn't give the proper format requested here datatables.net/usage/server-side Commented Jun 23, 2012 at 20:56
  • I made a simple tutorial that explains well how to solve your issue, check [this link ](refreshmymind.com/datatables-dom-php-ajax-mysql-datasources) Commented Dec 3, 2015 at 10:30

7 Answers 7

1

I was having a similar problem. Turns out I wasn't forming the JSON response properly. This worked for me:

<?php

$arr = array ('aaData' => array(
array('3','35','4', '$14,500', '$15,200','$16,900','5','1'),
array('1','16','4', '$14,200', '$15,100','$14,900','Running','1'),
array('5','25','4', '$14,500', '$15,600','$16,900','Not Running','1')
)
);

echo json_encode($arr);
?>
Sign up to request clarification or add additional context in comments.

Comments

0

This plugin expects the returned JSON object to be an object, with a property which is an array of arrays. This property should be called 'aaData'. You are not returning an object; you are just returning the array.

Comments

0

Check out this json resource example from DataTables.net: http://datatables.net/examples/examples_support/json_source.txt. Notice that you are returning json with brackets as compared to the example's braces.

Comments

0

you can remove the $.ajax part, instead you can use the $.getJSON method.

Comments

0

You can also add the following to avoid adding an extra object like "aaData":

"sAjaxDataProp": ''

Comments

0

What are you setting sEcho to?

Your JSON should have this structure:

{
  "sEcho": 'refer to "sEcho" in $_GET or $_POST; don't forget to sanitize', 
  "iTotalRecords": 'for pagination',
  "iTotalDisplayRecords": 'number displayed',
  "aaData" => { /* your data here */ }
}

Comments

0

This is a few years late but might help someone. :)

DataTable cannot render null values. See defaultContent to set content when data return is null.

See link: https://datatables.net/reference/option/columns.defaultContent

For legacy dataTables, see http://legacy.datatables.net/ref and search for sDefaultContent

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.