types detection on columns with nulls

types detection on columns with nulls

lukesslukess Posts: 3Questions: 0Answers: 0
edited December 2009 in General
I was having trouble with getting type detection to work on columns that have nulls in them. Would I be correct in assuming that the first row values are what are being passed to the regex functions? It seems to see a null and classify the column as a string as opposed to the date type that I set up. If so would there be a good way of grabbing the first row with an actual value to evaluate?

Trying to avoid using column type definitions, but may need to resort to that.

[code]
jQuery.fn.dataTableExt.aTypes.push(
function ( sData )
{
if (sData.match(/^(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])\-(19|20|21)\d\d$/))
{
return 'us_date';
}
return null;
}
);
[/code]

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi lukess,

    DataTables doesn't really "like" null data, for the simple fact that null isn't any data at all :-). What is null when you show it in a table? An empty cell possibly, in which case it should be an empty String, or the cell shouldn't exist at all, which would probably break the table altogether.

    I don't really think this is a bug in DataTables as such, since, as I say, null is not data, and shouldn't really be in a table. I presume that you are using Ajax data or some other form of dynamic injection? One option might be to replace null with an empty string internally in DataTables when the data is added, but then it's no longer null and I've modified your data, which I'm most reluctant to do... So currently (I'm open to persuasion though!) I think that this should be done externally.

    Regards,
    Allan
  • lukesslukess Posts: 3Questions: 0Answers: 0
    You were correct in assuming that I was building my tables dynamically. I am using asp.net to build the tables. What I didn't realize was that asp.net automatically convert the null to a nbsp (which makes sense due to table not liking empty 's). So the issue still remains that the nbsp is being looked at for the type detection. I resorted to defining the column type by hand, but it would be nice if there was a switch of some kind that would move to a cell that is not "empty" to determine the type.

    In my case, I am trying to define date columns that may or may not have a value depending on whether the event has occurred or not.

    Anyway, regardless of whether you look into this or not, awesome stuff. It's a great tool for the kind of reports I am generating. Thanks.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi lukess,

    Ah okay - I see what the issue is then. The reason DataTables isn't automatically detecting the non-breaking space as a date is because it isn't :-). Therefore, that character overrules the other information in the column and forces the column to be of type 'string'. So there are a couple of ways around this:

    1. Replace (or put in front of in the array, is probably easier) the current date detection function which is built into DataTables using a plug-in, basically as you have above (using the Array.unshift method).

    2. Have a 'neutral' date in the cell (probably not an attractive option)

    3. Set sType for the column manually, as you have done now.

    The first option seems like the most attractive to me to maintain automatic detection :-)

    Regards,
    Allan
This discussion has been closed.