Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Dev: Trivial tidy up of _fnConvertToWidth

- Just 43 bytes saved, but tidier code and smaller size...
  • Loading branch information...
commit a67ff2fadb82c11348ea5baa8befbd62801f05f6 1 parent 7c53a18
@DataTables authored
Showing with 18 additions and 20 deletions.
  1. +9 −10 media/js/jquery.dataTables.js
  2. +9 −10 media/src/core/core.sizing.js
View
19 media/js/jquery.dataTables.js
@@ -3563,25 +3563,24 @@
*/
function _fnConvertToWidth ( sWidth, nParent )
{
- if ( !sWidth || sWidth === null || sWidth === '' )
+ if ( ! sWidth )
{
return 0;
}
- if ( !nParent )
+ if ( ! nParent )
{
nParent = document.body;
}
- var iWidth;
- var nTmp = document.createElement( "div" );
- nTmp.style.width = _fnStringToCss( sWidth );
-
- nParent.appendChild( nTmp );
- iWidth = nTmp.offsetWidth;
- nParent.removeChild( nTmp );
+ var n = $('<div/>')
+ .css( 'width', _fnStringToCss( sWidth ) )
+ .appendTo( nParent );
+
+ var width = n.offsetWidth;
+ n.remove();
- return ( iWidth );
+ return width;
}
View
19 media/src/core/core.sizing.js
@@ -7,25 +7,24 @@
*/
function _fnConvertToWidth ( sWidth, nParent )
{
- if ( !sWidth || sWidth === null || sWidth === '' )
+ if ( ! sWidth )
{
return 0;
}
- if ( !nParent )
+ if ( ! nParent )
{
nParent = document.body;
}
- var iWidth;
- var nTmp = document.createElement( "div" );
- nTmp.style.width = _fnStringToCss( sWidth );
-
- nParent.appendChild( nTmp );
- iWidth = nTmp.offsetWidth;
- nParent.removeChild( nTmp );
+ var n = $('<div/>')
+ .css( 'width', _fnStringToCss( sWidth ) )
+ .appendTo( nParent );
+
+ var width = n.offsetWidth;

This line breaks functionality sWidth. Should be ' n[0].offsetWidth '. Current code returns 'undefined', which then sends 'undefined' to _fnStringToCss( s ).

StackTrace:

Error: Uncaught TypeError: Cannot read property 'length' of undefined.
_fnStringToCss jquery.dataTables.js:3912
_fnCalculateColumnWidths jquery.dataTables.js:3617
_fnInitialise jquery.dataTables.js:2676
_that jquery.dataTables.js:6690

@DataTables Owner

Sure. Simplest case would be setting some custom parameters for aoColumns.

oTable = _dataTableId.dataTable({
"aaSorting": [],
"aoColumns": [null, null, null, null, null, {'sWidth': '100px','bSearchable': false,'bSortable': false}]
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ n.remove();
- return ( iWidth );
+ return width;
}

2 comments on commit a67ff2f

@hummerpj

After using the above fix there seems to be another possibly unrelated bug. I haven't had enough time to fully debug it but here is the stack trace after using the change commented above. Even tried reverting this function to 1.9.4 original and the below bug is still occurring so this leads me to believe it is related to a different change.

Hope this helps out. Let me know if there is anything else I can provide

Uncaught TypeError: Cannot call method 'replace' of undefined jquery.dataTables-1.9.4_RC1.js:2514
_fnDataToSearch - line 2514
_fnBuildSearchRow - line 2449
_fnBuildSearchArray - line 2425
_fnFilterComplete - line 2250
_fnSort- line 4199
_fnInitialise - line 2696
_that - line 6692

@DataTables
Owner

Doh - that was stupid of me. Fixed in 1bb4fc6. Thanks for spotting this and letting me know!

Regarding the other issue, are you able to open a new issue with a link to a test case please?

Please sign in to comment.
Something went wrong with that request. Please try again.