0

I try to create an jquery_dataTable. It works quite well with the documentation https://datatables.net/examples/api/row_details.html

Now I try to change the call from "ajax": "objects.txt" in "ajax": "some.php" ändern.

My HTML-Table:

<table id="systeme" class="display" style="width:100%">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
        </tr>
    </thead>
    <tbody></tbody>
    <tfoot>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
        </tr>
    </tfoot>
</table>

The ajax-call to recieve the data is:

$(document).ready(function() {
    $.ajax({
    type : 'POST',
    url  : 'some.php',
    dataType: 'json',
    //cache: false,
    success :  function(result)
    {
      console.log(result);
      $('#systeme').DataTable({
        "searching": false, 
        "aaData": [result], //get the array data from the ajax call.
        "aoCcolumns": [
           {
             "className":      'details-control',
             "orderable":      false,
             "data":           null,
             "defaultContent": ''
           },
             { "result": "ID" },
             { "result": "Name" },
             { "result": "Email" }
         ],
         "order": [[1, 'asc']]
        });
    }
});

In the PHP file I connect to a database and recieve the information.

$conn = connectDB();
$dataArray = array();
$sql = "SELECT ID, Name, Email FROM `person` WHERE 1";
$result = $conn->query($sql);
if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $dataArray[] = $row["ID"];
        $dataArray[] = $row["Name"];
        $dataArray[] = $row["Email"];
    }
}
closeDB($conn);

echo json_encode($dataArray);

When I check the loggs I recieve all relevant data. They´re formatted like

0: "1"
1: "Tom"
2: "mail@mail"
3: "2"
4: "Tim"
5: "mail@mail"
6: "3"
7: "Daniel"
8: "mail@mail"

But inside of my table is only one entrie (the first entrie). I don´t know how to format the json file correct or to handly the data right. I tried a lot and for many hours to come to this point, but now I need some help.

I´m pretty new in all of this stuff and an answer would be great

thanks

Timo

4
  • I think response data must be array of objects not text list Commented Nov 18, 2019 at 13:21
  • Datatables do have their own ajax handler which just reads the json datatables.net/examples/data_sources/ajax Try using that Commented Nov 18, 2019 at 13:29
  • Dojing it with the dt-ajay-notation works well, but only with an formattet txt-File. My problem was to format the data in the correct form... Commented Nov 18, 2019 at 13:50
  • change aoCcolumns to aoColumns, hope it works Commented Nov 20, 2019 at 10:37

2 Answers 2

1

You need to remove square bracket from result (DataTable's property aaData) and should show like :

 "aaData": result,

And need other changes in your PHP file (as you have not encode JSON string properly via while loop) :

$conn = connectDB();
$sql = "SELECT ID, Name, Email FROM `person` WHERE 1";
$result = $conn->query($sql);
if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $dataArray = array();
        $dataArray[] = $row["ID"];
        $dataArray[] = $row["Name"];
        $dataArray[] = $row["Email"];
        $dataResult[] = $dataArray; 
    }
}
closeDB($conn);

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

Comments

0

Your data i thinks it should be set like this :

 while($row = $result->fetch_assoc()) {
    $dataArray[] = array($row["ID"], $row["Name"], $row["Email"] );        
}

1 Comment

No, than I have in every column "id" "name" and "email" 3 entries.

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.