Adding prefix/suffix to cell

Adding prefix/suffix to cell

jeryslojeryslo Posts: 4Questions: 0Answers: 0
edited April 2011 in General
Hello!

I didn't find anything on this site about auto appending prefix/suffix to each cell. I am using server side processing, where I read data from mysql database. The problem is, that I have weather data (temperature, pressure, humidity) and I don't save the °C, or hPa, or % inside each cell in database. But when displaying it to users, I would like to have these suffix inside each cell. I have looked over examples and have not been able to find any example that shows this kind of usage. How could I then append this parameters to each cell?

Replies

  • RizziRizzi Posts: 12Questions: 0Answers: 0
    edited April 2011
    fnRender for cells is your friend :D


    http://datatables.net/usage/columns#fnRender

    example:

    [code]
    "fnRender": function ( oObj ) {
    return oObj.aData[iDataColumn] +' °C';
    }
    [/code]
  • jeryslojeryslo Posts: 4Questions: 0Answers: 0
    Thanks for your help!

    I did it like this:
    [code]
    $(document).ready(function() {
    $('#example').dataTable( {
    "aoColumns": [
    null,
    { "fnRender": function ( oObj ) {
    return oObj.aData[1] +' '+ "°C";
    } },
    { "fnRender": function ( oObj ) {
    return oObj.aData[2] +' '+ "hPa";
    } }
    ],
    "bProcessing": true,
    "bServerSide": true,
    "bInfo": true,
    "sAjaxSource": "./server_processing.php"
    } );
    } );
    [/code]

    It works, but is it really necesarry to define fnrender for each column?
  • allanallan Posts: 63,519Questions: 1Answers: 10,473 Site admin
    edited April 2011
    It is if they are different for each of the columns. Although having said that it is possible to combine them using aoColumnDefs to be something like this:

    [code]
    $(document).ready(function() {
    $('#example').dataTable( {
    "aoColumnDefs": [
    {
    "fnRender": function ( oObj ) {
    return oObj.aData[oObj.iDataColumn] +' '+ (oObj.iDataColumn==1)?"°C" : "hPa";
    },
    "aTargets": [1,2]
    }
    ],
    "bProcessing": true,
    "bServerSide": true,
    "bInfo": true,
    "sAjaxSource": "./server_processing.php"
    } );
    } );
    [/code]
    which one you pick is up to you :-). Sometimes one can be useful, sometimes the other is better.

    Allan
  • jeryslojeryslo Posts: 4Questions: 0Answers: 0
    Thanks for your help!
This discussion has been closed.