Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
596 views
in Technique[技术] by (71.8m points)

php - JqGrid + Autocomplete

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:

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}},

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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);
}

and then use

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...