0

我是 Yii 的新手,我有两张桌子。主客户,主客户。在客户端模型中,在视图中我有一个搜索框,如果我在搜索框中输入几个字母,它必须自动获取数据并从 master_customers 给出结果(如谷歌建议)。我在客户端模型中与 master_customer 建立了关系。请帮我写代码。提前致谢

控制器动作:

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>
4

1 回答 1

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 中搜索该特定值并再次呈现视图。

于 2014-02-14T17:07:30.513 回答