diff options
author | lou <louiscuny@gmail.com> | 2011-06-29 18:27:18 +0200 |
---|---|---|
committer | lou <louiscuny@gmail.com> | 2011-06-29 18:27:18 +0200 |
commit | d8071eaaee31c851a1a86374a7c5c04c1060f8cc (patch) | |
tree | 68593ec2f4a121e5731026a00c7d009b239fd8d4 /js/jquery.multi-select.js | |
parent | 21a09a578f554cf3bf2782fd4799cbb219a05c8c (diff) | |
download | multi-select-d8071eaaee31c851a1a86374a7c5c04c1060f8cc.zip multi-select-d8071eaaee31c851a1a86374a7c5c04c1060f8cc.tar.gz multi-select-d8071eaaee31c851a1a86374a7c5c04c1060f8cc.tar.bz2 |
use $(option).attr(selected, selected) instead of $(select).val() method + introduce emptyArray setting to become truly unobstrusive
Diffstat (limited to 'js/jquery.multi-select.js')
-rw-r--r-- | js/jquery.multi-select.js | 63 |
1 files changed, 37 insertions, 26 deletions
diff --git a/js/jquery.multi-select.js b/js/jquery.multi-select.js index ff49f7e..1c590aa 100644 --- a/js/jquery.multi-select.js +++ b/js/jquery.multi-select.js @@ -2,7 +2,8 @@ var msMethods = { 'init' : function(options){ this.settings = { - disabledClass : 'disabled' + disabledClass : 'disabled', + emptyArray : false }; if(options) { this.settings = $.extend(this.settings, options); @@ -21,8 +22,12 @@ selectableUl = $('<ul></ul>'), selectedUl = $('<ul></ul>'); - if (ms.children("option[value='']").length == 0){ - ms.prepend("<option value=''></option>"); + if (multiSelects.settings.emptyArray){ + if (ms.children("option[value='']").length == 0){ + ms.prepend("<option value='' selected='selected'></option>"); + } else { + ms.children("option[value='']").attr('selected', 'selected'); + } } ms.data('settings', multiSelects.settings); ms.children("option:not(option[value=''])").each(function(){ @@ -58,20 +63,20 @@ 'select' : function(value, method){ var ms = this, msValues = ((ms.val() && ms.val()[0] != '') ? ms.val() : []), - alreadyPresent = $.inArray(value, msValues), - text = ms.find('option[value="'+value+'"]').text(), - titleAttr = ms.find('option[value="'+value+'"]').attr('title'); - - if(alreadyPresent == -1 || method == 'init'){ + selectedOption = ms.find('option[value="'+value +'"]'), + text = selectedOption.text(), + titleAttr = selectedOption.attr('title'); + var selectedLi = $('<li ms-value="'+value+'">'+text+'</li>').detach(), - newValues = $.merge(msValues, [value]), selectableUl = $('#ms-'+ms.attr('id')+' .ms-selectable ul'), selectedUl = $('#ms-'+ms.attr('id')+' .ms-selection ul'), - selectableLi = selectableUl.children('li[ms-value="'+value+'"]'); - - if (!selectableLi.attr('disabled')){ + selectableLi = selectableUl.children('li[ms-value="'+value+'"]'), + haveToSelect = !selectableLi.attr('disabled') && value != '' && + ((method == 'init' && selectedOption.attr('selected')) || + (method != 'init' && !selectedOption.attr('selected'))) + if (haveToSelect ){ selectableLi.hide(); - ms.val(newValues); + selectedOption.attr('selected', 'selected'); if(titleAttr){ selectedLi.attr('title', titleAttr) } @@ -79,32 +84,38 @@ ms.multiSelect('deselect', $(this).attr('ms-value')); }); selectedUl.append(selectedLi); - selectedUl.trigger('change'); - selectableUl.trigger('change'); - if (typeof ms.data('settings').afterSelect == 'function' && method != 'init') { - ms.data('settings').afterSelect.call(this, value, text); + if (ms.children("option[value='']")){ + ms.children("option[value='']").removeAttr('selected'); } + if(method != 'init'){ + selectedUl.trigger('change'); + selectableUl.trigger('change'); + if (typeof ms.data('settings').afterSelect == 'function' && method != 'init') { + ms.data('settings').afterSelect.call(this, value, text); + } } } }, 'deselect' : function(value){ var ms = this, - msValues = (ms.val() ? ms.val() : []), - present = false, - newValues = $.map(msValues, function(e){ if(e != value){ return e; }else{ present = true}}); - - if(present){ + selectedUl = $('#ms-'+ms.attr('id')+' .ms-selection ul'), + selectedOption = ms.find('option[value="'+value +'"]'), + selectedLi = selectedUl.children('li[ms-value="'+value+'"]'); + + if(selectedLi){ var selectableUl = $('#ms-'+ms.attr('id')+' .ms-selectable ul'), selectedUl = $('#ms-'+ms.attr('id')+' .ms-selection ul'), selectableLi = selectableUl.children('li[ms-value="'+value+'"]'), selectedLi = selectedUl.children('li[ms-value="'+value+'"]'); - if (newValues.length == 0){ - newValues = ['']; - } - ms.val(newValues); + selectedOption.removeAttr('selected'); selectableLi.show(); selectedLi.remove(); + if (ms.data('settings').emptyArray && selectedUl.children('li').length == 0){ + if (ms.children("option[value='']")){ + ms.children("option[value='']").attr('selected', 'selected'); + } + } selectedUl.trigger('change'); selectableUl.trigger('change'); if (typeof ms.data('settings').afterDeselect == 'function') { |