gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 06:19:23 24 4
gpt4 key购买 nike

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

索引 Controller .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;

}

最后是 Regions db 模型
public function getRegion($term)
{

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


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

}

现在,当我进入自动完成字段时,它会显示结果,但作为 UNDEFINED ,我认为这是将数据发送回 json 助手的方式。

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

[{"City":"London"},{"City":"Londonderry"},{"City":"Longfield"},{"City":"Longhope"},{"City":"Longniddry"} ]

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

干杯

J

最佳答案

ZendX_JQuery_Form_Element_AutoComplete element 是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com