2012-07-18 48 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中电网也使用。