// Affiliate Name, Sales, Commission, % Commission, Net, Date of Last Sale
[ "Dan", 1000000, 400000, .4, 600000 ],
[ "Jim", 20000, 5000, .25, 15000 ],
[ "Smith", 80000, 40000, .5, 40000 ],
[ "Bart", 100000, 99000, .99, 1000 ]
[ "Dan", 1000000, 400000 ],
[ "Jim", 20000, 5000 ],
[ "Smith", 80000, 40000 ],
[ "Bart", 100000, 99000 ]
function fnRowDataCallback(iRow, aDataOriginal, aDataRow)
{
aDataRow[0] = aDataOriginal[0]; // Affiliate Name
aDataRow[1] = aDataOriginal[1]; // Sales
aDataRow[2] = aDataOriginal[2]; // Commission
aDataRow[3] = aDataOriginal[2] / aDataOriginal[1]; // % Commission
aDataRow[4] = aDataOriginal[1] - aDataOriginal[2]; // Net
}
function fnRowDataCallback(iRow, aDataOriginal, aDataRow)
{
aDataRow[0] = aDataOriginal[0]; // Affiliate Name
aDataRow[1] = aDataOriginal[1]; // Sales
aDataRow[2] = aDataOriginal[2]; // Commission
aDataRow[3] = aDataOriginal[1] - aDataOriginal[2]; // Net
aDataRow[4] = aDataRow[3] / aDataOriginal[1]; // % Profit
}
function fnRowDataCallback(iRow, aDataOriginal, aDataRow)
{
aDataRow[0] = aDataOriginal[0]; // Affiliate Name
aDataRow[1] = aDataOriginal[1]; // Sales
aDataRow[1].display = "$" + FormatAmount(aDataOriginal[1]); // Display a "$" in front of the sales and add 1000 separators
aDataRow[1].filter = aDataRow[1] + " " + aDataRow[1].display; // Filter using either the raw value or the formatted value
}
"aaData": [ // Affiliate Name, Sales, Commission [ "Dan", 1000000, 400000 ], [ "Jim", 20000, 5000 ], [ "Smith", 80000, 40000 ], [ "Bart", 100000, 99000 ] ]
"fnServerData": function (sSource, aoData, fnCallback ) {
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": function (json) {
for ( var i=0, iLen=json.aaData.length ; i<iLen ; i++ ) {
var row = json.aaData[i];
fnRowDataCallback(i, row);
/*
// Calculation of commission %
row.push( ((100/row[1]) * row[2])+"%" );
// Calculation of Net
row.push( "$"+(row[1] - row[2]) );
// Formatting of sales and commission
row[1] = "$"+row[1];
row[2] = "$"+row[2];
*/
}
fnCallback( json );
}
} );
}
so I do not have to repeat the core logic of fnServerData for each table
"fnServerData": myFunction
"fnServerData": myFunction( function (json) {
// process json
} );
Overall, the code is working, except when I set aaData[] for testing purpose.
This is very cool for real-time testing :)
For this would would just call a function to process the data before passing it to DataTables as the aaData property :-)True, however it is nice to have an abstraction between the testing data and live data. This is what I really like about aaData[]. It allows me to test EVERYTHING first, and then the guy writing the code on the server just need to mimic the aaData[]. If there are two functions, then there is a risk of errors and bugs, and those bugs take time to find and are costly. In other words, I would like to ensure that if my code works fine with aaData[], then when when the live data arrives, I am confident it will work. In my 20+ years of experience as a developer, if two functions do the same work, they should be consolidated. For instance, in my sort routine, there is only one callback for comparing elements. If an array need to be sorted in descending order, then the sort routine will sort using the ascending order, then reverse the array before returning.
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.