Column width issue

Column width issue

sd_zuosd_zuo Posts: 78Questions: 1Answers: 0
edited October 2010 in Bug reports
I've just downloaded the 1.7.3 version, and the following column configuration brings out an exception.
[code]
var columns = [
{ sTitle: "A", sWidth: "40pt" }
]
[/code]
I used Visual Web Developer to track down the minimized code and see the problem is here
[code]
if(a.aoColumns[b].sWidth!==null)
a.aoColumns[b].nTh.style.width=u(a.aoColumns[b].sWidth);
[/code]
And the evaluation result of "u(a.aoColumns[b].sWidth)" is "40ptpx", which obviously is incorrect.

Please fix this problem by adding the indicated line into the source code.
[code]
if(a.indexOf("em")!=-1||a.indexOf("%")!=-1||a.indexOf("ex")!=-1||a.indexOf("px")!=-1
||a.indexOf("pt")!=-1 // add this line
)return a;
[/code]

Actually I don't think this function is necessary. Why not just check the data type of the width parameter? if number then return widthNumber+"px" otherwise return the width string?

Replies

  • allanallan Posts: 61,692Questions: 1Answers: 10,102 Site admin
    Sorry about that, I had clean forgotten to consider point units in that function. I've just committed the change now and it can be picked up from the 'nightly' version of DataTables here: http://datatables.net/download/ , it will be included in the next release.

    The reason for the function, is to try and take account of all possibilities - it's not unreasonable for sScrollX (for example) to be given as a string without a unit "sScrollX": "100" for example. DataTables is forgiving enough to add on the pixels, and I think this is probably the right thing to do, since most devs tend not to think about typing in Javascript to much (rightly or wrongly.... :-) ).

    Thanks for spotting this, letting me know and the fix!

    Regards,
    Allan
  • sd_zuosd_zuo Posts: 78Questions: 1Answers: 0
    edited October 2010
    Hello,

    If you are just trying to determine whether the width is a number in String type. Maybe checking the last character code would be more effective.
    [code]
    var c = a.charCodeAt (a.length-1);
    return (c < 0x30 || c > 0x39)
    ? a // the last character is not 0-9
    : a + "px";
    [/code]
  • allanallan Posts: 61,692Questions: 1Answers: 10,102 Site admin
    That sounds like quite a good idea - thanks for the suggestion. I've just committed this code to the tree, in that function:

    [code]
    /* Check if the last character is not 0-9 */
    var c = s.charCodeAt( s.length-1 );
    if (c < 0x30 || c > 0x39)
    {
    return s;
    }
    return s+"px";
    [/code]
    Regards,
    Allan
  • sd_zuosd_zuo Posts: 78Questions: 1Answers: 0
    Oops, my previously posted code was wrong and I'd just fixed it. Fortunately you understood it :)
This discussion has been closed.