JqG​​rid + 自动完成

2022-01-19 00:00:00 php javascript jqgrid

我在 jqgrid 中实现自动完成时遇到问题.我一直在研究,直到我将这个问题建立在一个目前不满足的网站上.问题是,在我正在开发的应用程序中,我必须多次使用自动完成功能.现在我有了这个功能:

I'm having trouble implementing autocomplete in jqgrid. I've walked researching, alias until I based this question on a site that currently do not meet. The problem is this, I have to use the autocomplete several times throughout the application I'm developing. And now I have this function:

Javascript:

Javascript:

function autocomplete_element(value, options) {
    var $ac = $('<input type="text"/>');
    $ac.val(value);
    $ac.autocomplete({
    source: function(request, response) 
    {        
        $.getJSON("autocomplete.php?id=estrategico",
            { q: request.term }, response);
    }
    });
    return $ac;
}

Jqgrid:

jQuery("#obj_oper_org").jqGrid({
    (...)
        {name:'COD_OBJ_EST',index:'COD_OBJ_EST', hidden: true, editable:true, editrules:{required:true, edithidden:true}, edittype : 'custom', editoptions : {'custom_element' : autocomplete_element}},

更多的目的是将参数传递给 javascript 函数,以免每个字段永远重复相同的函数,因为我需要不断更改 url.有没有可能制作出这种类型的东西?很抱歉这个问题,但我在 javascript 方面没有太多经验,所以我遇到了一些困难

What was intended to pass a parameter to the javascript function more in order not to repeat forever the same function for each field because I need to be constantly changing url. Is it possible to make something of the genre? Sorry for the question but I do not have much experience in javascript, so I have some difficulties

推荐答案

首先你不需要使用 edittype : 'custom' 就能使用 jQuery UI Autocomplete.取而代之的是,您可以只使用 dataInit.

First of all you don't need to use edittype : 'custom' to be able to use jQuery UI Autocomplete. Instead of that you can use just dataInit.

您可以定义 myAutocomplete 功能,例如像

You can define myAutocomplete function for example like

function myAutocomplete(elem, url) {
    setTimeout(function () {
        $(elem).autocomplete({
            source: url,
            minLength: 2,
            select: function (event, ui) {
                $(elem).val(ui.item.value);
                $(elem).trigger('change');
            }
        });
    }, 50);
}

然后使用

{ name:'COD_OBJ_EST', hidden: true, editable: true,
    editoptions: {
        dataInit: function (elem) {
            myAutocomplete(elem, "autocomplete.php?id=estrategico");
        }
    }}

请注意,将发送到服务器的参数名称是标准名称term,而不是您当前使用的名称q.我个人认为不需要更改参数的默认名称.

Be careful that the name of parameter which will be send to the server is the standard name term instead of the name q which you currently use. I personally don't see any need to change the default name of the parameter.

相关文章