搜索中的AJAX验证

时间:2014-02-14 09:19:20

标签: yii yii-events

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

CONTROLLER行动:

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>

1 个答案:

答案 0 :(得分:0)

如果您需要像google一样搜索每个键按下并更新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中搜索该特定值并再次渲染视图。