yii:如何ajax更新cgridview [英] yii : how to ajax update the cgridview
问题描述
状态:我已完成cookie。但有一个问题。它是在页面上传时,日期始终显示在最新和最新的文本框中。
这是我的看法
<?php $ form = $ this-> beginWidget('CActiveForm',array(
'id'=>'page-form',
'enableAjaxValidation'= > true,
)); ?>
< div style =margin-top:30px;>
< b>发件人:< / b>
<?php
$ this-> widget('zii.widgets.jui.CJuiDatePicker',array(
'name'=>'from_date',//发布的名称参数
//'value'=> Yii :: app() - > request-> cookies ['from_date'] - > value,// value在提交后来自cookie
' options'=> array(
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
),
' htmlOptions'=>数组(
'style'=>'height:20px;'
),
));
?>
< span>& nbsp;& nbsp;& nbsp;& nbsp;< / span>
< b>收件人:< / 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 echocreate;?> class =btn btn-info>新付款接收< / 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'=>'acc_recei_id',
'header'=>' ($ data->方法))?CHtml :: encode($ data->方法):''',
),
array(
'name'=>'date',
'value'=>'($ data-> date = 0)?:date( d'yy,strtotime($ data-> date))',
),
数组('name'=>'方法,
'value'=>'(isset($ data-> method))?CHtml :: encode($ data-> method):'',
),
数组('name'=> 'description',
//'value'=>'(isset($ data-> method))? CHtml :: encode($ data->方法):',
),
/ *
'created_date',
'updated_date',
' file_name',
* /
array(
'class'=>'CButtonColumn',
),
),
));
这是控制器
public function actionIndex()
{
$ accpaymentrecei = new AccPaymentRecei('search');
$ accpaymentrecei-> unsetAttributes(); //清除任何默认值
if(isset($ _ GET ['AccPaymentRecei']))
$ accpaymentrecei-> attributes = $ _ GET ['AccPaymentRecei'];
$ acccategory = AccCategory :: model() - > findAll();
$ arr_method = array('Cash'=>'Cash','Check'=>'Check','Credit Card'=>'Credit Card','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'] ); //首先为日期取消设置cookie
unset(Yii :: app() - > request-> cookies ['to_date']);
// $ model = new XyzModel('search'); //你的模型
$ model-> unsetAttributes(); //清除任何默认值
if(!empty($ _ POST))
{
Yii :: app() - > request-> cookies ['from_date' ] = new CHttpCookie('from_date',$ _POST ['from_date']); //定义来自from_date $ b $的cookie cookie 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));
$ div $解析方案
下面 - 这只是例子,解决方案的主要原则。第一个原则:我们不需要刷新页面(通过提交),我们只需要在请求后刷新CGridView。
解决方案有几个部分:
-
我们需要带有一些参数的表单。在你的情况 - fromDate 和 toDate 。你已经有了这个。但我们不需要任何提交元素。根本!我们将使用AJAX。
-
我们需要一些启动AJAX请求的函数。例如,让我们针对此请求的事件将<参数> from_date *或* to_date *的更改。此功能也必须在请求后更新CGridView。
-
我们需要在控制器中处理AJAX请求。
We need form with some parameters. In your case - fromDate and toDate. You have this one already. But we don't need any submit element. At all! We will use AJAX.
We need some function for initiate AJAX-request. For example, let's event for this request will be change of parameters *from_date* or *to_date*. This function also must update CGridView after request.
We need action in controller for processing AJAX-request.
让我们从第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(
//这是CGridView $ b $'acc-payment-recei-grid',
{data:ajaxRequest}
)
}的id,
//这是延迟
300);
});
);
将此代码放入视图中。注意:
$(this).serialize()
- 关键时刻。寻找更多信息
selector_for_parameters_fields
- 给他们( fromDate 和 toDate )一些独特的
选择器
我们使用一些延迟
(300毫秒)来减少不必要的AJAX请求中的加载数据。
现在 - 关于Controller中的操作
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,));
}
它必须有效,但需要一些调整具体情况。
Q : How to ajax update the CgridVeiw?
Status : I've done with cookie. but there are a problem. it is when the page upload, the date are always show in from-date and to-date text box. and When reload the page, it doesn't clear out.
This is my view
<?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> </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> </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',
),
),
));
This is controller
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"));
}
}
Code below - it's only example, main principles of solution. First principle: we don't need refresh page (by the submit), we need only refresh CGridView after request.
The solution has a few parts:
Let's begin from part 2. It may be some like this:
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); });" );
Place this code into view. Notes:
$(this).serialize()
- key moment. Look for more info
selector_for_parameters_fields
- give them ( fromDate and toDate ) some unique
selector
We use some delay
(300 ms) for decreasing loading from unnecessary AJAX-requests
Now - about action in Controller
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,)); }
It must works, but requires some tuning for concrete conditions.
这篇关于yii:如何ajax更新cgridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!