(function($) {
$.fn.sortlist = function(options) {
var defaults = {
sortElement: 'li',
sortFunction: function(a, b) {
var compA = $(a).text();
var compB = $(b).text();
return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
}
};
var options = $.extend(defaults, options);
return this.each(function(listIndex, listInstance) {
var listItems = $(listInstance).children(options.sortElement);
listItems.sort(options.sortFunction);
$.each(listItems, function(listItemIndex, listItemInstance) {
$(listInstance).append(listItemInstance);
});
});
};
})(jQuery);
(function($) {
$.fn.hideextra = function(options) {
var defaults = {
extraElement: 'li',
extraClass: 'extra',
buttonClass: 'showMore',
maxItems: 4,
moreText: "more...",
lessText: "less..."
};
var options = $.extend(defaults, options);
this.each(function(listIndex, listInstance) {
var listItems = $(listInstance).children(options.extraElement);
for (i = 0; i < listItems.length; i++) {
if (i > (options.maxItems)) {
$(listItems[i]).hide();
$(listItems[i]).addClass(options.extraClass);
}
}
var hiddenLis = $(options.extraElement + '.' + options.extraClass, $(listInstance));
// Add More/Less Link
if (listItems.length > options.maxItems) {
$(listInstance).append('<li><a href="#_" class="' + options.buttonClass + '">' + hiddenLis.length + ' ' + options.moreText + '</a></li>');
}
// Add the click events
var moreLink = $('.' + options.buttonClass, $(listInstance));
moreLink.click(function() {
if (moreLink.text().indexOf(options.moreText) > 0) {
hiddenLis.show();
moreLink.text(options.lessText);
}
else {
hiddenLis.hide();
moreLink.text(hiddenLis.length + ' ' + options.moreText);
}
});
});
};
})(jQuery);
// Usage
$("ul.sorted").sortlist().hideextra();
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Jquery Plugin Example Page</title>
</head>
<body>
<h2>Sorted and Hidden Extra</h2>
<ul class="sorted">
<li>Tomatoe</li>
<li>Strawberry</li>
<li>Pear</li>
<li>Grape</li>
<li>Peach</li>
<li>Blackberry</li>
<li>Kiwi</li>
<li>Bannana</li>
<li>Apple</li>
<li>Blueberry</li>
</ul>
</body>
</html>
body
{
background-color: #fff;
font-size: .75em;
font-family: Verdana, Helvetica, Sans-Serif;
margin: 0;
padding: 0;
color: #696969;
}
a:link
{
color: #034af3;
text-decoration: underline;
}
a:visited
{
color: #505abc;
}
a:hover
{
color: #1d60ff;
text-decoration: none;
}
a:active
{
color: #12eb87;
}
p, ul
{
margin-bottom: 20px;
line-height: 1.6em;
}
/* HEADINGS
----------------------------------------------------------*/
h1, h2, h3, h4, h5, h6
{
font-size: 1.5em;
color: #000;
font-family: Arial, Helvetica, sans-serif;
}
h1
{
font-size: 2em;
padding-bottom: 0;
margin-bottom: 0;
}
h2
{
padding: 0 0 10px 0;
}
h3
{
font-size: 1.2em;
}
h4
{
font-size: 1.1em;
}
h5, h6
{
font-size: 1em;
}