使用 Zend 框架的 JQuery UI 自动完成 [英] JQuery UI Autocomplete with Zend Framework
问题描述
我想知道如何在不使用 ZendX 的情况下将自动完成的 JQuery UI 小部件添加到我在 Zend 框架中开发的表单中.网站的文件夹是根据框架设置的,但我没有使用 Zend_Form.
I was wondering about how to add the autocomplete JQuery UI widget to a form I'm developing in the Zend Framework without using ZendX. The folders for the website are set up per the framework, but I'm not using Zend_Form.
所以我把所有东西都简化为最简单的形式,它有效:
So I stripped everything down to the simplest form, which works:
<script>
$(document).ready(function() {
$("input#autocomplete").autocomplete({
source: ["best", "buy"]
});
});
</script>
<input id="autocomplete" />
但我有一个 PHP 文件,它以 JSON 格式返回数据库中的条目.我该如何使用它?我尝试用文件名替换数组,但没有任何反应.谢谢!
But I have a PHP file that returns entries from a database in JSON. How do I use that instead? I tried replacing the array with the name of the file, but then nothing happens. Thanks!
推荐答案
这应该适合你:
// js stuff
$( "input#autocomplete" ).autocomplete({
source: "http://localhost/application/index/autocomplete"
});
//IndexController.php
/**
* Return AutoComplete stuff
*/
public function autocompleteAction()
{
// disable view and layout, we want some fanzy json returned
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$values = array('best', 'buy');
$valuesJson = Zend_Json::encode($values);
echo $valuesJson;
}
您可以将 BaseUrl 传递给您的脚本(而不是使用完整路径)
You could altough pass the BaseUrl to your Script (instead of using fullpath)
//layout.phtml
<script type="text/javascript">
var baseUrl = "<?= $this->baseUrl(); ?>";
</script>
所以你可以这样做:
source: baseUrl + "/index/autocomplete"
这篇关于使用 Zend 框架的 JQuery UI 自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!