Column width in IE 11

Column width in IE 11

MontMont Posts: 4Questions: 1Answers: 0

(trying to report a bug but I can't post to that topic, if there is a better way for me to do this please let me know)

We recently upgraded from DataTables 1.10.5 to 1.10.9. In doing so we discovered a bug when using IE 11.

Column widths are not calculated correctly when the container of the datatable isn't visible at the time the data is loaded.

This works correctly in Chrome and used to work in IE before we upgraded.

columns.adjust() also doesn't work until after the show().

If you move the $('#MyContainer').show(); before the DataTable initialization columns have the correct width.

Answers

  • glendersonglenderson Posts: 231Questions: 11Answers: 29

    Have you tried using the adjust() method?

    In the documentation, there is a mention that columns widths might be incorrect if the table is hidden, like when it's in a tab or the data is updated which might affect the columns widths.

    var table = $('#MyTable').DataTable();
    table.columns.adjust().draw();
    

    I tried the above on your fiddle and it seemed to resolve the issue. This would be executed / placed after the table is initialized.

  • MontMont Posts: 4Questions: 1Answers: 0

    Thank you for the suggestion. I had tried using adjust(), and it only worked for me when I placed it after the call to show().

    You said it worked for you. I am wondering if I am missing something. I've updated the fiddle to include the call to adjust(). In IE 11 (wide window) it still isn't working for me.

    New version: https://jsfiddle.net/foraydev/onux4rwx/6/

    Also, my apologies to you or anyone else that tried yesterday's fiddle. Apparently the Saved version had errors.

    Thanks,
    -Mont

  • glendersonglenderson Posts: 231Questions: 11Answers: 29

    It should only work after the .show(). That's when all the columns are assigned widths in the DOM.

    $('#MyContainer').show();
        
    myTable.columns.adjust().draw();
    
  • MontMont Posts: 4Questions: 1Answers: 0

    However this changed some time after 1.10.5. Further, it works fine in Chrome. It is only IE and more recent versions of DataTables that requires either that the data be loaded after the show or that adjust() be called after the show.

    It isn't the end of the work but it is an inconvenience over the previous behavior.

  • MontMont Posts: 4Questions: 1Answers: 0

    OK, I found the bug. In 1.10.9 _fnCalculateColumnWidths() was modified.

    Specifically the use of _fnVisibleToColumnIndex() was added to get the correct index of the visible column, taking into account hidden columns. In this for loop there is an incorrect if statement.

    Incorrect, as it appear in 1.10.9

    for ( i=0 ; i<columnCount ; i++ ) {
                    var colIdx = _fnVisibleToColumnIndex( oSettings, i );
        
                    if ( colIdx ) {
                        columns[ colIdx ].sWidth = _fnStringToCss( headerCells.eq(i).width() );
                    }
                }
    

    The if returns false for the first column when the index of the first column is 0. It should be:

    if ( colIdx != null ) {
                        columns[ colIdx ].sWidth = _fnStringToCss( headerCells.eq(i).width() );
                    }
    

    colidx is null when not found, not zero. By adding the != null tables have the correct column widths in IE, even when the table isn't visible when the data is loaded.

    I created a pull request.

This discussion has been closed.