jquery & DataTables: custom field rendering (url example )

Trabla: jquery & DataTables: custom field rendering (urls, buttons etc)

DataTables is a plug-in for the jQuery Javascript library - http://datatables.net/

Solving:

Server returns json with postId and authorId.
Custom render functions form correct link and display in table.

$(document).ready(function() {

    var server = 'http://mysite.com/index.php';
    var postUrl= 'http://mysite.com/post.php?id=';

    var authorUrl= 'http://mysite.com/user.php?id=';
   
    function url( path , label ){
        return '<a href="' + path + '">' + label + '</a>';
    }

    var table = $('#table').dataTable( {
        "ajax": {
                "url":server ,
                "data": function ( d ) {
                     d.type = 'json';
                }
            },
        "columns": [

         
            { "data": "postId" },
            { "data": "title" },

            { "data": "description" },
            { "data": "authorId" }        
          ],

        "columnDefs": [
            {
                "render": function ( data, type, row ) {
                    return url( postUrl + data, data);
                },
                "targets": 0
//column postId
            },
            {
                "render": function ( data, type, row ) {
                    return url( authorUrl+ data , data);
                },
                "targets": 3
//column authorId
            }
        ]

    } );

} );

No comments:

Post a Comment