Syn Syn - 2 months ago 22
jQuery Question

How does DataTables output a table with server side processing?

I am using DataTables server side (https://datatables.net/examples/server_side/) to create a paginated table from a MySQL table.

I have created a simple table using DataTables: http://clients.serallo.co.uk/formtest/

I would like to work out how DataTables generates the table, as I cannot seem to find any occurrence of

<td>
in the script.

DataTables currently outputs something similar to this:

<tbody>
<tr class="odd" role="row">
<td class="sorting_1">[mysql result]</td>
</tr>
</tbody>


I want to be able to add extra code alongside the MySQL output. For example, to turn the output into a hyperlink:

<tbody>
<tr class="odd" role="row">
<td class="sorting_1"><a href="[mysql result]">[mysql result]</a></td>
</tr>
</tbody>


But I cannot do this as I cannot work out where DataTables is outputting the HTML code.

Any suggestions?

Answer

You need to use render for this:

$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "pull_2.php",
        "columns": [
            {
               "render": function (data, type, full, meta) {
                    return "<a href=" + full[0] + ">" + full[0] +"</a>"
             }
    });
});

In this example let's say you are returning a dataset with 1 column of data. Within render, you build the hyperlink with html and an indexed reference to the row of data. full represents the entire row.

If you were returning multiple columns, you could build the hyperlink with different fields, e.g. an Id for the hyperlink, and a string for the link text.