[英]Autocomplete in jQgrid with JSON data returned by a Php page
我正在尝试为jQgrid生成的文本框实现自动完成功能。 一个Php页面将返回JSON数据。 到目前为止,这是我能够执行的操作:(请帮助)
function autocomplete_element(value, options) {
var $ac = $('<input type="text"/>');
$ac.val(value);
$ac.autocomplete({
source: function(request, response) {
$.getJSON("autocomplete.php", { q: request.term }, response);
}
});
return $ac;
}
function autocomplete_value(elem, op, value) {
if (op == "set") {
$(elem).val(value);
}
return $(elem).val();
}
$(function(){
$("#list").jqGrid({
url:'process1.php',
datatype: 'xml',
mtype: 'GET',
colNames:['Column Name'],
colModel :[
{name:'columnid', index:'columnid', width:50, edittype:'custom',
editoptions: {
custom_element : autocomplete_element,
custom_value : autocomplete_value
}
}
]
........
........
////////////////////////////////////////////////
/// THE PHP PAGE ////
////////////////////////////////////////////////
/*
autocomplete.php
*/
<?php
require_once("../dbconfig.php");
$term = trim(strip_tags($_REQUEST['q']));//retrieve the search term that autocomplete sends
$qstring = "SELECT description as value, id FROM test WHERE name LIKE '%".$term."%'";
$result = mysql_query($qstring);//query the database for entries containing the term
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))//loop through the retrieved values
{
$row['id']=(int)$row['id'];
$row['value']=htmlentities(stripslashes($row['value']));
$row_set[] = $row;//build an array
}
echo json_encode($row_set);//format the array into json data
?>
当我在$ ac.autocomplete的源代码中使用诸如[“ blah”,“ hello”,“ howdy”]之类的数据时,事情似乎运行良好。 但是我有大约2000行数据可供搜索。 jQgrid表单运行正常,我能够添加和编辑数据。 另外,我已经测试了php页面,当我将浏览器指向它时,该页面显示正确的JSON数据。 由于我对jQuery不太满意,所以我对自动完成功能从php页面返回的数据感到惊讶。 请帮忙。
尝试在php代码中的最后一个回显之后添加exit()。 我希望这会解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.