Solution for Ellipsis - Jquery + Bootstrap + Datat

2019-07-31 07:09发布

问题:

I am using Datatables on a site with bootstrap and jquery for a large (2000+ entries) table. Some of the (text) values are way too long and at the moment I use something like this:

render: function ( data, type, row ) {
                        return data.length > 35 ?
                        data.substr( 0, 35 ) +'…' :
                            data;
                    }

in Datatables to cut the strings and display the full value in the html title. Is there a better way to do this? Like a plugin that automatically cuts but shows all when clicked on or something similar? I was not able to find something.

I saw jquery dotdotdot but it doesnt give me a click to extend.

回答1:

Modifying strings in javascript in order to make them more viewable or "fitted" to certain DOM elements is a total no-go. Especially when you as here have many strings that is being preprocessed in a callback.

If you force fixed widths to some (or all) columns, example :

table.dataTable td:nth-child(3) {
  max-width: 100px;
}

Then you can use simple CSS to ensure that the content of the columns not is wrapped, not go beyond margins and displays a nice ellipsis if it is too large by

table.dataTable td  {
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

demo -> http://jsfiddle.net/ax1gr1og/