Is there a jQuery plugin for list sorting? -
Is there a jQuery plugin that will allow a list of items to sort ( & lt; li & gt; ; ... & lt; / li & gt;
) according to their meta data stored as a hidden element? For example, each list item contains two hidden information: author
and year
. I want to sort the list using those hidden elements. I'm surprised that jQuery is ready to use the plugin or not. So far, I have not found such a plugin. Only I have found that for table sorting.
& lt; Ul & gt; & Lt; Li & gt; Status 1 & lt; Input type = "hidden" name = "author" value = "peter" /> & Lt; Input type = "hidden" name = "year" value = "2004" /> & Lt; / Li & gt; & Lt; Li & gt; Status 2 & lt; Input type = "hidden" name = "author" value = "john" /> & Lt; Input type = "hidden" name = "year" value = "2005" /> & Lt; / Li & gt; & Lt; Li & gt; Status 3 & lt; Input type = "hidden" name = "author" value = "Tony" /> & Lt; Input type = "hidden" name = "year" value = "2006" /> & Lt; / Li & gt; & Lt; / Ul & gt;
Here you go:
& lt; Ul & gt; & Lt; Li & gt; Status 1 & lt; Input type = "hidden" name = "author" value = "peter" /> & Lt; Input type = "hidden" name = "year" value = "2004" /> & Lt; / Li & gt; & Lt; Li & gt; Status 2 & lt; Input type = "hidden" name = "author" value = "john" /> & Lt; Input type = "hidden" name = "year" value = "2005" /> & Lt; / Li & gt; & Lt; Li & gt; Status 3 & lt; Input type = "hidden" name = "author" value = "Tony" /> & Lt; Input type = "hidden" name = "year" value = "2006" /> & Lt; / Li & gt; & Lt; / Ul & gt; & Lt; Script type = "text / javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js" & gt; & Lt; / Script & gt; & Lt; Script type = "text / javascript" & gt; JQuery.fn.sort = function () {return to this.pushStack ([] .sort.apply (this, logic), []); }; Function mySorter (a, b) {return $ (a) .find ("[name = author]"). Val () & gt; $ (B) .find ("[name = author]"). Val ()? 1 1; }; $ (Document) .ready (function () {$ ('ul li'). MySorter .appendTo ('ul');}) & lt; / Script & gt;
I have optimized from:
Editing: Fixed error with val () and 'ul'
Comments
Post a Comment