Skip to content

Instantly share code, notes, and snippets.

@rakuishi
Created January 22, 2015 08:33
Show Gist options
  • Save rakuishi/89ead9c0d49858f436fb to your computer and use it in GitHub Desktop.
Save rakuishi/89ead9c0d49858f436fb to your computer and use it in GitHub Desktop.
/*! Table Expander jQuery Plugin | (c) 2014 rakuishi | MIT license */
;(function($) {
$.fn.tableExpander = function(options) {
var defaults = {
'expandClassName' : 'expand',
'backgroundColor' : 'transparent',
'hoverBackgroundColor' : '#f0f0f0',
};
var setting = $.extend(defaults, options);
var trArray = $('> tbody tr', this);
function onExpand(event) {
for (var i = event.data.num + 1; i < trArray.length; i++) {
if (trArray[i].className === setting['expandClassName']) { break; }
if ($(trArray[i]).is(':hidden')) {
$(trArray[i]).show();
} else {
$(trArray[i]).hide();
}
}
}
for (var i = 0; i < trArray.length; i++) {
if (trArray[i].className === setting['expandClassName']) {
$(trArray[i])
.bind('click', {num: i}, onExpand)
.css('cursor', 'pointer')
.css('background-color', setting['backgroundColor'])
.mouseout(function() { $(this).css('background-color', setting['backgroundColor']); })
.mouseover(function() { $(this).css('background-color', setting['hoverBackgroundColor']); });
} else {
$(trArray[i]).hide();
}
}
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment