Open GoogleCodeExporter opened 8 years ago
Improved the way to automatically handle and set the width of the input
filtering boxes. Just put the following code under your datatables, after the
try and catch(), and the input boxes will have their width adjusted to the
width of its tr th.
This code has many advantages. The function is global, and will render the
columns width again after the Datatables has been loaded. It will allow to work
under deep divs and iframes where its hard to detect and get the right column
width for each column and prevent a bad alignement of header and body column
width.
//oTable is the datatables object: oTable = $('#example').dataTable(
$(function()
{
$(window).bind('load', function()
{
if (typeof oTable != 'undefined')
{
if ((oTable != null) && (oTable != ""))
{
oTable.fnAdjustColumnSizing();
}
var oTabletrth = $('#example_wrapper.dataTables_wrapper div.dataTables_scroll div.dataTables_scrollHead div.dataTables_scrollHeadInner table.display thead tr th');
if ( (typeof(oTabletrth) != 'undefined') && (oTabletrth != null) )
{
oTabletrth.each(function(){
var otrth = $(this);
var oInput = $(this).find("input");
if ( (typeof(oInput) != 'undefined') && (oInput != null) )
{
var swidth = otrth.attr('width');
if ((swidth == null) || (swidth == 0) || (swidth == ""))
{
swidth = otrth.width();
}
var sinputwidth = swidth - 20;
sinputwidth = sinputwidth + 'px';
oInput.css({'width':sinputwidth});
}
});
}
}
});
});
Original comment by blacksha...@gmail.com
on 19 Jul 2013 at 8:29
In order for this to work for me, I had to make a few changes. First, putting
the code into the fnInitComplete attribute of the dataTable call, so that the
widths would take into account the loaded data (versus the column titles). I
also added a check to see if the column filter type was a date range - I want
those to stay small (I've got the .date_range_filter class set with a 3em width
in css)
"fnInitComplete": function(oSettings, json) {
var oTabletrth = $('#mytablename_wrapper.dataTables_wrapper div.dataTables_scroll div.dataTables_scrollHead div.dataTables_scrollHeadInner table.display thead tr th');
if ( (typeof(oTabletrth) != 'undefined') && (oTabletrth != null) )
{
oTabletrth.each(function(){
var otrth = $(this);
var oInput = $(this).find("input");
if ( (typeof(oInput) != 'undefined') && (oInput != null) )
{
if (oInput.attr('className') == 'date_range_filter hasDatepicker') {
console.log("has date - not setting width");
} else {
var swidth = otrth.attr('width');
etc etc
Original comment by t...@gmara.org
on 30 Apr 2014 at 7:05
Original issue reported on code.google.com by
blacksha...@gmail.com
on 21 May 2013 at 2:57Attachments: