diff options
author | louis cuny <louiscuny@gmail.com> | 2011-05-05 03:01:53 -0700 |
---|---|---|
committer | louis cuny <louiscuny@gmail.com> | 2011-05-05 03:01:53 -0700 |
commit | 46ec109620af198e54c3bbbac2bf60e64618f027 (patch) | |
tree | 4b2a59cbb7e5da46d361a2eef51ae38bb4924b3b /js/jquery.multi-select.js | |
parent | d0cc0913d10c708de4277ca55c9922844ad2dbfd (diff) | |
parent | fd97c2f5dbe570679f4d160e64d8f44a071630e3 (diff) | |
download | multi-select-46ec109620af198e54c3bbbac2bf60e64618f027.zip multi-select-46ec109620af198e54c3bbbac2bf60e64618f027.tar.gz multi-select-46ec109620af198e54c3bbbac2bf60e64618f027.tar.bz2 |
Merge pull request #1 from Serophos/master
That's it for now
Diffstat (limited to 'js/jquery.multi-select.js')
-rw-r--r-- | js/jquery.multi-select.js | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/js/jquery.multi-select.js b/js/jquery.multi-select.js index fbeec76..c9ed0a7 100644 --- a/js/jquery.multi-select.js +++ b/js/jquery.multi-select.js @@ -1,5 +1,5 @@ (function($){ - + var msMethods = { 'init' : function(options){ this.settings = { @@ -7,12 +7,12 @@ if(options) { this.settings = $.extend(this.settings, options); } - + var multiSelects = this; multiSelects.hide(); - + multiSelects.each(function(){ - + var ms = $(this); ms.attr('id', ms.attr('id') != undefined ? ms.attr('id') : 'select'+((new Date()).getTime())); var container = $('<div id="ms-'+ms.attr('id')+'" class="ms-container"></div>').detach(), @@ -20,11 +20,12 @@ selectedContainer = $('<div class="ms-selection"></div>').detach(), selectableUl = $('<ul></ul>').detach(), selectedUl = $('<ul></ul>').detach(); - + ms.data('settings', multiSelects.settings); ms.children('option').each(function(){ - var selectableLi = $('<li ms-value="'+$(this).val()+'">'+$(this).text()+'</li>').detach(); - + + var selectableLi = $('<li ms-value="'+$(this).val()+'" title="' + $(this).attr('title') + '">'+$(this).text()+'</li>').detach(); + selectableLi.click(function(){ ms.multiSelect('select', $(this).attr('ms-value')); }); @@ -51,14 +52,15 @@ msValues = (ms.val() ? ms.val() : []), alreadyPresent = $.inArray(value, msValues), text = ms.find('option[value="'+value+'"]').text(); - + title_attr = ms.find('option[value="'+value+'"]').attr('title'); + if(alreadyPresent == -1 || method == 'init'){ - var selectedLi = $('<li ms-value="'+value+'">'+text+'</li>').detach(), + var selectedLi = $('<li ms-value="'+value+'" title="' + title_attr + '">'+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+'"]'); - + selectableLi.hide(); ms.val(newValues); selectedLi.click(function(){ @@ -74,14 +76,14 @@ var ms = this, msValues = (ms.val() ? ms.val() : []), present = false, - newValues = $.map(msValues, function(e){ if(e != value){ return e; }else{ present = true}}); - + newValues = $.map(msValues, function(e){ if(e != value){ return e; }else{ present = true} return false;}); + if(present){ 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+'"]').detach(); - + ms.val(newValues); selectableLi.show(); selectedLi.remove(); @@ -102,8 +104,8 @@ ms.multiSelect('deselect', $(this).val(), 'deselect_all'); }); } - } - + }; + $.fn.multiSelect = function(method){ if ( msMethods[method] ) { return msMethods[method].apply( this, Array.prototype.slice.call( arguments, 1 )); @@ -112,5 +114,6 @@ } else { if(console.log) console.log( 'Method ' + method + ' does not exist on jquery.multiSelect' ); } - } + return false; + }; })(jQuery);
\ No newline at end of file |