Is it possible to set a cell Class and colspan in aaData?

Is it possible to set a cell Class and colspan in aaData?

AkhorAkhor Posts: 6Questions: 0Answers: 0
edited December 2012 in DataTables 1.9
Hi,

I have a table that's being generated via the an aaData array. Based on the data, I would sometimes like to change the cells css and sometimes add a column span.

What is the best way auto generate a table row like below

[code]DataSPECIALData2
Data ColspanData2
DataSPECIALData2[/code]


I may be wrong, but aaData seems to only take the "column values" as input.
[code]
"aaData": [
/* Reduced data set */
[ "Trident", "Internet Explorer 4.0", "Win 95+" ],
[ "Trident", "Internet Explorer 5.0", "Win 95+" ]
[/code]

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    No it isn't. Also colspan / rowspan is not supported in the TBODY in DataTables.

    You would need to use fnCreatedCell or some other callback to modify the css based on the data.

    Allan
  • AkhorAkhor Posts: 6Questions: 0Answers: 0
    Thanks for the information. I ended up getting it to work by

    1) using fnCreatedCell to assign the appropriate CSS
    2) usign fnCreatedRow to manipluate the row DOM to hide a the columns.



    Below test code will randomly assign a background color to any column with the value "R".

    [code]
    "fnCreatedCell": function(nTD,sData,oData,iRow,iCol){
    if (sData == "R"){
    $(nTD).css('background-color', 'red');
    if (Math.floor( (Math.random()*10+1)) == 2 ){
    $(nTD).attr('colSpan',2);
    }
    }
    }
    [/code]



    Below test code will hide 1 column if the colspan is 2 or more.

    [code]
    "fnCreatedRow": function( nRow, aData, iDataIndex){


    // Check each cell for a column span attribute
    // If there is one that is greater then 1 then
    // hide the following columns so that the table
    // does not expand.
    $(nRow).find('[colSpan]').each( function(index,element){

    for( i = 1; parseInt( $(element).attr('colSpan') ) > i ; i++ ){

    //Hide the next elements that are part of the
    //column span
    var nextIndex = $(element).index() + i;
    $(nRow).find("td:eq("+ nextIndex +")").hide();

    }
    });
    }
    [/code]
This discussion has been closed.