2012-07-18 51 views
0

如果只從默認的Yii ajax搜索中找到單個結果,我想重定向到它的視圖操作。我如何觸發重定向? (這類似於谷歌的「手氣不錯」選項)Yii默認ajax搜索重定向

+0

一些更多的細節將有助於回答,就像你嘗試過什麼?你在說什麼默認搜索? – 2012-07-18 07:02:16

+0

您將不得不編寫一個定製的JavaScript函數,它將使用來自服務器的結果更新CGridView,並且如果結果數等於1,則重定向頁面。這很容易,如果除了呈現的HTML,您還會發送結果數量和/或URL以重定向到。 – adamors 2012-07-18 09:54:08

回答

1

回答自己,

我endup使用javascript黑客怎麼我找不到一種合適的方式來做到這一點。

在我CGridView我用afterAjaxUpdate

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $dataProvider, 
    'summaryCssClass'=>'summary fr', 
    'id'=>'order-grid', 
    'afterAjaxUpdate'=>'function(id, options){ if($("#order-grid tbody tr").length == 1 && $("#order-grid tbody tr:first td").length > 1) {window.location = $("#order-grid tbody tr td:first a").attr("href");}}', 
    'columns' => array() 
)); 

希望這有助於給別人。

1
public function actionAdmin() { 
     $this->pageTitle = Yii::app()->name . ' - Customer Support'; 

     $model = new residence('customer'); 

     $model->unsetAttributes(); 

     if (isset($_GET['residence'])) 
      $model->attributes = $_GET['residence']; 
     if (!Yii::app()->request->isAjaxRequest && isset($_GET['search_field'])) { 
      $raw = $model->customer()->getData(); 
      if (count($raw) == 1) { 
       $this->redirect(array("customer/details", "id" => $raw[0]->id)); 
      } 
     } 

     $this->render('admin', array(
      'model' => $model, 
     )); 
    } 

其中$模型 - >客戶()返回dataProvider中電網也使用。