顧客を検索したいとします。ソースエンドポイントは、おそらくオートコンプリートデータの提供専用のコントローラーでのアクションである可能性があります。
注意すべき主なことは、期待されるデータの形式です。
[{"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を送り返すためのビューヘルパーを持っています、あなたはその方法でコントローラーアクションで繰り返されるコードのいくつかを取り除くことができます。