Domanda

I'm not very familiar with jquery, so don't be very strict. Task was to make order in Chosen multiselect plugin. Here is workaround:

$(function(){
        $("select#Books_id_author").each(function(){
            if($(this).data("order")==true){
                $(this).val('');
                $(this).chosen();
                var $ordered = $('[name="_'+jQuery(this).attr('id')+'"]');
                var selected = $ordered.val().split(',');
                var chosen_object = $(this).data('chosen');
                $.each(selected, function(index, item){
                    $.each(chosen_object.results_data, function(i, data){
                        if(data.value == item){
                            $("#"+data.dom_id).trigger('mouseup');
                        }
                    });
                });
                $(this).data("backupVal",$(this).val());
                $(this).change(function(){
                    var backup = $(this).data("backupVal");
                    var current = $(this).val();
                    if(backup == null){
                        backup = [];
                    }
                    if(current == null){
                        current = [];
                    }
                    if(backup.length > current.length){
                        $.each(backup, function(index, item) {
                            if($.inArray(item, current) < 0){
                                for(var i=0; i<selected.length; i++){
                                    if(selected[i] == item){
                                        selected.splice(i,1);
                                    }
                                }
                            }
                        });
                    }else if(backup.length < current.length){
                        $.each(current, function(index, item) {
                            if($.inArray(item, backup) < 0){
                                selected.push(item);
                            }
                        });
                    }
                    $ordered.val(selected.join(','));
                    $(this).data("backupVal",current);
                });
            }else{
                $(this).chosen();
            }
        });
    });

The problem is, that plugin active after page load (i suppose it's because trigger('mouseup')) and showing results drop.

Is there any trigger in Chosen to make it not active, or maybe something another. Thanks.

È stato utile?

Soluzione

Problem solved by writing order plugin for chosen.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top