0

I'm trying to migrate to DataTables Table plug-in for jQuery

What is the right way to make below json Results to work with datatables's Object data source? I tried the nested Object data source example and others, but still not working:

{
 "query": {
 ...
  "diagnostics": {
   "publiclyCallable": "true",
   "url": {
    "execution-start-time": "1",
...
   },
   "user-time": "68",
  },
  "results": {
   "item": [
    {
     "title": "",
     "description": 
......

I tried this:

$('#example').DataTable( {
    "processing": true,
    "serverSide": true,
    "ajax": "scripts/objects.php",
    "columns": [
        { "query.results.item": "title" },
        { "query.results.item": "description" },
        { "query.results.item": "position" },
        { "query.results.item": "office" },
        { "query.results.item": "start_date" },
        { "query.results.item": "salary" }
    ]

Error is:

SyntaxError: missing ; before statement

{"query":{"count":16,"created":"2016-03-30T02:41:49Z","lang":"en-US"

Thanks a lot!

1
  • you forgot a ; in the code Commented Mar 30, 2016 at 4:47

2 Answers 2

0

SyntaxError: missing ; before statement

This is runtime JavaScript error, nothing to do with jquery,datatable.

You most likely have an extra ).

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

1 Comment

I think it is not the correct answer, because I have faced this kind of errors but not for this problem of just missing a ;
0

A simple way of using Data-tables is by this way-

Server side code (in PHP)-

$data = array();
while( $row=mysqli_fetch_array($query) )
{  // Preparing an array For Returning Reponce
    $nestedData=array(); 
    $nestedData = array (
                            "employee_name"         =>  $nestedData[] = $row["employee_name"],
                            "employee_salary"       =>  $nestedData[] = $row["employee_salary"],
                            "employee_age"          =>  $nestedData[] = $row["employee_age"]
                        );
    $data[] = $nestedData;
}
$json_data  =   array(
                        "draw"            => intval( $draw_request_code ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
                        "recordsTotal"    => intval( $totalData ),  // total number of records
                        "recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
                        "data"            => $data   // total data array
                    );
echo json_encode($json_data);  // send data as json format

And JS for that server side will be like this-

var dataTable = $('#employee-grid').DataTable(
{
    "processing": true,
    "serverSide": true,
    "ajax":
    {
        url :"employee-grid-data.php", // json datasource
        type: "post",  // method  , by default get
        error: function()
        {  // error handling
            $(".employee-grid-error").html("");
            $("#employee-grid").append('<tbody class="employee-grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>');
            $("#employee-grid_processing").css("display","none");
        }
    },
    "columns":  [               //Name should be same as PHP file JSON NAmes and ordering should be as in the HTML file
                    {   "data": "employee_name"         },
                    {   "data": "employee_salary"       },
                    {   "data": "employee_age"          }
                ]
});

A simple example for your purpose is given in this GitHub code.

More simple examples are given here.

And a advanced use of that plugin is given in this GitHub repo.

Even more can be found in this documentations.

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.