0

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"});

最後の行がターゲットです

4

2 に答える 2

0

顧客を検索したいとします。ソースエンドポイントは、おそらくオートコンプリートデータの提供専用のコントローラーでのアクションである可能性があります。

注意すべき主なことは、期待されるデータの形式です。 [{"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-09-20T21:12:31.207 に答える
0

これをビューファイルに入れるだけです:

$this->jQuery()->getOnLoadActions()

jQueryヘルパーによって生成されたjavaScriptを返します

于 2012-10-01T08:57:42.573 に答える