Skip to content

Instantly share code, notes, and snippets.

@carlosfilho88
Created November 12, 2014 17:14
Show Gist options
  • Save carlosfilho88/d7ec7fad97a08f09dc5f to your computer and use it in GitHub Desktop.
Save carlosfilho88/d7ec7fad97a08f09dc5f to your computer and use it in GitHub Desktop.
Multiple checkbox jquery + zend form multiple checkbox (ZF1)
var checkboxes = function() {
var items = new Array();
return {
"add": function(val) {
items.push(val);
},
"addUnique": function(val) {
Array.prototype.inArray = function(comparer) {
for(var i = 0; i < this.length; i++) {
if(comparer(this[i])) return true;
}
return false;
};
Array.prototype.pushIfNotExist = function(element, comparer) {
if (!this.inArray(comparer)) {
this.push(element);
}
};
items.pushIfNotExist(val, function(e) {
return e === val;
});
},
"remove": function (val) {
indx = items.indexOf(val);
if(indx!=-1) items.splice(indx, 1);
},
"clear": function() {
items = null;
},
"items": function() {
return items;
}
}
}
var chklist = new checkboxes();
$('#checkall').on('click', function(e){
var all_checkboxes = $('input:checkbox');
all_checkboxes.each(function() {
if($(this).is(':visible') && !$(this).prop('checked')) {
$(this).prop('checked','checked');
chklist.addUnique($(this).val());
$(this).parent().prepend('<span>'+chklist.items().length+'. </span>');
}
});
});
$('#uncheckall').on('click', function(e){
var all_checkboxes = $('input:checkbox');
all_checkboxes.each(function() {
if($(this).is(':visible') && $(this).prop('checked')) {
$(this).prop('checked', false);
$(this).parent().find('span').remove();
}
});
chklist.clear();
chklist = new checkboxes();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment