diff options
author | lou <louiscuny@gmail.com> | 2012-12-18 18:08:59 +0100 |
---|---|---|
committer | lou <louiscuny@gmail.com> | 2012-12-18 18:08:59 +0100 |
commit | 95271c2db8313eb2a4b846489ab76655aea2a249 (patch) | |
tree | 403d23155551ff7bd32571964ee3acbd22376dea /js/jquery.multi-select.js | |
parent | a35cf612fee008ac68ab3f1a707ac4149533adf3 (diff) | |
download | multi-select-95271c2db8313eb2a4b846489ab76655aea2a249.zip multi-select-95271c2db8313eb2a4b846489ab76655aea2a249.tar.gz multi-select-95271c2db8313eb2a4b846489ab76655aea2a249.tar.bz2 |
fixes #49: new option to select nested optgroup options0.9.2
Diffstat (limited to 'js/jquery.multi-select.js')
-rw-r--r-- | js/jquery.multi-select.js | 58 |
1 files changed, 42 insertions, 16 deletions
diff --git a/js/jquery.multi-select.js b/js/jquery.multi-select.js index 6eb3da9..4ac0986 100644 --- a/js/jquery.multi-select.js +++ b/js/jquery.multi-select.js @@ -1,5 +1,5 @@ /* -* MultiSelect v0.9.1 +* MultiSelect v0.9.2 * Copyright (c) 2012 Louis Cuny * * This program is free software. It comes without any warranty, to @@ -44,27 +44,52 @@ var optgroupLabel = null, optgroupId = null, optgroupCpt = 0, - scroll = 0; - + scroll = 0, + optgroupContainerTemplate = '<li class="ms-optgroup-container"></li>', + optgroupUlTemplate = '<ul class="ms-optgroup"></ul>', + optgroupLiTemplate = '<li class="ms-optgroup-label"><span></span></li>'; ms.find('optgroup, option').each(function(){ if ($(this).is('optgroup')){ - optgroupLabel = $(this).attr('label'); + optgroupLabel = '<span>'+$(this).attr('label')+'</span>'; optgroupId = 'ms-'+ms.attr('id')+'-optgroup-'+optgroupCpt; - that.$selectableUl.append( - '<li class="ms-optgroup-container" id="'+optgroupId+'-selectable">\ - <ul class="ms-optgroup">\ - <li class="ms-optgroup-label"><span>'+optgroupLabel+'</span></li>\ - </ul>\ - </li>'); - that.$selectionUl.append( - '<li class="ms-optgroup-container" id="'+optgroupId+'-selection">\ - <ul class="ms-optgroup">\ - <li class="ms-optgroup-label"><span>'+optgroupLabel+'</span></li>\ - </ul>\ - </li>'); + var optgroup = $(this), + optgroupSelectable = $(optgroupContainerTemplate), + optgroupSelection = $(optgroupContainerTemplate), + optgroupSelectionLi = $(optgroupLiTemplate), + optgroupSelectableLi = $(optgroupLiTemplate); + + if (that.options.selectableOptgroup){ + optgroupSelectableLi.on('click', function(){ + var values = optgroup.children(':not(:selected)').map(function(){ return $(this).val() }).get(); + that.select(values); + }); + + optgroupSelectionLi.on('click', function(){ + var values = optgroup.children(':selected').map(function(){ return $(this).val() }).get(); + that.deselect(values); + }); + } + + optgroupSelectableLi.html(optgroupLabel); + + optgroupSelectable.attr('id', optgroupId+'-selectable') + .append($(optgroupUlTemplate) + .append(optgroupSelectableLi)); + + that.$selectableUl.append(optgroupSelectable); + + optgroupSelectionLi.html(optgroupLabel); + + optgroupSelection.attr('id', optgroupId+'-selection') + .append($(optgroupUlTemplate) + .append(optgroupSelectionLi)); + + that.$selectionUl.append(optgroupSelection); + optgroupCpt++; + } else { var attributes = ""; @@ -389,6 +414,7 @@ } $.fn.multiSelect.defaults = { + selectableOptgroup: false, disabledClass : 'disabled', dblClick : false } |