2013-04-14 49 views
1

我一直在尋找select2和yii,並設法通過json請求/響應加載數據。Yii select2 - 無法選擇返回的數據

我遇到的問題是當我嘗試選擇返回的數據條目時,我不能。

我哪裏錯了?由動作returnd的數據被JSON格式如在形式CustomerCode和名稱

的widget碼從控制器動作SearchCustomer

$this->widget('bootstrap.widgets.TbSelect2', array( 
    'asDropDownList' => false, 
    'name' => 'CustomerCode', 
    'options' => array(  
     'placeholder' => 'Type a Customer Code', 
       'minimumInputLength' => '2', 
     'width' => '40%', 
       'ajax' => array( 
           //'url'=> 'http://api.rottentomatoes.com/api/public/v1.0/movies.json', 
           'url'=> Yii::app()->getBaseUrl(true).'/customer/SearchCustomer', 
          'dataType' => 'jsonp', 
          'data' => 'js: function (term,page) { 
             return { 
             term: term, // Add all the query string elements here seperated by , 
             page_limit: 10, 
               }; 
                  }',   
          'results' => 'js: function (data,page) {return {results: data};}', 
          ), 
      'formatResult' => 'js:function(data){ 
           var markup = data.CustomerCode + " - "; 
           markup += data.Name; 
           return markup; 
          }', 
      'formatSelection' => 'js: function(data) { 
           return data.CustomerCode; 
          }', 

    ))); 

代碼剪斷

Yii::app()->clientScript->scriptMap['jquery.js'] = false; 
$this->renderJSON(Customer::model()->searchByCustomer($term)); 

renderJSON功能從基站控制器類

protected function renderJSON($data) 
    { 
     header('Content-type: application/json'); 
     echo $_GET['callback'] . "("; 
     echo CJSON::encode($data); 
     echo ")"; 


     foreach (Yii::app()->log->routes as $route) { 
      if($route instanceof CWebLogRoute) { 
       $route->enabled = false; // disable any weblogroutes 
      } 
     } 
     Yii::app()->end(); 
    } 

感謝此幫助

+1

愛....解決的問題是,格式化的數據必須包含一個id和文本值對...即CustoemrCode必須避開id並且Name必須爲文本評估。愚蠢的,但它的作品..... – Flashvn

回答