Last active
November 1, 2018 11:44
-
-
Save vmcilwain/49d273751fa1c964f5185f518f1ddb60 to your computer and use it in GitHub Desktop.
Processing iCheck checkboxes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Code for processing icheck checkboxes | |
# with slight modification it can be used with normal checkboxes as well | |
# Check or uncheck all checkboxes | |
# | |
$('#checkAll').on 'ifChecked ifUnchecked', (event) -> | |
$checkboxes = $(this).closest('form').find(':checkbox') | |
if event.type == 'ifChecked' | |
$checkboxes.prop 'checked', true | |
else | |
$checkboxes.prop 'checked', false | |
$checkboxes.iCheck 'update' | |
return | |
# Decide how to dispaly select all checkbox based on other checkboxes bing checked or not | |
# | |
$("form input[type='checkbox']").on 'ifChanged', (event) -> | |
# Returns an array of 1 JQuery checkbox object | |
$checkAll = $('#checkAll') | |
# Removes that one object from total number of checkbox JQuery objects found on the page | |
$checkboxes = $(this).closest('form').find(':checkbox').filter (index, $elm) -> | |
return $elm != $checkAll[0] | |
# Decides what to do with that one JQuery checkobx object based on the number of checkboxex checked | |
if $checkboxes.filter(':checked').length == $checkboxes.length | |
$checkAll.prop 'checked', true | |
else | |
$checkAll.prop 'checked', false | |
$checkAll.iCheck 'update' | |
return | |
# Check all children under a parent filter. | |
# A parent can't be converted unless all of the children are converted with it or it doesno't have children | |
# | |
# When checkbox with data attribute is_child=false | |
$("form :checkbox[data-is-child='false']").on 'ifChecked ifUnchecked', (event) -> | |
checkbox_id = $(this).val() | |
if event.type == 'ifChecked' | |
# find and select all children witih the parent id of the checkbox checked | |
$("form :checkbox[data-parent-id='#{checkbox_id}']").prop('checked', true).iCheck('update') | |
else | |
# find and de-select all children witih the parent id of the checkbox checked | |
$("form :checkbox[data-parent-id='#{checkbox_id}']").prop('checked', false).iCheck('update') | |
return | |
# Uncheck the parent checkbox if a child is de-selected | |
$("form :checkbox[data-is-child='true']").on 'ifChecked ifUnchecked', (event) -> | |
# find the parent id of the child that has been clicked | |
parent_id = $(this).data('parentId') | |
# get all checkboxes with that parent id | |
$checkboxes = $("form :checkbox[data-parent-id='#{parent_id}']") | |
# when a check event happens and all of the checkboxes are checked, check the checkbox with the value of the parent id of the child selected | |
if event.type == 'ifChecked' && $checkboxes.filter(':checked').length == $checkboxes.length | |
$("form :checkbox[value='#{parent_id}']").prop('checked', true).iCheck('update') | |
else | |
# uncheck check the checkbox with the value of the parent id of the child when a child is unchecked or all children are unchecked | |
$("form :checkbox[value='#{parent_id}']").prop('checked', false).iCheck('update') | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment