PDA

View Full Version : Jquery cookie to session help



icklechurch
Nov 18th, 2009, 05:43 PM
Hi,

I've just written a lovely bit of code where a user sorts a list and then this list is returned to them. Problem is, that I used cookies to do this and I've just found out that cookies are banned for this website - ah!

My code is below - can someone please give me some pointers? I've tried a few things but can't get it to work :(

// function that writes the list order to a cookie
function getOrder() {
// save custom order to cookie
$.cookie(setCookieName, $(setSelector).sortable("toArray"), { expires: setCookieExpiry, path: "/" });
}

// function that restores the list order from a cookie
function restoreOrder() {
var list = $(setSelector);
if (list == null) return

// fetch the cookie value (saved order)
var cookie = $.cookie(setCookieName);
if (!cookie) return;

// make array from saved order
var IDs = cookie.split(",");

// fetch current order
var items = list.sortable("toArray");

// make array from current order
var rebuild = new Array();
for ( var v=0, len=items.length; v<len; v++ ){
rebuild[items[v]] = items[v];
}

for (var i = 0, n = IDs.length; i < n; i++) {

// item id from saved order
var itemID = IDs[i];

if (itemID in rebuild) {

// select item id from current order
var item = rebuild[itemID];

// select the item according to current order
var child = $("div.ui-sortable").children("#" + item);

// select the item according to the saved order
var savedOrd = $("div.ui-sortable").children("#" + itemID);

// remove all the items
child.remove();

// add the items in turn according to saved order
// we need to filter here since the "ui-sortable"
// class is applied to all div elements and we
// only want the very first! You can modify this
// to support multiple lists - not tested!
$("div.ui-sortable").filter(":first").append(savedOrd);
}
}
}

// code executed when the document loads
$(function() {
// here, we allow the user to sort the items
$(setSelector).sortable({
axis: "y",
cursor: "move",
update: function() { getOrder(); }
});

// here, we reload the saved order
restoreOrder();
});

icklechurch
Nov 23rd, 2009, 12:37 PM
Can anyone help me with this please?

Philip M
Nov 23rd, 2009, 04:36 PM
Not sure what you are wanting - if cookies are not available you will have to user server-side coding.