Server side processing: Put data from different database columns into single dataTables column

Server side processing: Put data from different database columns into single dataTables column

cortezcortez Posts: 5Questions: 0Answers: 0
edited June 2013 in General
Hi,

I am doing server-side processing and I need to display the returned data so that the data from certain columns is compressed into a single column. So, if my database has columns [ 'name', 'comment', 'timestamp', 'foo', 'bar', 'baz' ] I want to display the name, comment and timestamp values all within the first cell of a row. I've managed to achieve this by using a combination of hidden columns and pre-rendering (using fnRowCallback) - the 'comment' and 'timestamp' columns are hidden, but the data for those columns is rendered under the 'name' column (see the example here: http://live.datatables.net/ataqod/2/edit#source). My question is basically only whether this is the right (or best) way to achieve this.

An obvious solution would be to stuff the relevant values into a single database column, but for various reasons I need to restrict the database entries to just strings. My server-side script simply returns rows, and doesn't do any more complex pre-processing.

Many thanks for any help/suggestions,
Chris

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    I'd suggest you use mData for this rather then fnRowCallback .

    This blog post talks about mData with objects, but it can also be used to reorder arrays - just use a different index: http://datatables.net/blog/Extended_data_source_options_with_DataTables .

    Allan
  • cortezcortez Posts: 5Questions: 0Answers: 0
    Thanks very much Allan. I need to use fnRowCallback for rendering anyway, since the three elements in the cell (name, comment, timestamp) need to live in their own DOM objects. That would assume that the data for the cell in question was actually available as an object, whose internal structure I could access. So within the fnRowCallback aData would give me access to an object rather than simply a single value. Is there any way to do this? Or am I missing something about mData that would allow me to do this?

    Best,
    Christopher
  • cortezcortez Posts: 5Questions: 0Answers: 0
    Actually I solved it by simply using objects in my returned JSON, accessing only some of them through mData, and then using the object property name to access the extra fields I need within the fnRowCallback. Basically something like this:

    [code]
    $(document).ready(function() {
    $('#test').dataTable( {
    "aaData": [
    {
    comment: "Lorem ipsum.",
    number: 0,
    author: "Joe Blogs",
    datestamp: "2012-10-17T02:01:32+0000",
    foo: 0.0,
    bar: 0.9,
    baz: 0.4,
    quux: -2.3
    },
    {
    comment: "Aliquam erat volutpat.",
    number: 1,
    author: "Tim Smith",
    datestamp: "2012-10-17T07:40:16+0000",
    foo: 0.2,
    bar: 0.7,
    baz: 0.6,
    quux: -1.5
    }
    ] ,
    "aoColumns": [
    { "mData": "number" },
    { "mData": "comment" },
    { "mData": "foo" },
    { "mData": "bar" },
    { "mData": "baz" },
    { "mData": "quux" }
    ],
    "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
    $(nRow).children().eq(1).html(
    ''+aData.comment+''+aData.author+''+' '+aData.datestamp+'');
    }
    } );
    } );
    [/code]
This discussion has been closed.