简体   繁体   English

jQgrid中的自动完成功能,其中包含Php页面返回的JSON数据

[英]Autocomplete in jQgrid with JSON data returned by a Php page

I was trying to implement autocomplete for a textbox which is generated by jQgrid. 我正在尝试为jQgrid生成的文本框实现自动完成功能。 A Php page would return JSON data. 一个Php页面将返回JSON数据。 Here's what I was able to do so far: (Please help) 到目前为止,这是我能够执行的操作:(请帮助)

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

?>

When I use data such as ["blah","hello","howdy"] in source of $ac.autocomplete, the thing seems to work nicely. 当我在$ ac.autocomplete的源代码中使用诸如[“ blah”,“ hello”,“ howdy”]之类的数据时,事情似乎运行良好。 But I have around 2000 rows of data to search from. 但是我有大约2000行数据可供搜索。 The jQgrid form is working correctly and I am being able to add & edit data. jQgrid表单运行正常,我能够添加和编辑数据。 Also, I have tested the php page which displays proper JSON data when I point a browser at it. 另外,我已经测试了php页面,当我将浏览器指向它时,该页面显示正确的JSON数据。 I am only struck with autocomplete with data returned from the php page since I am not much comfortable with jQuery. 由于我对jQuery不太满意,所以我对自动完成功能从php页面返回的数据感到惊讶。 Please help. 请帮忙。

Try adding exit() after the last echo in php code. 尝试在php代码中的最后一个回显之后添加exit()。 I hope this will fix. 我希望这会解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM