jQueryUI autocomplete not working with dialog and

2019-03-17 03:54发布

I ran into an interesting issue with jQueryUI autocomplete in a dialog box.

My dialog HTML looks like this:

<div id="copy_dialog">
    <table>
        <tbody>
            <tr>
                <th>Title:</th>
                <td><input type="text" class="title" name="title"></td>
            </tr>
            <tr>
                <th>Number:</th>
                <td><input type="text" name="number"></td>
            </tr>
        </tbody>
    </table>
</div>

When I run the jQueryUI autocomplete on the above HTML, it works perfect.

When I open it up using dialog

$('#copy').click(function()
{
    $('#copy_dialog').dialog({
        autoOpen: true,
        width: 500,
        modal: false,
        zIndex: 10000000,
        title: 'Duplicate',
        buttons: {
            'Cancel': function()
            {
                $(this).dialog('close');
            },
            'Save': function()
            {
                $(this).dialog('close');
            }
        }
    });

    return false;
});

Then in FireBug, I can see autocomplete is still working. It's requesting and receiving results, but I no longer see a list of options below the input field.

My thought is that this has something to do with the zIndex on the dialog box being much greater than what the autocomplete menu gives, but I don't know for sure. I'm still researching exact details of what's happening, but I'm hoping someone here will have some idea for me.

Edit I tried removing the zIndex from the dialog and my autocomplete starts showing up. Unfortunately, I need that zIndex value to get over the dreadfully high zIndex of the menubar, which I can't change (don't have access to that area of the code). So if there's a way to add a zIndex to the autocomplete, that would be fantastic; until then, I'll probably just remove the zIndex from the dialog and make sure it doesn't show up around the menubar area.

14条回答
smile是对你的礼貌
2楼-- · 2019-03-17 04:16

This link worked for me.

https://github.com/taitems/Aristo-jQuery-UI-Theme/issues/39

Am using jquery-ui-1.10.3.

I configured the autocomplete combobox inside the "open" event of the jquery dialog.

查看更多
神经病院院长
3楼-- · 2019-03-17 04:18

Through pursuing this problem myself I discovered that appendTo has to be set before the dialog is opened. The same seems to apply to setting (or modifying) the source property.

$("#mycontrol").autocomplete({appendTo:"#myDialog",source:[..some values]})
$("#mycontrol").autocomplete("option","source",[...some different values]) // works

// doesn't work if the lines above come after
$("#myDialog").dialog("open")

This might just be a byproduct of what dialog open does, or not correctly addressing the element. But the order things happen seems to matter.

查看更多
叛逆
4楼-- · 2019-03-17 04:21

When using jQuery UI 1.10, you should not mess around with z-indexes (http://jqueryui.com/upgrade-guide/1.10/#removed-stack-option). The appendTo option works, but will limit the display to the height of the dialog.

To fix it: make sure the autocomplete element is in the correct DOM order with: autocomplete.insertAfter(dialog.parent())

Example

 var autoComplete,
     dlg = $("#copy_dialog"),
     input = $(".title", dlg);

 // initialize autocomplete
 input.autocomplete({
     ...
 });

 // get reference to autocomplete element
 autoComplete = input.autocomplete("widget");

 // init the dialog containing the input field
 dlg.dialog({
      ...
 });

 // move the autocomplete element after the dialog in the DOM
 autoComplete.insertAfter(dlg.parent());

Update for z-index problem after dialog click

The z-index of the autocomplete seems to change after a click on the dialog (as reported by MatteoC). The workaround below seems to fix this:

See fiddle: https://jsfiddle.net/sv9L7cnr/

// initialize autocomplete
input.autocomplete({
    source: ...,
    open: function () {
        autoComplete.zIndex(dlg.zIndex()+1);
    }
});
查看更多
够拽才男人
5楼-- · 2019-03-17 04:22

Try setting the appendTo option to "#copy_dialog":

$(/** autocomplete-selector **/)
    .autocomplete("option", "appendTo", "#copy_dialog");

This option specifies which element the autocomplete menu is appended to. By appending the menu to the dialog, the menu should inherit the correct z-index.

查看更多
叛逆
6楼-- · 2019-03-17 04:26

Super simple solution. Increase the z-index for the autocomplete. When it is active I am pretty sure you want it on top :)

.ui-autocomplete {
 z-index: 2000;
}
查看更多
聊天终结者
7楼-- · 2019-03-17 04:29

user1357172's solution worked for me but in my opinion it needs two imprevements.

If appendTo is set to null, we can find the closest .ui-front element instead of .ui-dialog, because our autocomplete should be already attached to it. Then we should change the z-index only for related widget (related ul list) instead of changing all existing elements with class .ui-autocomplete.ui-front. We can find related widget by using elem.autocomplete('widget')

Solution:

elem.autocomplete({
    open: function(event, ui){
        var onTopElem = elem.closest('.ui-front');
        if(onTopElem.length > 0){
            var widget = elem.autocomplete('widget');
            widget.zIndex(onTopElem.zIndex() + 1);
        }
    }
});

BTW this solution works but it looks bit hacky, so it is not probably the best one.

查看更多
登录 后发表回答