multiSelect.js: Give each select a unique msid to prevent double adding in users.js

This commit is contained in:
Thomas Tanghus 2012-12-04 21:21:54 +01:00
parent 95bdfdb319
commit 6d643f9e74
2 changed files with 21 additions and 12 deletions

View File

@ -1,5 +1,7 @@
/** /**
* @param 'createCallback' A function to be called when a new entry is created. Only argument to the function is the value of the option. * @param 'createCallback' A function to be called when a new entry is created. Two arguments are supplied to this function:
* The select element used and the value of the option. If the function returns false addition will be cancelled. If it returns
* anything else it will be used as the value of the newly added option.
* @param 'createText' The placeholder text for the create action. * @param 'createText' The placeholder text for the create action.
* @param 'title' The title to show if no options are selected. * @param 'title' The title to show if no options are selected.
* @param 'checked' An array containing values for options that should be checked. Any options which are already selected will be added to this array. * @param 'checked' An array containing values for options that should be checked. Any options which are already selected will be added to this array.
@ -23,6 +25,7 @@
'onuncheck':false, 'onuncheck':false,
'minWidth': 'default;', 'minWidth': 'default;',
}; };
$(this).attr('data-msid', multiSelectId);
$.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.
@ -189,30 +192,36 @@
if (exists) { if (exists) {
return false; return false;
} }
var li=$(this).parent();
var val = $(this).val()
var select=button.parent().next();
if(typeof settings.createCallback === 'function') {
var response = settings.createCallback(select, val);
if(response === false) {
return false;
} else if(typeof response !== 'undefined') {
val = response;
}
}
if(settings.singleSelect) { if(settings.singleSelect) {
$.each(select.find('option:selected'), function() { $.each(select.find('option:selected'), function() {
$(this).removeAttr('selected'); $(this).removeAttr('selected');
}); });
} }
var li=$(this).parent();
$(this).remove(); $(this).remove();
li.text('+ '+settings.createText); li.text('+ '+settings.createText);
li.before(createItem(this)); li.before(createItem(this));
if(self.menuDirection === 'up') {
var list = li.parent();
list.css('top', list.position().top-li.outerHeight());
}
var select=button.parent().next();
var option=$('<option selected="selected"/>'); var option=$('<option selected="selected"/>');
option.text($(this).val()).val(val).attr('selected', 'selected');
select.append(option); select.append(option);
option.text($(this).val()).val($(this).val()).attr('selected', 'selected');
li.prev().children('input').prop('checked', true).trigger('change'); 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 button.children('span').first().text(settings.labels.length > 0
? settings.labels.join(', ') ? settings.labels.join(', ')
: settings.title); : settings.title);
if(typeof settings.createCallback === 'function') { if(self.menuDirection === 'up') {
settings.createCallback($(this).val()); var list = li.parent();
list.css('top', list.position().top-li.outerHeight());
} }
} }
}); });

View File

@ -178,9 +178,9 @@ var UserList={
}else{ }else{
checkHandeler=false; checkHandeler=false;
} }
var addGroup = function(group) { var addGroup = function(select, group) {
$('select[multiple]').each(function(index, element) { $('select[multiple]').each(function(index, element) {
if ($(element).find('option[value="'+group +'"]').length == 0) { if ($(element).find('option[value="'+group +'"]').length === 0 && select.data('msid') !== $(element).data('msid')) {
$(element).append('<option value="'+group+'">'+group+'</option>'); $(element).append('<option value="'+group+'">'+group+'</option>');
} }
}) })