Semi-colon in filter string breaks cookies [read second post]

Semi-colon in filter string breaks cookies [read second post]

ZerxerZerxer Posts: 13Questions: 0Answers: 0
edited January 2010 in Bug reports
Alright, I'm trying to get DataTables to keep the current user's display number and sort type with the bStateSave boolean, but it doesn't appear to work. I'm going to assume I'm doing something wrong or forgetting something, so I will paste my code here and hope for the best.

[code]pTable = $("#pagesTable").dataTable({
fnInitComplete: function(){
$("#pagesTable_length select").appendTo("#numRecords");
$(".dataTables_paginate").appendTo("#pagesTable_pages");
$("#pagesTable_length, #pagesTable_filter").hide();
},
bSortClasses: false,
bStateSave: true,
bSearchable: false,
iDisplayLength: 100,
sPaginationType: 'full_numbers',
aoColumns: [{sType: 'num-html'}, null, {sType: 'html'}, {sType: 'html'}, {sType: 'html'}, {sType: 'html'}, {sType: 'num-html'}, null, {bSortable: false}]
});[/code]

I'm not sure if it possibly has something to do with me modifying the DOM on fnInitComplete. Anyways, thanks in advance.

Replies

  • ZerxerZerxer Posts: 13Questions: 0Answers: 0
    edited January 2010
    I found the cause of the issue, which is this code here:

    [code]$("#filter_active").change(function(){
    pTable.fnFilter($(this).is(':checked') ? ' Active' : '', 7);
    }).change();[/code]

    Filtering as  Active is breaking the cookie. When I look at my cookie, it ends abruptly at &nbsp, with no semi-colon even. So the semi-colon is ending the cookie (which, I'm pretty sure that's how document.cookie works with semi-colons, right?). So yeah, seems DataTables needs to escape this or something.

    By the way, the reason I'm even doing this is because that column can have either 'Inactive' or 'Active' in it and if I just filter by 'Active', it also matches 'Inactive' (case-insensitive?). And I'm pretty sure I had some type of problem when I tried just using a space without actually typing ' '
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi Zerxer,

    Good find - thanks for spotting this and reporting it back to me. :-)

    Your are quite right in your analysis. The semi-colon was causing issues since DataTables wasn't correctly encoding it. I'll release a fix for this in 1.6.1, but until then you can replace the two cookie functions with the following to fix this in your local copy:

    [code]
    /*
    * Function: _fnCreateCookie
    * Purpose: Create a new cookie with a value to store the state of a table
    * Returns: -
    * Inputs: string:sName - name of the cookie to create
    * string:sValue - the value the cookie should take
    * int:iSecs - duration of the cookie
    */
    function _fnCreateCookie ( sName, sValue, iSecs )
    {
    var date = new Date();
    date.setTime( date.getTime()+(iSecs*1000) );

    /*
    * Shocking but true - it would appear IE has major issues with having the path being
    * set to anything but root. We need the cookie to be available based on the path, so we
    * have to append the pathname to the cookie name. Appalling.
    */
    sName += '_'+window.location.pathname.replace(/[\/:]/g,"").toLowerCase();

    document.cookie = sName+"="+encodeURIComponent(sValue)+
    "; expires="+date.toGMTString()+"; path=/";
    }

    /*
    * Function: _fnReadCookie
    * Purpose: Read an old cookie to get a cookie with an old table state
    * Returns: string: - contents of the cookie - or null if no cookie with that name found
    * Inputs: string:sName - name of the cookie to read
    */
    function _fnReadCookie ( sName )
    {
    var sNameEQ = sName +'_'+ window.location.pathname.replace(/[\/:]/g,"").toLowerCase() + "=";
    var sCookieContents = document.cookie.split(';');

    for( var i=0 ; i
This discussion has been closed.