multiSelect: Make sure last added entry is selected and some code cleanup.

This commit is contained in:
Thomas Tanghus 2012-12-04 16:03:02 +01:00
parent b62542f74c
commit fefb6aca1a
1 changed files with 44 additions and 31 deletions

View File

@ -26,7 +26,7 @@
$.extend(settings,options); $.extend(settings,options);
$.each(this.children(),function(i,option) { $.each(this.children(),function(i,option) {
// If the option is selected, but not in the checked array, add it. // If the option is selected, but not in the checked array, add it.
if($(option).attr('selected') && settings.checked.indexOf($(option).val()) == -1){ if($(option).attr('selected') && settings.checked.indexOf($(option).val()) === -1) {
settings.checked.push($(option).val()); settings.checked.push($(option).val());
settings.labels.push($(option).text().trim()); settings.labels.push($(option).text().trim());
} }
@ -51,7 +51,6 @@
button.data('settings',settings); button.data('settings',settings);
if(!settings.singleSelect && settings.checked.length>0) { if(!settings.singleSelect && settings.checked.length>0) {
//button.children('span').first().text(settings.checked.join(', '));
button.children('span').first().text(settings.labels.join(', ')); button.children('span').first().text(settings.labels.join(', '));
} else if(settings.singleSelect) { } else if(settings.singleSelect) {
button.children('span').first().text(this.find(':selected').text()); button.children('span').first().text(this.find(':selected').text());
@ -103,7 +102,13 @@
input.attr('checked', true); input.attr('checked', true);
} }
if(checked){ if(checked){
if(settings.singleSelect) {
settings.checked = [item];
settings.labels = [item];
} else {
settings.checked.push(item); settings.checked.push(item);
settings.labels.push(item);
}
} }
input.change(function(){ input.change(function(){
var value = $(this).attr('id').substring(String('ms'+multiSelectId+'-option').length+1); var value = $(this).attr('id').substring(String('ms'+multiSelectId+'-option').length+1);
@ -184,10 +189,15 @@
if (exists) { if (exists) {
return false; return false;
} }
if(settings.singleSelect) {
$.each(select.find('option:selected'), function() {
$(this).removeAttr('selected');
});
}
var li=$(this).parent(); var li=$(this).parent();
$(this).remove(); $(this).remove();
li.text('+ '+settings.createText); li.text('+ '+settings.createText);
li.before(createItem(this, true)); li.before(createItem(this));
if(self.menuDirection === 'up') { if(self.menuDirection === 'up') {
var list = li.parent(); var list = li.parent();
list.css('top', list.position().top-li.outerHeight()); list.css('top', list.position().top-li.outerHeight());
@ -195,9 +205,12 @@
var select=button.parent().next(); var select=button.parent().next();
var option=$('<option selected="selected"/>'); var option=$('<option selected="selected"/>');
select.append(option); select.append(option);
option.text($(this).val()); option.text($(this).val()).val($(this).val()).attr('selected', 'selected');
li.prev().children('input').trigger('click'); li.prev().children('input').prop('checked', true).trigger('change');
button.parent().data('preventHide',false); button.parent().data('preventHide',false);
button.children('span').first().text(settings.labels.length > 0
? settings.labels.join(', ')
: settings.title);
if(typeof settings.createCallback === 'function') { if(typeof settings.createCallback === 'function') {
settings.createCallback($(this).val()); settings.createCallback($(this).val());
} }