I am trying to keep my list in a cookie list so that the user can sort the list as they want. And the next time they open my list of pages, this is the same order as when they exit the page. Is it possible?
I use jQuery Mobile and sort the list this way:
$(document).ready(function() { $('li').taphold(function() { // the clicked LI var clicked = $(this); // all the LIs above the clicked one var previousAll = clicked.prevAll(); // only proceed if it not already on top (no previous siblings) if(previousAll.length > 0) { // top LI var top = $(previousAll[previousAll.length - 1]); // immediately previous LI var previous = $(previousAll[0]); // how far up do we need to move the clicked LI? var moveUp = clicked.attr('offsetTop') - top.attr('offsetTop'); // how far down do we need to move the previous siblings? var moveDown = (clicked.offset().top + clicked.outerHeight()) - (previous.offset().top + previous.outerHeight()); // let move stuff clicked.css('position', 'relative'); previousAll.css('position', 'relative'); clicked.animate({'top': -moveUp}); previousAll.animate({'top': moveDown}, {complete: function() { // rearrange the DOM and restore positioning when we're done moving clicked.parent().prepend(clicked); clicked.css({'position': 'static', 'top': 0}); previousAll.css({'position': 'static', 'top': 0}); }}); } }); });
When the user clicks and holds the list item, which item moves to the top of the list.
+ My page should also use offline.
UPDATE:
How do I get links to a new list? Now I like it:
Js
$('#home').live('pageinit', function(event) { // Check if we have saved the list order getListOrder(); $('li').taphold(function() { // the clicked LI var clicked = $(this); // all the LIs above the clicked one var previousAll = clicked.prevAll(); // only proceed if it not already on top (no previous siblings) if (previousAll.length > 0) { // top LI var top = $(previousAll[previousAll.length - 1]); // immediately previous LI var previous = $(previousAll[0]); // how far up do we need to move the clicked LI? var moveUp = clicked.attr('offsetTop') - top.attr('offsetTop'); // how far down do we need to move the previous siblings? var moveDown = (clicked.offset().top + clicked.outerHeight()) - (previous.offset().top + previous.outerHeight()); // let move stuff clicked.css('position', 'relative'); previousAll.css('position', 'relative'); clicked.animate({ 'top': -moveUp }); previousAll.animate({ 'top': moveDown }, { complete: function() { // rearrange the DOM and restore positioning when we're done moving clicked.parent().prepend(clicked); clicked.css({ 'position': 'static', 'top': 0 }); previousAll.css({ 'position': 'static', 'top': 0 }); saveListOrder(); } }); } }); $('
HTML
<!DOCTYPE html> <html class="ui-mobile-rendering"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://jquerymobile.com/test/css/themes/default/jquery.mobile.css" /> <script src="http://jquerymobile.com/test/js/jquery.js"></script> <script src="http://jquerymobile.com/test/js/jquery.mobile.js"></script> </head> <body> <div data-role="page" id="home"> <div data-role="content"> <ul data-role="listview" data-theme="g" id="theList"> <li rel="1"><a href="http://www.example1.com">List Item #1</a></li> <li rel="2"><a href="http://www.example2.com">List Item #2</a></li> <li rel="3"><a href="http://www.example3.com">List Item #3</a></li> <li rel="4"><a href="http://www.example4.com">List Item #4</a></li> <li rel="5"><a href="http://www.example5.com">List Item #5</a></li> <li rel="6"><a href="http://www.example6.com">List Item #6</a></li> <li rel="7"><a href="http://www.example7.com">List Item #7</a></li> <li rel="8"><a href="http://www.example8.com">List Item #8</a></li> <li rel="9"><a href="http://www.example9.com">List Item #9</a></li> <li rel="10"><a href="http://www.example10.com">List Item #10</a></li> </ul> <br /> <a href="#" data-role="button" id="resetLO">Reset List Order</a> <a href="#" data-role="button" id="clearLS">Clear Local Storage</a> </div> </div> </body> </html>
And when I reorder the list and save it, it changes the link to "#".
Example: I move Item3 to the top of the list, so the link of each item changes http://www.example3.com → #
So, I think this is a clear old list and make the whole list again. Therefore, I think I need to change this line, but I do not know how to do it. list.append('<li rel="'+item+'"><a href="#">List Item #'+item+'</a></li>');