Type detection causing JS error

Type detection causing JS error

bluefoxregbluefoxreg Posts: 12Questions: 0Answers: 0
edited May 2011 in DataTables 1.8
It seems that the type detection in 1.8 doesn't properly recognize boolean data type. If the data is false, then it'll fail on line Char = sData.charAt(0); of
[code]
/*
* Function: -
* Purpose: Check to see if a string is numeric
* Returns: string:'numeric' or null
* Inputs: mixed:sText - string to check
*/
function ( sData )
[/code]

Replies

  • bluefoxregbluefoxreg Posts: 12Questions: 0Answers: 0
    edited May 2011
    also
    [code]
    /*
    * Function: _fnGetMaxLenString
    * Purpose: Get the maximum strlen for each data column
    * Returns: string: - max strlens for each column
    * Inputs: object:oSettings - dataTables settings object
    * int:iCol - column of interest
    */
    function _fnGetMaxLenString( oSettings, iCol )
    [/code]
    Have problem dealing with none-string data.
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    edited May 2011
    Brilliant - thanks for the heads up. It suffers with null as well at the moment ( http://datatables.net/forums/comments.php?DiscussionID=4874 ). Will be fixed in the next beta (hopefully quite soon in the nightly builds!).

    Regards,
    Allan
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    I've just committed in a fix for this. You can get it in the nightly here: http://datatables.net/download/ and it will be in the next beta, which I hope to release fairly soon :-). Give me a shout if you have any problems with that.

    Allan
  • bluefoxregbluefoxreg Posts: 12Questions: 0Answers: 0
    Thanks for the quick fix! I have a few more problems that I'm facing right now:
    1. I have HTML elements that I would like to display using fnRender, but the column widths for those elements are way too big and I can't find a way to control the size to minimum. For example a checkbox would take up a huge chuck of space.

    2. As I mentioned in my previous question, I'm using checkboxes, how would one maintain the checkbox state with bDeferRender set to true?

    Regards,
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    1. You can control the column width with the sWidth parameter: http://datatables.net/usage/columns#sWidth

    2. You would need to have an external array which has a list of the state of all checkboxes, and use fn fnDrawCallback to then 'correct' the checkbox values based on that state array when each row is displayed.

    Allan
  • bluefoxregbluefoxreg Posts: 12Questions: 0Answers: 0
    1. Yeah I've tried the sWidth setting but its as if it doesn't do anything. I did a quick debug and it appears that no matter what sWdith I set for my columns, the _fnConvertToWidth call in _fnCalculateColumnWidths always returns 750, which is my table warpper width.

    2. I see, I was wondering if it is possible to store the checked info in the row data and the grid would simply reflect the data change?
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    1. Can you link me to your example for the width issue please?

    2. I'm afraid not. That's a level of abstraction above the DataTables layer.

    Allan
  • bluefoxregbluefoxreg Posts: 12Questions: 0Answers: 0
    edited May 2011
    1. Take this page for example http://jsbin.com/ixoyo4 even though I specified sWidth: 10, it has no effect, the size of the width is not set to 10px.

    2. That's unfortunate, also I found another problem when I try to update row data using fnUpdate, if try to update any row that's not rendered yet, an exception is thrown on, even when I set bReraw and bAction to false.
    oSettings.aoData[iRow].nTr.getElementsByTagName('td')[iVisibleColumn].innerHTML =
    sDisplay;
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    Thanks very much for the jsbin example - that was instrumental in finding the issue :-). Basically what was happening was a combination between bDeferRender and the HTML in your fnRender function - the HTML was being treated as a string, rather than HTML, so DataTables was calculating the width of the unrendered string, not the checkbox. I've just committed a fix for this and it is available in the nightly (hence your example is working as it should now :-) ).

    I've also just updated fnUpdate to be able to cope with bDerferRender - thanks for the bug report!

    Regards,
    Allan
  • bluefoxregbluefoxreg Posts: 12Questions: 0Answers: 0
    Thanks for the update, though I'm still not quite sure how sWidth works, seems like its just merely a suggestion and the grid will still calculate the width based on the actual data, this is sometimes annoying when you know a specific field to be a specific width always.
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    It basically is a suggestion - the way that DataTables does the width calculation is that it will construct a temporary table with the longest strings and the widths applied - it will then render than and take the widths that the browser has calculated and apply them to the main table. If the browser thinks that the width given needs to be modified for whatever reason, then that is the way it goes. To work around that it is possible to set td { overflow: hidden }. Equally you could wrap your content in a div with the exact sizes you want.

    Allan
  • bluefoxregbluefoxreg Posts: 12Questions: 0Answers: 0
    I see, thanks for the tips.
    I do notice that there's another problem, when I have a field that's not generating a string as result
    (NOTE: fnRender that returns a html string still counts as a string)
    _fnGetMaxLenString will return -1 and causing the _fnGetWidestNode to return null. This causes mismatching td columns to be generated when trying to determine the column width.

    Take this for example http://jsbin.com/ebodu3/
    If you put a break point on

    /* Build the table and 'display' it */
    var nWrapper = oSettings.nTable.parentNode;

    And inspect the nTr variable, you'll notice that there are 5 th in the temp table, but only 4 td in the actual table. I temporary fix my local file by replacing
    if ( typeof s == 'string' && s.length > iMax )
    with
    if (s && s.toString().length > iMax)
    in _fnGetMaxLenString, however, I'm not sure if this is the right way to properly determine the widest node.
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    I think I encountered this very problem this morning - I've committed a fix for it: https://github.com/DataTables/DataTables/commit/b3f5ddaf96a8bda5b10590d9481225d45960ba36 . I've just updated the nightly and hopefully that will do the job :-)

    Allan
  • bluefoxregbluefoxreg Posts: 12Questions: 0Answers: 0
    Unfortunately this patch doesn't fix my particular scenario since in _fnGetCellData, the sData is not null (its a number) so when checking the string length, the result is the same.
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    I see yes - sorry about that. I'm in the middle of making some other changes at the moment, so can't commit a change, but I think the thing to do here would be: var s = _fnGetCellData( oSettings, i, iCol, 'display' )+""; in the function _fnGetMaxLenString. It makes the typeof 'string' redundant, but that should do the job.

    Allan
  • bluefoxregbluefoxreg Posts: 12Questions: 0Answers: 0
    Yup that does the job, thx!
  • bluefoxregbluefoxreg Posts: 12Questions: 0Answers: 0
    I also got a workaround for handling checkboxs with the latest datatable 1.8 without creating an external array and would like to get your input on it.
    http://jsbin.com/ohane3

    Basically I make sure bUseRendered: false is set on the checkbox column (maybe bUseRendered should be default to false for any bSortable: false?) So that the actual data doesn't get changed to the checkbox string. Then I attach handlers to synchronize data with the checkboxes state. Finally to read the data, I just loop through all data and retrieve the checked items. Please let me know if there's something else I could do to minimize the hacking and/or improve performance.

    Regards,
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    The code looks fine to me on a quick scan - good use of bUseRendered :-). It is useful having that option available. I won't change it's default based on other parameters (it can be useful for filtering as well as sorting), that's something which should be explicitly noted by the developer I'd say - but it is a good parameter to know about.

    Regards,
    Allan
  • bluefoxregbluefoxreg Posts: 12Questions: 0Answers: 0
    Thx for the feedback, so I guess there's nothing I should be aware of when saving/using the data directly from the datatable internal data array?
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    Not in the way you've done it. If you were to modify the settings in DataTables settings object without it knowing (for example changing the length of aiDisplayMaster or the contents of aoData) then it would get upset. But using the API as you have, it should all be fine.

    Allan
This discussion has been closed.