Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery DataTable overflow and text-wrapping issues

I have the following DataTable (full-width css class sets width = 100%)

<table class="datatable full-width">     <thead>         <th>LOB</th>         <th>Creditor Line 1</th>         <th>Creditor Line 2</th>         <th>Address</th>         <th>City</th>         <th>State</th>         <th>Zip</th>         <th></th>     </thead>     <tbody>         ...     </tbody> </table> 

Javascript:

var profileTable =             $(".datatable").dataTable({                 "iDisplayLength": 25,                 "bDestroy": true,                 "bJQueryUI": true,                 "sPaginationType": "full_numbers",                 "bAutoWidth": false             }); 

Everything works fine until there is a record with a long text string...when a record appears with really long text, the data table overflows on the right of the page. (See Screenshot Below, the red line is where the page should end) http://i1109.photobucket.com/albums/h430/tbarbedo/overflow.jpg

Can someone tell me how to either wrap the text in the cells or prevent this overflow issue?

I've tried via 'table-layout: fixed'...this prevents the overflow but sets all of the columns to the same width.

Thanks

like image 699
thiag0 Avatar asked Nov 08 '11 22:11

thiag0


2 Answers

I settled for the limitation (to some people a benefit) of having my rows only one line of text high. The CSS to contain long strings then becomes:

.datatable td {   overflow: hidden; /* this is what fixes the expansion */   text-overflow: ellipsis; /* not supported in all browsers, but I accepted the tradeoff */   white-space: nowrap; } 

[edit to add:] After using my own code and initially failing, I recognized a second requirement that might help people. The table itself needs to have a fixed layout or the cells will just keep trying to expand to accomodate contents no matter what. If DataTables styles or your own styles don't already do so, you need to set it:

table.someTableClass {   table-layout: fixed } 

Now that text is truncated with ellipses, to actually "see" the text that is potentially hidden you can implement a tooltip plugin or a details button or something. But a quick and dirty solution is to use JavaScript to set each cell's title to be identical to its contents. I used jQuery, but you don't have to:

  $('.datatable tbody td').each(function(index){     $this = $(this);     var titleVal = $this.text();     if (typeof titleVal === "string" && titleVal !== '') {       $this.attr('title', titleVal);     }   }); 

DataTables also provides callbacks at the row and cell rendering levels, so you could provide logic to set the titles at that point instead of with a jQuery.each iterator. But if you have other listeners that modify cell text, you might just be better off hitting them with the jQuery.each at the end.

This entire truncation method will ALSO have a limitation you've indicated you're not a fan of: by default columns will have the same width. I identify columns that are going to be consistently wide or consistently narrow, and explicitly set a percentage-based width on them (you could do it in your markup or with sWidth). Any columns without an explicit width get even distribution of the remaining space.

That might seem like a lot of compromises, but the end result was worth it for me.

like image 57
Greg Pettit Avatar answered Sep 20 '22 12:09

Greg Pettit


The following CSS declaration works for me:

.td-limit {     max-width: 70px;     text-overflow: ellipsis;     white-space: nowrap;     overflow: hidden; } 
like image 31
Mukesh Salaria Avatar answered Sep 21 '22 12:09

Mukesh Salaria