Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery autocomplete makes mCustomScrollbar scroll to top on select

I have implemented the mCustomScrollbar plugin in my backoffice interfaces. It works fine. But in one of my forms, I have a city field that needs the autocomplete.

The autocomplete also works fine. But when I select one of the item from autocomplete source data, the mCustomScrollbar plugin automatically brings me to the top of the scrolling content and I have to click a second time for the item to actually be selected.

This is how I implemented the scrollbar plugin :

$('#mainContent').mCustomScrollbar({
        set_height: height,
        scrollInertia: 500,
        scrollEasing: "easeInOutQuad",
        mouseWheel: 20,
        autoDraggerLength: true,
        advanced: {
            updateOnBrowserResize: true,
            updateOnContentResize: false
        }
    });

And this is how I implemented the autocomplete :

el.autocomplete({
    source: function (request, response) {
        $.ajax({
            url: activityAutocomplete,
            dataType: "json",
            data: request,
            success: function (data) {
                if (data.length == 0) {
                    data.push({
                        label: "Pas de résultat"
                    });
                }
                response(data);
            }
        });
    },
    //If overflow edge of window, the autocomplete flips to top of input
    position: { collision: "flip" },
    autofocus: true,
    delay: 150,
    minLength: 1,
    select: function (event, ui) {
        //ui.hide();
        //The following code resizes the input by bluring it.
        setTimeout(function () { el.autoGrowInput(); }, 50);


    },
    appendTo: '#autocomplete-tb-city-' + el.parents('.item').attr('id')
});

I hope you'll find something wrong here, i've been working on this for like 3 days !

EDIT: This is the generated autocomplete markup.

<ul class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all"   role="listbox" aria-activedescendant="ui-active-menuitem">
    <li class="ui-menu-item" role="menuitem"><a class="ui-corner-all" tabindex="-1">Angers</a</li>
    <li class="ui-menu-item" role="menuitem"><a class="ui-corner-all" tabindex="-1">Amiens</a</li>
</ul>

I must add something might be importante : it brings me to top even on RIGHT CLICK !!

Thank you.

like image 338
aztuk Avatar asked Oct 17 '12 13:10

aztuk


2 Answers

A new version of custom scrollbars have an advanced option autoScrollOnFocus.

Example:

        $($element).find('> .scrollbars').mCustomScrollbar({
            horizontalScroll: false,
            autoDraggerLength: true,
            autoHideScrollbar: true,
            advanced:{
                autoScrollOnFocus: false,
                updateOnContentResize: true,
                updateOnBrowserResize: true
            }
        });
like image 138
pro Avatar answered Oct 03 '22 09:10

pro


I was facing the same issue with autocomplete. Selecting any autocomplete items scrolls the window to the top.

I saw your comment here, and I think you got the solution.

Using hint you mentioned in the above link, I went through mcustomscrollbar.js code and just commented out lines 533 and 534 and yeppy it worked for me.

Thanks for the hint. Cheers !!

like image 38
varunvlalan Avatar answered Oct 03 '22 09:10

varunvlalan