Search code examples
ajaxdatatabledatatablesserver-sidedatatables-1.10

Datatables generate column and get row id Server-side


I have got a datatable using Datatables server-side. I have created and filled the table as shown below. Now I need to generate column with button but i need to have the id of the row ..

My data returned :

{"data":[{"id":"13","name":"gerrard","adress":"new york"}, .... }

I want something like if i click in this button it show me the id of the row .. There is my code :

<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>name</th>
                <th>adress</th>
            </tr>
        </thead>
</table>

$(document).ready(function() {
    var oTable = $('#example').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "server-side-process",
            },

        "columns": [
                    { "data": "name" },
                    { "data": "adress" },
                ]
    } );

} );

I found this example in the documentation but i cant get the id from the data returned .. it is possible to do it please ? thank you ..


Solution

  • You need to use the render property:

    "render": function ( data, type, full) {
                return "<button type='button' class='btn btn-success' data-id=" + full[3] + ">PAY</button>";
        }
    

    The full parameter is the full datasource for the data row, so you can access it by index. In your case you would use full[0] as it's in the first position.

    Here is a working example which uses different data, but you should be able to see how it works.