multiSelect: Add sorting options.

This commit is contained in:
Thomas Tanghus 2012-12-21 06:18:10 +01:00
parent 689edc2534
commit c317da7814
1 changed files with 42 additions and 7 deletions

View File

@ -18,6 +18,8 @@
'createCallback':false, 'createCallback':false,
'createText':false, 'createText':false,
'singleSelect':false, 'singleSelect':false,
'selectedFirst':false,
'sort':true,
'title':this.attr('title'), 'title':this.attr('title'),
'checked':[], 'checked':[],
'labels':[], 'labels':[],
@ -133,6 +135,7 @@
} }
settings.checked.push(value); settings.checked.push(value);
settings.labels.push(label); settings.labels.push(label);
$(this).parent().addClass('checked');
} else { } else {
var index=settings.checked.indexOf(value); var index=settings.checked.indexOf(value);
element.attr('selected',null); element.attr('selected',null);
@ -142,6 +145,7 @@
return; return;
} }
} }
$(this).parent().removeClass('checked');
settings.checked.splice(index,1); settings.checked.splice(index,1);
settings.labels.splice(index,1); settings.labels.splice(index,1);
} }
@ -162,6 +166,9 @@
}); });
var li=$('<li></li>'); var li=$('<li></li>');
li.append(input).append(label); li.append(input).append(label);
if(input.is(':checked')) {
li.addClass('checked');
}
return li; return li;
} }
$.each(options,function(index,item){ $.each(options,function(index,item){
@ -169,7 +176,7 @@
}); });
button.parent().data('preventHide',false); button.parent().data('preventHide',false);
if(settings.createText){ if(settings.createText){
var li=$('<li>+ <em>'+settings.createText+'<em></li>'); var li=$('<li class="creator">+ <em>'+settings.createText+'<em></li>');
li.click(function(event){ li.click(function(event){
li.empty(); li.empty();
var input=$('<input class="new">'); var input=$('<input class="new">');
@ -237,20 +244,48 @@
}); });
list.append(li); list.append(li);
} }
var doSort = function(list, selector) {
var rows = list.find('li'+selector).get();
if(settings.sort) {
rows.sort(function(a, b) {
return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());
});
}
$.each(rows, function(index, row) {
list.append(row);
});
};
if(settings.sort && settings.selectedFirst) {
doSort(list, '.checked');
doSort(list, ':not(.checked)');
} else if(settings.sort && !settings.selectedFirst) {
doSort(list, '');
}
list.append(list.find('li.creator'));
var pos=button.position(); var pos=button.position();
if($(document).height() > (button.offset().top+button.outerHeight() + list.children().length * button.height()) if($(document).height() > (button.offset().top+button.outerHeight() + list.children().length * button.height())
|| $(document).height()/2 > pos.top || $(document).height()/2 > pos.top
) { ) {
list.css('top',pos.top+button.outerHeight()-5); list.css({
list.css('left',pos.left+3); top:pos.top+button.outerHeight()-5,
list.css('width',(button.outerWidth()-2)+'px'); left:pos.left+3,
width:(button.outerWidth()-2)+'px',
'max-height':($(document).height()-(button.offset().top+button.outerHeight()+10))+'px'
});
list.addClass('down'); list.addClass('down');
button.addClass('down'); button.addClass('down');
list.slideDown(); list.slideDown();
} else { } else {
list.css('top', pos.top - list.height()); list.css('max-height', $(document).height()-($(document).height()-(pos.top)+50)+'px');
list.css('left', pos.left+3); list.css({
list.css('width',(button.outerWidth()-2)+'px'); top:pos.top - list.height(),
left:pos.left+3,
width:(button.outerWidth()-2)+'px'
});
list.detach().insertBefore($(this)); list.detach().insertBefore($(this));
list.addClass('up'); list.addClass('up');
button.addClass('up'); button.addClass('up');