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

发布于 2024-11-25 20:16:07 字数 1354 浏览 1 评论 0原文

您好,我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱格式化 2024-12-02 20:16:07

ZendX_JQuery_Form_Element_AutoComplete 元素是 AutoComplete View Helper 的代理,而 AutoComplete View Helper 又是 的代理jQuery UI 自动完成 小部件。

如果您阅读 jQuery UI 自动完成 页面上的概述,您会注意到:

本地数据可以是一个简单的字符串数组,或者它包含数组中每个项目的对象,具有标签或值属性或两者。标签属性显示在建议菜单中。当用户从菜单中选择某些内容后,该值将被插入到输入元素中。如果只指定一个属性,它将用于两个属性,例如。如果您仅提供值属性,则该值也将用作标签。

当使用字符串时,自动完成插件期望该字符串指向将返回 JSON 数据的 URL 资源。它可以位于同一主机或不同主机上(必须提供 JSONP)。请求参数“term”被添加到该 URL 中。 数据本身可以采用与上述本地数据相同的格式

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

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

祝你好运!

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:

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.

So, the JSON you are returning to the autocomplete should be structured more like:

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

Good luck!

单调的奢华 2024-12-02 20:16:07

经过数小时的痛苦后解决了问题!

我修改了下面的代码,它访问数据

public function getRegion($term)

{

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


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

}

我添加了一行 SQL City AS Value

public function getRegion($term)

{

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


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

}

这似乎有效!

干杯

J

Fixed the problem afters hours of pain!

I modified the below code where it accesses the data

public function getRegion($term)

{

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


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

}

I added a line of SQL City AS Value

public function getRegion($term)

{

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


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

}

This seems to have worked!

Cheers

J

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文