JqGrid +自动完成 [英] JqGrid + Autocomplete
问题描述
我在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
函数例如
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.
这篇关于JqGrid +自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!