Skip to content

Instantly share code, notes, and snippets.

@bernardop
Created April 25, 2012 20:18
Show Gist options
  • Select an option

  • Save bernardop/2493011 to your computer and use it in GitHub Desktop.

Select an option

Save bernardop/2493011 to your computer and use it in GitHub Desktop.
Gongos jQuery plugins
// Have a checkall and checkone in the same page
(function ($) {
jQuery.fn.onePageCheckallCheckone = function (options) {
// Default settings
var settings = $.extend({
'firstQuestion': '',
'secondQuestion': '',
'isLastExclusive': false
}, options);
if (!settings.firstQuestion) return this;
// Re-calculate background of rows
var alternateRowColor = function () {
var secondQSelectedRows = $('.ShownRow');
secondQSelectedRows.each(function (index) {
var tds = $(this).children('td[id]');
if (index % 2 === 0) {
$(tds[0]).removeClass('NormalCell').addClass('AlternateCell');
$(tds[1]).removeClass('NormalCell').addClass('AlternateCell');
} else {
$(tds[0]).removeClass('AlternateCell').addClass('NormalCell');
$(tds[1]).removeClass('AlternateCell').addClass('NormalCell');
}
});
};
// Second question text
var secondQText = $('#' + settings.secondQuestion + 'Text');
// Second question rows of choices
var secondQRows = $('td[id^="CellOption_' + settings.secondQuestion + '"]').parent();
// Hide second question
secondQText.hide();
secondQRows.hide();
// Get choices that should be shown when the page loads (for postback)
var firstQChoicesSelected = $('td[class*="SelectedCell"][id*="CellOption_' + settings.firstQuestion + '_"]');
if ($(firstQChoicesSelected).length >= 1) {
secondQText.show();
firstQChoicesSelected.each(function (index) {
var id = $(this)[0].id;
var qNum = id.substr(id.lastIndexOf("_") + 1);
$(secondQRows[qNum - 1]).addClass('ShownRow').show();
});
alternateRowColor();
}
// When a choice is clicked in first question...
$('td[id*="_' + settings.firstQuestion + '_"]').click(function () {
// Count of selected choices in first question
var firstQSelectedCount = $('td[class*="SelectedCell"][id*="_' + settings.firstQuestion + '_"]').parent().length;
// Is row selected in previous question?
var isSelected = $(this).hasClass('SelectedCell');
// Get the clicked element index
var id = $(this)[0].id;
var index = parseInt(id.substr(id.lastIndexOf("_") + 1));
// If last index of previous question is exclusive (Generally 'None')
var firstQChoicesNum = $('td[id^="CellOption_' + settings.firstQuestion + '"]').length;
if (settings.isLastExclusive && index === firstQChoicesNum) {
secondQText.fadeOut();
secondQRows.removeClass('ShownRow').fadeOut();
} else {
// Show or remove clicked row
if (isSelected) {
$(secondQRows[index - 1]).addClass('ShownRow').fadeIn();
} else {
$(secondQRows[index - 1]).removeClass('ShownRow').fadeOut();
}
// Show or remove question text
if (firstQSelectedCount === 1) {
secondQText.fadeIn();
}
if (firstQSelectedCount === 0) {
secondQText.fadeOut();
}
}
// Re-do background of rows
alternateRowColor();
});
return this;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment