Soldato
- Joined
- 30 Nov 2005
- Posts
- 3,084
- Location
- London
Following up from my draggable/sortable thread, have found a few tutorials helping with the moving phase, but having trouble with the cookie side.
The problem is with loading the positions, think the saving code is ok but not the loading.
Any ideas?
The problem is with loading the positions, think the saving code is ok but not the loading.
Any ideas?
PHP:
<script type="text/javascript">
$(document).ready(function(){
// Get items
function getItems(exampleNr)
{
var columns = [];
$(exampleNr + ' ul.sortable-list').each(function(){
columns.push($(this).sortable('toArray').join(','));
});
return columns.join('|');
}
// Load items from cookie
function loadItemsFromCookie(name)
{
if ($.cookie(name) != null )
{
renderItems($.cookie(name));
}
else
{
alert('No items saved in "' + name + '".');
}
}
// Render items
function renderItems(items)
{
var html = '';
var columns = items.split('|');
for ( var c in columns )
{
html += '<div class="column left';
if ( c == 0 )
{
html += ' first';
}
html += '"><ul class="sortable-list">';
if ( columns[c] != '' )
{
var items = columns[c].split(',');
for ( var i in items )
{
html += '<li class="sortable-item" id="' + items[i] + '">Sortable item ' + items[i] + '</li>';
}
}
html += '</ul></div>';
}
$('#example-1-4').html(html);
}
// load positions form cookies
$('#example-1-4 .sortable-list').each( function( index ){
loadItemsFromCookie('cookie-b');
});
// Example 1.4: Sortable and connectable lists (within containment)
$('#example-1-4 .sortable-list').sortable({
connectWith: '#example-1-4 .sortable-list',
containment: '#containment'
});
// save positions into cookies
$('#example-1-4 .sortable-list').sortable({
connectWith: '#example-1-4 .sortable-list',
update: function(){
$.cookie('cookie-b', getItems('#example-1-4'));
}
});
});
</script>
Last edited: