开发者

DataTables jQuery plugin nowrap for Ajax table

开发者 https://www.devze.com 2022-12-20 11:52 出处:网络
Can someone give me an example of how to add a nowrap=\"nowrap\" to a column when allinformation is generated on the fly for an ajax table?

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 lon开发者_StackOverflow中文版g shot. Thanks in advance.


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


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'
});
0

精彩评论

暂无评论...
验证码 换一张
取 消