Adding Column (Client Side) to Server Side DataTable

Adding Column (Client Side) to Server Side DataTable

jcrawfordjcrawford Posts: 172Questions: 0Answers: 0
edited November 2011 in General
I have a table which reads database columns from a database and just displays it as is column for column. However on the display side I need to inject a (calculated column) at column index 3. I can do this by adding it to the html header and adding the data to the server side output that feeds the table. However I am having problems with the server side sorting as now the server side code does not know about this column.

Is there a way I can tell Datatables either to not allow sorting of that "fake" column. I am probably not doing something the proper way here lol

here is my js

[code]
var oTable = $('#fbcreditlog').dataTable( {
"bProcessing": true,
"bFilter": false,
"bServerSide": true,
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"sAjaxSource": "/admin/pstoolfbcreditajax/?start_date="+default_start_date+"&end_date="+default_end_date,
"aaSorting": [[ 1, "desc" ]],
"oLanguage": {
"sZeroRecords": "No transactions found."
},
"sDom": '<"H"Tfr>t<"F"ip>',
"oTableTools": {
"sSwfPath": "/admin-assets/TableTools.swf",
"aButtons": [
"copy", "print",
{
"sExtends": "collection",
"sButtonText": "Save",
"aButtons": [ "csv", "xls", "pdf" ]
}
]
},
"fnServerData": function( sSource, aoData, fnCallback) {
$.getJSON(sSource, aoData, function(json) {
if(jQuery.isEmptyObject(json)) location.href = '/admin/login/';
else fnCallback(json);
});
},
} );
[/code]

here is my html

[code]




Order ID
Buyer Facebook ID
Credits
Dollar Amount
Description
Bundle ID
Status
Transaction Date




Loading data from server




[/code]

and here is the output from my server side script

[code]
{"sEcho":1,"iTotalRecords":"2","iTotalDisplayRecords":"2","aaData":[["23453453323","100002497224420","30","3","3000 Coins for 30 Facebook Credi","1","placed","2011-11-21 13:41:58"],["56745467456","100002497224420","20","2","2000 Coins for 20 Facebook Credi","1","placed","2011-11-18 13:41:58"]]}
[/code]

Replies

  • jcrawfordjcrawford Posts: 172Questions: 0Answers: 0
    I should note that I know what the issue is, it's happening because the table is showing the extra column so it sends the index of the sorting column to the server which is not aware of this column.

    Just wondering if there is a way to make the datatable ignore the column when doing the index counts or how I could work around this with a sorting callback or something of that nature.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    When I've needed to do this, I just repeat a valid db column (i.e. my ID column) and use fnRender on the client side to display whatever I need. This way there are no complications on the server side.

    You can set bSortable: false in aoColumns/aoColumnDefs for that column (and bSearchable: false if you want)
  • jcrawfordjcrawford Posts: 172Questions: 0Answers: 0
    fbas, is there a way to do the sorting in a callback? If that's possible it would be great. It would be nice to have the sorting on the column.

    Thanks for the tip on the server side bit.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    What's the calculated data in the column? Is it derived from the data already in the database? You can perform various functions/operations on database queries and return it in a column, so you could perform searching and sorting for that column on the server side.
  • jcrawfordjcrawford Posts: 172Questions: 0Answers: 0
    it just so happens that the sorting works anyway. Its because the calculation is the prev column / 10 and I just used the value of the previous column in my server side output :)
This discussion has been closed.