Adding prefix/suffix to cell
Adding prefix/suffix to cell
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?
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?
This discussion has been closed.
Replies
http://datatables.net/usage/columns#fnRender
example:
[code]
"fnRender": function ( oObj ) {
return oObj.aData[iDataColumn] +' °C';
}
[/code]
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?
[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