Permalink
Browse files

Dev: Trivial tidy up of _fnConvertToWidth

- Just 43 bytes saved, but tidier code and smaller size...
  • Loading branch information...
DataTables committed Feb 18, 2013
1 parent 7c53a18 commit a67ff2fadb82c11348ea5baa8befbd62801f05f6
Showing with 18 additions and 20 deletions.
  1. +9 −10 media/js/jquery.dataTables.js
  2. +9 −10 media/src/core/core.sizing.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;
}
@@ -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 comment has been minimized.

Show comment Hide comment
@hummerpj

hummerpj Feb 22, 2013

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

@hummerpj

hummerpj Feb 22, 2013

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

This comment has been minimized.

Show comment Hide comment
@DataTables

DataTables Feb 22, 2013

Owner
@DataTables

DataTables via email Feb 22, 2013

Owner

This comment has been minimized.

Show comment Hide comment
@hummerpj

hummerpj Feb 22, 2013

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}]
});

@hummerpj

hummerpj Feb 22, 2013

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}]
});

+ n.remove();
- return ( iWidth );
+ return width;
}

2 comments on commit a67ff2f

@hummerpj

This comment has been minimized.

Show comment Hide comment
@hummerpj

hummerpj Feb 22, 2013

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

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

This comment has been minimized.

Show comment Hide comment
@DataTables

DataTables Feb 26, 2013

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?

Owner

DataTables replied Feb 26, 2013

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.