【问题标题】:yii : how to ajax update the cgridviewyii:如何通过 ajax 更新 cgridview
【发布时间】:2012-09-06 04:00:45
【问题描述】:

问:如何通过 ajax 更新 CgridVeiw?

状态:我已经完成了 cookie。但是有一个问题。在页面上传时,日期总是显示在从日期和到日期文本框中。并且当重新加载页面时,它并没有清除。

这是我的看法

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'page-form',
    'enableAjaxValidation'=>true,
)); ?>
<div style="margin-top:30px;">


    <b>From :</b>
    <?php
    $this->widget('zii.widgets.jui.CJuiDatePicker', array(
        'name'=>'from_date',  // name of post parameter
        //'value'=>Yii::app()->request->cookies['from_date']->value,  // value comes from cookie after submittion
         'options'=>array(
            'showAnim'=>'fold',
            'dateFormat'=>'yy-mm-dd',
        ),
        'htmlOptions'=>array(
            'style'=>'height:20px;'
        ),
    ));
    ?>
    <span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
    <b>To :</b>
    <?php
    $this->widget('zii.widgets.jui.CJuiDatePicker', array(
        'name'=>'to_date',
        //'value'=>Yii::app()->request->cookies['to_date']->value,
         'options'=>array(
            'showAnim'=>'fold',
            'dateFormat'=>'yy-mm-dd',

        ),
        'htmlOptions'=>array(
            'style'=>'height:20px;'
        ),
    ));
    ?>
    <span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
    <?php echo CHtml::submitButton('Go'); ?>
    <?php
        echo CHtml::ajaxSubmitButton('Search', CHtml::normalizeUrl(array('index')),
                array(  
                    'success'=>'js:'
                    .'function(){'
                    .'$.fn.yiiGridView.update("acc-payment-recei-grid", {url:"index"});'                    
                    .'}',
                ),
                array('id'=>'go', 'name'=>'go'));

    ?>
    <?php $this->endWidget(); ?>


    <p style="float:right;">
        <a href="<?php echo "create"; ?>" class="btn btn-info">New Payment Receive</a>
    </p>
</div>
<style>
.items table tr:last-child td:first-child {
    -moz-border-radius-bottomleft:10px;
    -webkit-border-bottom-left-radius:10px;
    border-bottom-left-radius:10px
}

.items table tr:last-child td:last-child {
    -moz-border-radius-bottomright:10px;
    -webkit-border-bottom-right-radius:10px;
    border-bottom-right-radius:10px
}

</style>
<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'acc-payment-recei-grid',
    'dataProvider'=>$accpaymentrecei->search(),
    //'filter'=>$accpaymentrecei,
    'columns'=>array(
        //'id',
        array('name' => 'acc_category_id',
               'value'=>'(isset($data->acccategories->name)) ? CHtml::encode($data->acccategories->name) :""',
        ),
        array('name' => 'acc_recei_id',
              'header'=> 'Account Received',
               //'value'=>'(isset($data->method)) ? CHtml::encode($data->method) :""',
        ),
        array(
            'name' => 'date',
            'value'=>'($data->date= 0) ? "" : date("d M yy",strtotime($data->date))',
        ),                
        array('name' => 'method',
               'value'=>'(isset($data->method)) ? CHtml::encode($data->method) :""',
        ),
        array('name' => 'description',
               //'value'=>'(isset($data->method)) ? CHtml::encode($data->method) :""',
        ),
        /*
        'created_date',
        'updated_date',
        'file_name',
        */
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); 

这是控制器

public function actionIndex()
{
    $accpaymentrecei=new AccPaymentRecei('search');
    $accpaymentrecei->unsetAttributes();  // clear any default values

    if(isset($_GET['AccPaymentRecei']))
        $accpaymentrecei->attributes=$_GET['AccPaymentRecei'];



    $acccategory = AccCategory::model()->findAll();

    $arr_method = array('Cash'=>'Cash', 'Cheque'=>'Cheque', 'Credit Card'=>'Credit Card', 'Bank Transfer'=>'Bank Transfer');


    $this->from_to_date($accpaymentrecei);
    //exit();
    $this->render('index',array(
        'accpaymentrecei'=>$accpaymentrecei,
        'acccategory'=>$acccategory,
        'arr_method'=>$arr_method,
    ));
}

protected function from_to_date($model)
{
    unset(Yii::app()->request->cookies['from_date']);  // first unset cookie for dates
    unset(Yii::app()->request->cookies['to_date']);

    //$model=new XyzModel('search');  // your model

    $model->unsetAttributes();  // clear any default values

    if(!empty($_POST))
    {
        Yii::app()->request->cookies['from_date'] = new CHttpCookie('from_date', $_POST['from_date']);  // define cookie for from_date
        Yii::app()->request->cookies['to_date'] = new CHttpCookie('to_date', $_POST['to_date']);
        $model->from_date = $_POST['from_date'];
        $model->to_date = $_POST['to_date'];
    }else{
        Yii::app()->request->cookies['from_date'] = new CHttpCookie('from_date', date("Y/m/d"));
        Yii::app()->request->cookies['to_date'] = new CHttpCookie('to_date', date("Y/m/d"));    
    }
}

【问题讨论】:

    标签: php gridview yii


    【解决方案1】:

    下面的代码 - 它只是示例,解决方案的主要原则。第一个原则:我们不需要刷新页面(通过提交),我们只需要在请求后刷新CGridView。

    解决方案有几个部分:

    1. 我们需要带有一些参数的表单。在您的情况下 - fromDate 和 toDate。你已经有了这个。但是我们不需要任何提交元素。一点都不!我们将使用 AJAX。

    2. 我们需要一些函数来发起 AJAX 请求。例如,此请求的 let's 事件将是参数 *from_date* 或 *to_date* 的更改。此函数还必须在请求后更新 CGridView。

    3. 我们需要控制器中的操作来处理 AJAX 请求。

    让我们从第 2 部分开始。可能是这样的:

    Yii::app()->clientScript->registerScript('scriptId',
        "var ajaxUpdateTimeout;
        var ajaxRequest;
        $('**selector_for_parameters_fields**').change(function(){  
            ajaxRequest = $(this).serialize();
            clearTimeout(ajaxUpdateTimeout);
            ajaxUpdateTimeout = setTimeout(function () {
                $.fn.yiiGridView.update(
    // this is the id of the CGridView
                    'acc-payment-recei-grid',
                    {data: ajaxRequest}
                )
            },
    // this is the delay
            300);
        });"
    );
    

    将此代码置于视图中。备注:

    $(this).serialize() - 关键时刻。寻找more info

    selector_for_parameters_fields - 给他们(fromDate 和 toDate)一些独特的 选择器

    我们使用一些 delay (300 ms) 来减少不必要的 AJAX 请求的加载

    现在 - 关于控制器中的操作

    public function actionIndex($fromDate='', $toDate='')       
    {
      $criteria = new CDbCriteria();   
      if(!empty($fromDate))
        $criteria->addSearchCondition('your search condition about fromDate');    
      if(!empty($toDate))
        $criteria->addSearchCondition('your search condition about toDate');                 
       $dataProvider=new CActiveDataProvider('YourModel',
            array('criteria' => $criteria,));           
       $this->render('YourView',array('dataProvider'=>$dataProvider,)); 
     }
    

    它必须有效,但需要针对具体条件进行一些调整。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      • 1970-01-01
      相关资源
      最近更新 更多