2014-02-14 46 views
0

我是Yii的新手,我有兩張表。 master_customer,master_client。在客戶端模型中,在視圖中我有一個搜索框,如果我在搜索框中輸入幾個字母,它必須自動獲取數據並給出master_customers的結果(如谷歌建議)。我已經和master_customer在客戶端模型中建立了關係。 請幫助我的代碼。在此先感謝AJAX驗證搜索

控制器動作:

public function actionIndex() 
    { 

      $criteria = new CDbCriteria(); 
      if(isset($_GET['q'])) 
    { 
     $q = "%".$_GET['q']."%"; 
     $criteria->condition = 'cust_name='.$q; 
      $arrTier3 = MasterCustomers::model()->findAll($criteria); 
     //$criteria->compare(MasterCustomers::model()->cust_name,$q, true); 

     //$criteria->compare('$data->customers->cust_name', $q, true, 'OR'); 
     print_r($arrTier3); 
     die(); 

    } 

    $dataProvider=new CActiveDataProvider('Host', array('criteria'=>$criteria)); 

       $this->render('index',array(
      'dataProvider'=>$dataProvider, 


     )); 

    } 

觀點:

<!--Content--> 
<div id="content"> 
    <div style="padding: 10px;"> 
     <a href="<?php echo $this->createUrl('/Controller/create');?>" title="Create New Host" class="btn btn-primary circle_ok" style="text-decoration: none;" >Add New Host to Customer</a> 

    <div style="float:right"> 
         <?php 
          echo CHtml::link('Upload Customer CSV', array('/Controller/uploadCustomers'), array(
          'onclick'=>'return hs.htmlExpand(this, { objectType: "iframe", wrapperClassName: "full-size", align: "center" })', 
          'class'=>'btn btn-primary', 
          'id'=>'upload_link', 
          )); 
         ?>       
        </div> 
    </div> 
    <h3><?php echo $title; ?></h3> 


    <div class="innerLR"> 
     <div class="row-fluid"> 
<?php 
$obj=$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider, 

    //'afterAjaxUpdate'=>'\'changeTRColor()\'', 
    //'itemView'=>'_view', 
    'columns'=>array(

       array(   // display 'create_time' using an expression 
          'name'=>'name', 
              'value'=>'$data->host_name', 
       ), 
       array(
          'name'=>'serviceId', 
          'value'=>'$data->host_serviceid', 
       ), 

       array(
              'name'=>'customer', 
              'value'=>'$data->customers->cust_name', 
       ), 
       array(
            'class'=>'CButtonColumn', 
            'template'=>'{delete}{update}',) 



), 
)); 

?> 
     <form method="get"> 
<input type="search" placeholder="search" name="q" value="<?=isset($_GET['q']) ? CHtml::encode($_GET['q']) : '' ; ?>" /> 
<input type="submit" value="search" /> 
</form>  
     </div> 
     <div class="separator bottom"></div> 
    </div> 
</div> 
<!-- // Content END --> 
<div class="clearfix"></div> 
<!-- // Sidebar menu & content wrapper END --> 

<div id="footer" class="hidden-print"> 
<?php $this->renderPartial('application.views.layouts._footer_inc'); ?> 
</div> 
+0

你試過了什麼?顯示 –

回答

0

如果你需要像谷歌搜索,搜索每個按鍵和更新GridView控件,那麼你可以嘗試這樣的。在JavaScript編寫

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#id of your search box').keyup(function() { 

$.fn.yiiGridView.update('id of your grid to be updated', { 
     data: $(this).serialize() 
    }); 
}); 
    }); 
</script> 

在你的控制器檢查什麼價值它有$ _ POST變量,所以你可以抓住這個值和master_customers搜索特定值,再次渲染視圖。