$(document).ready(function() {
var oTable = $('#example').dataTable( {
"aoColumnDefs": [
{
"aTargets": [ 0 ],
"mDataProp": function ( source, type, val ) {
if (type === 'set') {
source[0] = val;
// Store the computed display for speed
source.date_rendered = renderDate( val );
return;
}
else if (type === 'display' || type === 'filter') {
return source.date_rendered;
}
// 'sort' and 'type' both just use the raw data
return source[0];
}
]
} );
} );
function render( column, renderType ) {
return function ( source, type, val ) {
if (type === 'set') {
source[column] = val;
// Store the computed display for speed
if ( renderType === "date" ) {
source[column+'_rendered'] = renderDate( val );
}
else if ( renderType === "percentage" ) {
source[column+'_rendered'] = renderPercent( val );
}
else {
source[column+'_rendered'] = val;
}
return;
}
else if (type === 'display' || type === 'filter') {
return source[column+'_rendered'];
}
// 'sort' and 'type' both just use the raw data
return source[column];
};
};
$(document).ready(function() {
var oTable = $('#example').dataTable( {
"aoColumns": [
{ "mDataProp": render( 0, 'date' ) },
{ "mDataProp": render( 1, 'percentage' ) }
null // regular column
]
} );
} );
function render( column, renderType ) {
return function ( source, type, val ) {
if (type === 'set') {
source[column] = val;
// Store the computed display for speed
if ( renderType === "date" ) {
source[column+'_rendered'] = FormatDateTimeAgo( val );
}
else if ( renderType === "percentage" ) {
source[column+'_rendered'] = FormatPercentage( val );
}
else {
source[column+'_rendered'] = val;
}
return;
}
else if (type === 'display' || type === 'filter') {
return source[column+'_rendered'];
}
// 'sort' and 'type' both just use the raw data
return source[column];
};
};
$(document).ready(function()
{
var oTable = $('#test').dataTable(
{
"aoColumns":
[
null, // regular column
{ "mDataProp": render( 1, 'percentage' ) },
{ "mDataProp": render( 2, 'date' ) }
],
"aaData":
[
[ "Dan", .4, "2012-01-17T13:32:00" ],
[ "Jim", .25, "2012-01-16T20:32:43" ],
[ "Smith", .5, "2012-01-27T11:22:33" ],
[ "Bart", .99, "2012-01-22T02:03:00" ]
]
});
}); // ready()
"fnRender": function(oObj)
{
return '<a href="/customer/?id=' + oObj.aData[6] + '">' + oObj.aData[0] + '</a>';
}
Is there a way to render the data for the display cell, however preserve the original data for all calls to fnRender()?
function DataPropDate(source, type, val)
{
if (type === 'display' || type == 'filter')
return FormatDate(val);
return val;
}
function DataPropDate(source, iColumn, type, val)
{
if (type === 'set')
{
source[iColumn] = val;
return;
}
if (type === 'display' || type == 'filter')
return FormatDate(source[iColumn]);
return source[iColumn];
} I meant the original supplied value from aaData[]
If this is not possible, could val contains the index of the column, so this parameter is available inside the function
"aoColumns":
[
{ "sClass": "NoNumber" },
{ "mDataProp": function(rgData, eType) { return FormatTypePercent(eType, 1 - (rgData[2] / rgData[1])); } },
{ "mDataProp": DataPropSales },
{ "mDataProp": DataPropCommissionPercent },
{ "mDataProp": DataPropCommission },
{ "mDataProp": DataPropNet },
],
function DataPropSales(rgData, eType, val)
{
return FormatTypeAmount(eType, rgData[1]);
}
function FormatTypeAmount(eType, n)
{
if (eType === 'display')
return '$' + n;
if (eType == 'filter')
return n + ' $' + n;
return n;
}
<table id="test">
<thead>
<tr>
<th>Affiliate Name</th>
<th>Profits %</th>
<th>Sales</th>
<th>Commission %</th>
<th>Commission</th>
<th>Net</th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
<tr>
<th>Total</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</table>
<script>
function DataPropSales(rgData, eType, val)
{
return FormatTypeAmount(eType, rgData[1]);
}
function DataPropCommissionPercent(rgData, eType, val)
{
return FormatTypePercent(eType, rgData[2] / rgData[1]);
}
function DataPropProfitsPercent(rgData, eType, val)
{
return FormatTypePercent(eType, 1 - (rgData[2] / rgData[1]));
}
function DataPropCommission(rgData, eType, val)
{
return FormatTypeAmount(eType, rgData[2]);
}
function DataPropNet(rgData, eType, val)
{
return FormatTypeAmount(eType, rgData[1] - rgData[2]);
}
$(document).ready(function()
{
g_oTable = $('#test').dataTable(
{
"bJQueryUI": true,
"sScrollY": "150px",
"bPaginate": false,
"bScrollCollapse": true,
"fnFooterCallback": function(oRow, aaData, iStart, iEnd, aiDisplay)
{
var nTotalSales = 0;
var nTotalCommissions = 0;
for (var iRow = aiDisplay.length - 1; iRow >= 0; iRow--)
{
var rgData = aaData[aiDisplay[iRow]];
nTotalSales += rgData[1];
nTotalCommissions += rgData[2];
}
// Modify the footer row to display the sums and percentage
var nPercentCommission = nTotalCommissions / nTotalSales;
var rgCells = oRow.getElementsByTagName('th');
rgCells[1].innerHTML = FormatPercentage(1 - nPercentCommission);
rgCells[2].innerHTML = FormatAmount(nTotalSales);
rgCells[3].innerHTML = FormatPercentage(nPercentCommission);
rgCells[4].innerHTML = FormatAmount(nTotalCommissions);
rgCells[5].innerHTML = FormatAmount(nTotalSales - nTotalCommissions);
},
"aoColumns":
[
{ "sClass": "NoNumber" },
{ "mDataProp": function(rgData, eType) { return FormatTypePercent(eType, 1 - (rgData[2] / rgData[1])); } },
{ "mDataProp": DataPropSales },
{ "mDataProp": DataPropCommissionPercent },
{ "mDataProp": DataPropCommission },
{ "mDataProp": DataPropNet },
],
"aaData":
[
// Affiliate Name, Sales, Commission
[ "Dan", 1000000, 400000 ],
[ "Jim", 20000, 5000 ],
[ "Smith", 80000, 40000 ],
[ "Bart", 100000, 99000 ],
[ "Dan", 1000000, 400000 ],
[ "Jim", 20000, 5000 ],
[ "Smith", 80000, 40000 ],
[ "Mark", 500, 600 ]
]
});
}); // ready()
</script> It looks like you're new here. If you want to get involved, click one of these buttons!
Get useful and friendly help straight from the source.