Zend Jquery 自动完成从数据库填充
您好,我正在尝试使用 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));
}
然后我调用了 Model City
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":"伦敦"},{"City":"伦敦德里"},{"City":"朗菲尔德"},{"City":"朗霍普"},{"City":"朗尼德里"} ]
我认为这种格式不正确,请问有人处理过这个问题吗?
干杯
J
Hi I am trying to implement a autocomplete field using Zend Jquery. I followed a tutorial to grab the data from an array and I have extended the code to access the data from my mysql table.
IndexController.php
$this->view->autocompleteElement = new ZendX_JQuery_Form_Element_AutoComplete('ac');
$this->view->autocompleteElement->setLabel('Autocomplete');
$this->view->autocompleteElement->setJQueryParam('source', '/index/city');
This calls the cityAction()
public function cityAction()
{
$results = Application_Model_City::search($this->_getParam('term'));
$this->_helper->json(array_values($results));
}
I then call the Model City
public static function search($term)
{
$region = new Application_Model_DbTable_Regions();
$results = $region->getRegion($term);
return $results;
}
And finally the Regions db model
public function getRegion($term)
{
$select = $this->select()->from($this,'City')
->where('City LIKE ? ',$term.'%');
return $this->fetchAll($select)->toArray();
}
Now when I go the autocomplete field it shows the results but as UNDEFINED , I think its something to do the way I am send the data back to the json helper.
I used firebug and I can see the data is been pulled in the following format.
[{"City":"London"},{"City":"Londonderry"},{"City":"Longfield"},{"City":"Longhope"},{"City":"Longniddry"}]
I think this format is incorrect, please any body dealt with this before?
Cheers
J
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ZendX_JQuery_Form_Element_AutoComplete
元素是 AutoComplete View Helper 的代理,而 AutoComplete View Helper 又是 的代理jQuery UI 自动完成 小部件。如果您阅读 jQuery UI 自动完成 页面上的概述,您会注意到:
因此,您返回到自动完成功能的 JSON 的结构应该更像是:
祝你好运!
The
ZendX_JQuery_Form_Element_AutoComplete
element is a proxy to the AutoComplete View Helper, which is a proxy to the jQuery UI Autocomplete widget.If you read the overview on the jQuery UI Autocomplete page, you will note:
So, the JSON you are returning to the autocomplete should be structured more like:
Good luck!
经过数小时的痛苦后解决了问题!
我修改了下面的代码,它访问数据
{
}
我添加了一行 SQL City AS Value
{
}
这似乎有效!
干杯
J
Fixed the problem afters hours of pain!
I modified the below code where it accesses the data
{
}
I added a line of SQL City AS Value
{
}
This seems to have worked!
Cheers
J