Showing image in jquery data table column when JSON is the DataSource

datatablesjquery-plugins

I am using JQuery Data Table plugin to show a list of students in a class. The data source of the plug in is set as a server side action, which will return a json object which contains these properties (FirstName, LastName, Age, Sex,….). Our requirement has recently changed to show an image (male /female) based on the student sex.

I can either load all data, format the table way I want and then convert it to DataTable, but this is not possible because we have lot of records and we are using pagination.

Is there any functions available in data table plugin that will do post-rendering ?

Best Answer

Use mRender option

Code from the datatables docs

$(document).ready(function() {
  $('#example').dataTable( {
    "aoColumnDefs": [
        {
            // `data` refers to the data for the cell (defined by `mData`, which
            // defaults to the column being worked with, in this case is the first
            // Using `row[0]` is equivalent.
            "mRender": function ( data, type, row ) {
                return data +' '+ row[3];
            },
            "aTargets": [ 0 ]
        },
        { "bVisible": false,  "aTargets": [ 3 ] },
        { "sClass": "center", "aTargets": [ 4 ] }
     ]
    } );
} );

Link to working example here

EDIT: Another way to achieve this is to use "fnRowCallback" as pointed out by Daniel. Use this link :)

Cheers!!

Related Topic