如何使用ZendX_JQuery_Form_Element_AutoComplete类获得生成的javascript代码?
$item = new ZendX_JQuery_Form_Element_AutoComplete('item', array(
'label' => 'Item' ,
'id' => 'auto-item'));
$item->setJQueryParams(array(
'source' => $some URL ,
'minLength' => '1'));viewHelper生成的javascript将是:
$("#auto-item").autocomplete({"source":"some URL","minLength":"1"});最后一行是目标
发布于 2012-09-21 05:12:31
假设你想找一位顾客。您的源端点可以是控制器上的一个操作,可能专用于提供自动完成数据。
需要注意的主要问题是预期数据的格式:[{"id":123,"label":"Some Customer","value":"Some Customer (ID#123)"},...
“label”和“value”使您有机会显示下拉列表的备用字符串以及最终选择的内容。
下面是一个例子。
class Service_AutocompleteController extends Zend_Controller_Action
{
/**
* The query / q for the auto completion
*
* @var string
*/
protected $_term;
/**
* @see Zend_Controller_Action::init()
*/
public function init()
{
$this->_term = (isset($this->_request->term)) ? $this->_request->term : null;
}
/**
* Serve up JSON response for use by jQuery Autocomplete
*/
public function customerLookupAction()
{
// Disable the main layout renderer
$this->_helper->layout->disableLayout();
// Do not even attempt to render a view
$this->_helper->viewRenderer->setNoRender(true);
$query = "
SELECT TOP 50
c.id AS id,
c.name + '(ID#' + c.id + ')' AS label,
c.name AS value
FROM customer c
WHERE $label LIKE ?
ORDER BY c.name";
$db = Zend_Registry::get(SOME_DB_ADAPTER);
$results = $db->fetchAll($query, array('%' . $this->_term . '%'));
echo Zend_Json::encode($results);
}
}有时人们在这方面做得更进一步,并且有一个视图帮助器来发回JSON,您可以通过这种方式删除控制器操作中的一些重复代码。
发布于 2012-10-01 16:57:43
只需将以下代码放入您的视图文件:
$this->jQuery()->getOnLoadActions()它返回由jQuery帮助器生成的javaScript
https://stackoverflow.com/questions/12511283
复制相似问题