Bug: Encoding on DateTimestamp + Sorting = Not working

Bug: Encoding on DateTimestamp + Sorting = Not working

chobo2chobo2 Posts: 23Questions: 2Answers: 0
edited July 2010 in Bug reports
Hi

I am using jquery datatables and MS XSS library. (http://www.microsoft.com/downloads/details.aspx?FamilyId=051ee83c-5ccf-48ed-8463-02f56a6bfc09&displaylang=en)

I did this

[code]AntiXss.HtmlEncode(MyDate.ToString("MM/dd/yyyy h:mm tt"))));[/code]

this renders this

[code]07/22/2010 4:04 PM [/code]

// hmm even in code block it unencodes it. Not sure how to show u.

So its all encoded. Now for whatever reason this messes datatables up. It can't figure out how to sort anymore.

I am guessing that datatables does not understand it when it is encoded. So I am unsure how to get around this and keep my security levels up.


You can see my post here of how it screws up my ordering up:

http://datatables.net/forums/comments.php?DiscussionID=2305&page=1#Item_1

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    There are a number of plug-ins for sorting date/times which Javascripts Date().parse() does not recognise (which is what DataTables uses internally): http://datatables.net/plug-ins/sorting . You may have to modify one, but probably not too much :-)

    Regards,
    Allan
  • chobo2chobo2 Posts: 23Questions: 2Answers: 0
    So this is a internal javascript problem?
  • dahepedahepe Posts: 7Questions: 0Answers: 0
    edited July 2010
    Hi,

    I'm also using DataTables to display/sort by date fields and stepped into similar issues.

    My solution was not to rely on JavaScripts Date facilities (which really kind of s...). To ensure that the date is correctly sorted in the table I set the table date values in ISO format (e.g. 2010-07-22 16:04:00) and specified the column type as string. For display of the date columns I added a render function, that takes the ISO formatted string, converts it into a date object an formats the date object to my needs. For parsing and formatting I use the jQuery datepicker parseDate and formatDate function. Don't know if they will fit your needs as these functions don't support time values. Perhaps this (http://www.xaprb.com/blog/2005/12/20/javascript-date-parsing/) library is more what you're looking for but I haven't used it myself yet.

    Thus my table initalization looks like:

    [code]oTable = $('#tbl_timedata').dataTable({
    "aoColumns": [
    { "sType": "string", "sWidth": "50%", "bUseRendered": false,
    "fnRender": function ( oObj ) { return getFormattedDate(oObj.aData[0]);}
    },
    { "sType": "string", "sWidth": "50%", "bUseRendered": false,
    "fnRender": function ( oObj ) { return getFormattedDate(oObj.aData[1]);}
    } ]
    });[/code]


    [code]function getFormattedDate( value ) {
    var oDate = $.datepicker.parseDate($.datepicker.ISO_8601, value);
    return $.datepicker.formatDate('D, dd. MM yy', oDate);
    };[/code]

    It is important to set bUseRendered to false to ensure DataTables uses the ISO formatted date for sorting and to set sType to string to prevent DateTable from guessing that the column might be a date. Other possibilities I tried (using date type columns, rely on JavaScript's date parsing facilities) did not work for me (Cross browser issues IE vs. Firefox, sorting did not work properly)..

    Best Regards
This discussion has been closed.