json - Zend Jquery 自动完成从数据库填充

标签 json zend-framework jquery-autocomplete

您好,我正在尝试使用 Zend Jquery 实现自动完成字段。我按照教程从数组中获取数据,并且扩展了代码以访问 mysql 表中的数据。

IndexController.php

$this->view->autocompleteElement = new ZendX_JQuery_Form_Element_AutoComplete('ac');
    $this->view->autocompleteElement->setLabel('Autocomplete');
    $this->view->autocompleteElement->setJQueryParam('source', '/index/city');

这会调用 cityAction()

public function cityAction()
{
    $results = Application_Model_City::search($this->_getParam('term'));
    $this->_helper->json(array_values($results));        
}

然后我调用模型城市

public static function search($term)
{
   $region = new Application_Model_DbTable_Regions();

   $results = $region->getRegion($term);

   return $results;

}

最后是区域数据库模型

public function getRegion($term)
{

    $select = $this->select()->from($this,'City')
                                 ->where('City LIKE ? ',$term.'%');


    return $this->fetchAll($select)->toArray();

}

现在,当我进入自动完成字段时,它会显示结果,但作为 UNDEFINED ,我认为它可以按照我将数据发送回 json 帮助器的方式进行。

我使用了firebug,我可以看到数据是以以下格式提取的。

[{"城市":"伦敦"},{"城市":"伦敦德里"},{"城市":"朗菲尔德"},{"城市":"朗霍普"},{"城市":"朗尼德里"}]

我认为这种格式不正确,请问之前有处理过这个问题的机构吗?

干杯

J

最佳答案

ZendX_JQuery_Form_Element_AutoComplete 元素是 AutoComplete View Helper 的代理,而 AutoComplete View Helper 又是 jQuery UI Autocomplete 的代理。小部件。

如果您阅读 jQuery UI Autocomplete 上的概述页面,您会注意到:

The local data can be a simple Array of Strings, or it contains Objects for each item in the array, with either a label or value property or both. The label property is displayed in the suggestion menu. The value will be inserted into the input element after the user selected something from the menu. If just one property is specified, it will be used for both, eg. if you provide only value-properties, the value will also be used as the label.

When a String is used, the Autocomplete plugin expects that string to point to a URL resource that will return JSON data. It can be on the same host or on a different one (must provide JSONP). The request parameter "term" gets added to that URL. The data itself can be in the same format as the local data described above.

因此,您返回到自动完成功能的 JSON 的结构应该更像:

[{"label":"London","value":"London"},{"label":"Londonderry","value":"Londonderry"},{"label":"Longfield","value":"Longfield"}]

祝你好运!

关于json - Zend Jquery 自动完成从数据库填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6791720/

相关文章:

javascript - 如何在隐藏输入字段(类型 ="text")上使用 jQuery.autocomplete ?

php - 使用 PHP 回显 JSON 响应

json - 将 json 传递到 AWS CLI 无效的 JSON

python - 将 json 数据集转换为 pandas 数据框

zend-framework - Zend Lucene - 标记瑞典字符

javascript - 使用 jQuery Autocomplete 动态加载内容

json - 为什么 Angular Universal Server Side Rendering 在部署到 Firebase Hosting 后会出错?

zend-framework - 可以在不重定向的情况下使用 FlashMessenger 吗?

php - phpunit 有默认位置吗?或者当我想使用它时,我是否必须继续在命令行中导出路径?

javascript - 使用 Jquery 自动完成处理 "No results"