jQuery: What is the best way to implement a sortable list with javascript?
-
jQuery UI was most obvious to me -- but is there a better, leaner, faster way to implement a sortable list? Can I do it about as easily without having to rely on jQuery UI? If so, how?
-
Answer:
If you're looking for a quick solution, use an existing one and as jQuery is so great, I'd say use a jQuery plugin. You don't need to use a plugin from jQuery UI, in fact the only sorting one I'm aware of that they have is a drag and drop based sorting plugin. If this isn't what you need then there are other sorting plugins that you can use, you will find plenty on Google. If you really don't want to use jQuery for whatever reason then you can base a very simple design off Array.prototype.sort() (see the Mozilla developer Network for more info on this function). This will sort numerically and alphabetically. As to the elements you actually want to sort, use an unordered list (can use others) to display each item. The general principal is to grab some data from each item and store it in an array, then use the native sort function. As a basic example you could do something like this (using jQuery selectors, you can equally use the more verbose plain javascript equivalent): HTML: <ul> <li>Carrot</li> <li>Apple</li> <li>Bannana</li> <li>Lettuce</li> </ul> Javascript: var i, sortArray = new Array(), refObject = new Object(); $("li").each(function(){ sortArray.push( $(this).text() ); refObject[$(this).text()] = $(this).clone(); }).remove(); sortArray.sort(); for(i=0;i<sortArray.length;i++){ refObject[ sortArray.pop() ].appendTo('ul'); } This is quite "hard coded" to the HTML and you could adjust it to make your own simple jQuery plugin for more generic usage. If you want to sort by other things like price etc. you can do that too. Grab the price from the element, strip the '$' character from the price and then use sort(). Numeric sorting requires some extra arguments compared to alphabetical sorting so I suggest you read up on it.
Simon Blee at Quora Visit the source
Other answers
Just go with jeasyUI: http://jeasyui.com, and look at their datagrid plugin. It's a suite of plugins that has completely replaced jQueryUI for me. Anything you might want to do with your interfaces is already supported and there's plenty of material to learn from - both demos and tutorials are available, along with a very detailed documentation.
Bruno Skvorc
This one did it for me. Very small footprint, and Touch-capable too: http://rubaxa.github.io/Sortable/ Play with the sortable lists demos, kool features! I use this since I didn't need most of what jQuery UI core and related CSS offered.
Dan E. NewMan
First: Load in jQuery UI.Then:Your list (your example list): //HTML <ul id="sortable"> <li>Item One</li> <li>Item Two</li> <li>Item Three</li> <li>Item Four</li> </ul> You can make it sortable like this: $(document).ready(function(){ $("#sortable").click(function(){ $(this).sortable(); }); }); รขCheers,Ben
Ben Dains
Related Q & A:
- What is the best way to calculate a date difference?Best solution by Stack Overflow
- What is the best way to sell a timeshare?Best solution by Yahoo! Answers
- What's the best way to get a job in a restaurant?Best solution by Yahoo! Answers
- What's the best way to make a good impression at a job interview?Best solution by Yahoo! Answers
- What is the best way to negotiate a salary for a new position?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.