Question

Can someone give me an example of how to add a nowrap="nowrap" to a column when all information is generated on the fly for an ajax table?

$('#results').dataTable({
    "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
        $(nRow).attr('id', aData[0]);
        return nRow;
    },
    "bAutoWidth": false,
    "sPaginationType": "full_numbers",  
    "bProcessing": true,
    "sAjaxSource": 'ajax/purchasers.php',
    "aaSorting": [[1,'asc']],                   
    "aoColumns": [                              
        { "bVisible": false },                      
        null,                                   
        null,
        null,
        null,
        null,
        null,
        null
    ]
});

I know this might be a long shot. Thanks in advance.

Was it helpful?

Solution 3

In case anyone is interested in the solution, you can use fnInitComplete to loop over the table after DataTables is done rendering it like so:

$('#results').dataTable({
    "fnInitComplete": function() {
        $('#results tbody tr').each(function(){
                $(this).find('td:eq(0)').attr('nowrap', 'nowrap');
        });
    },
    "sAjaxSource": 'ajax/purchasers.php'
});

OTHER TIPS

It's better to achieve this through styling instead.

"aoColumns": [                              
    { "sClass": "my_class"},

in the stylesheet

    .my_class {
   white-space:nowrap;
 }

While it certainly will work to add a class and create a CSS entry for this it seems like using a hammer to pound in a screw.

Datatables already provides an easy way of doing this.

In your dataTable declaration add:

"fnRowCallback": function( nRow ) {
    if(nRow.cells[2]) nRow.cells[2].noWrap = true;  // column index starts with 0 and we check if cells[2] is null to be ultra safe
    return nRow;
},

Hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top