JQuery UI Autocomplete with Zend Framework

泄露秘密 提交于 2020-01-10 15:38:08

问题


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" />

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!


回答1:


this should work for you:

// 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;
}

You could altough pass the BaseUrl to your Script (instead of using fullpath)

//layout.phtml
<script type="text/javascript">
var baseUrl = "<?= $this->baseUrl(); ?>";
</script>

So you could do:

source: baseUrl + "/index/autocomplete"


来源:https://stackoverflow.com/questions/11002730/jquery-ui-autocomplete-with-zend-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!