使用ajax更新CGridView无法正常工作

时间:2013-09-10 10:18:58

标签: javascript jquery ajax yii cgridview

我有一个使用CGridView的网格,当我尝试更新它时,我收到此错误。尝试解决这个问题的方法很长时间仍然无法使其工作。我不确定我错过了什么。

以下是我用来在点击按钮时强制网格刷新的内容。

$('#sessions .refreshdata').live('click', function(e) {
   e.preventDefault();
   $.fn.yiiGridView.update('sessions-grid');
}
);

错误来了,

TypeError: settings is undefined

$grid.addClass(settings.loadingClass);

我不知道这会触发到哪里。

enter image description here

编辑1,

我尝试设置进程输出为true然后它工作但它在同一页面中与我的UI和其他所需脚本混乱。

$this->getController()->renderPartial('sessionGrid', $params, false, true);

我开始获得yii的默认gridview及其样式。困惑为什么会发生这种情况。

排序在同一网格中也不起作用。当我点击列名的排序链接时,它会刷新页面并将我带到另一页。所以在这种情况下,Ajax完全不起作用。

编辑2,

以下方式也可以按照ANSWER 1

的建议进行工作
console.log('came in');
e.preventDefault();
$.fn.yiiGridView({ 'ajaxUpdate':['sessions-grid'] }); 
console.log('came out');

如何强制此网格刷新或更新?

编辑3,

当我使用正常方式时,jquery.yiigridview.js会出现错误。设置未定义 - 第203行.. $ grid.addClass(s​​ettings.loadingClass);

return this.each(function () {
var $form,
$grid = $(this),
id = $grid.attr('id'),
settings = gridSettings[id];
$grid.addClass(settings.loadingClass); 

编辑4,我在下面复制cgridview代码和操作。

<?php TK::registerCssFile('grid');?>
<div id="sessions">
<h4><?php echo Yii::t('activity', (isset($model->activity->name) ? $model->activity->name . ' Sessions'  : 'Sessions')) ?></h4>
<?php $this->beginContent('application.views.templates.form', array('options'=>array('id'=>'sessions-form'))); ?>
<?php echo CHtml::hiddenField('activities_id', $model->activities_id); ?>
<?php echo CHtml::hiddenField('source', $source); ?>
<?php
$this->widget('application.components.SGridView', array(
   'id'=>'sessions-grid',
   'dataProvider'=>$model->search(),
   'filter'=>$model,
   'emptyText'=>(!empty($model->activities_id)) ? 'No results found.' : 'Please select activity from top grid.',
   'showNew'=>(!empty($model->activities_id) &&  Yii::app()->user->checkAccess("piActivitySessionsCreate") && $model->activity->source === 'iJOB') ? true : false,
   'newButtonCss'=>'new dialog-link-session',
   'createUrl'=>Yii::app()->controller->createUrl("/activities/default/sessionCreate", array("activities_id"=>$model->activities_id)),
   //'showExport'=>true,
   'columns'=>array(
      /*
      array('type'=>'raw',
         'value'=>'CHtml::hiddenField("ActivitySession[$data->id]", false, array(\'value\'=>$data->id))',
         'htmlOptions'=>array('style'=>'width:0%;'),
         'headerHtmlOptions'=>array('style'=>'width:0%;')),
      */      
      array('name'=>'site_id',
         //'value'=>'CHtml::link($data->sites->code, "#", array("title"=>$data->sites->name))',
         //'value'=>'CHtml::label($data->sites->code, null, array("title"=>$data->sites->name))',
         'value'=>'$data->sites!=null ? CHtml::tag("div", array("title"=>$data->sites->name), $data->sites->code) : ""',
         'type'=>'raw',
         'htmlOptions'=>array('style'=>'width:16%;'),
         'headerHtmlOptions'=>array('style'=>'width:16%;')),
      array('name'=>'location',
         'htmlOptions'=>array('style'=>'width:30%;'),
         'headerHtmlOptions'=>array('style'=>'width:30%;')),
      array('name'=>'start_time',
         'value'=>'Yii::app()->dateFormatter->formatDateTime($data->start_time, "medium", "short")',
         'htmlOptions'=>array('style'=>'width:15%;'),
         'headerHtmlOptions'=>array('style'=>'width:15%;')),
      array('name'=>'end_time',
         'value'=>'Yii::app()->dateFormatter->formatDateTime($data->end_time, "medium", "short")',
         'htmlOptions'=>array('style'=>'width:15%;'),
         'headerHtmlOptions'=>array('style'=>'width:15%;')),
      /*
      array('name'=>'max_positions',
         'htmlOptions'=>array('style'=>'width:6%;'),
         'headerHtmlOptions'=>array('style'=>'width:6%;')),
      */
      array('name'=>'closed',
         'value'=>'$data->closed == "1" ? Yii::t("activity", "Yes") : Yii::t("activity", "No")',
         'filter'=>array("0"=>Yii::t("activity", "No"), "1"=>Yii::t("activity", "Yes")),
         'htmlOptions'=>array('style'=>'width:6%;text-align:left;'),
         'headerHtmlOptions'=>array('style'=>'width:6%;')),
      array('name'=>'registered',
         'type'=>'raw',
         'value'=>'$data->registeredCount . \'/\' . $data->max_positions',
         'filter'=>false,
         'sortable'=>true,
         'htmlOptions'=>array('style'=>'width:8%;text-align:right;padding-right:15px'),
         'headerHtmlOptions'=>array('style'=>'width:8%;')),
      array('class'=>'CButtonColumn',
         'template'=>'{users} {view} {update} {delete}',
         'header'=>Yii::t("activity","Actions"),
         'headerHtmlOptions'=>array('style'=>'width:10%;'),
         'htmlOptions'=>array('style'=>'width:10%;'),
         'buttons'=>array(
            'users'=>array(
               'visible'=>'(Yii::app()->user->checkAccess("piActivityJobseekerRead") || Yii::app()->user->checkAccess("piActivityJobseekerUpdate")) ? true : false',
               'url'=>'Yii::app()->controller->createUrl("/activities/participants/index", array("session_id"=>$data->id))',
               'imageUrl'=>Yii::app()->request->baseUrl.'/images/base/users.png',               
               'options'=>array(
                  //'class'=>'users dialog-link-session',
                  'title'=>Yii::t('activity', 'Participants'),
               ),
            ),
            'view'=>array(
               'visible'=>'(Yii::app()->user->checkAccess("piActivitySessionsRead") && 
                  !Yii::app()->user->checkAccess("piActivitySessionsUpdate") && $data->activity->source === "iJOB") ||
                  ($data->activity->source === "ESS") ? true : false',
               'url'=>'Yii::app()->controller->createUrl("/activities/default/sessionView", array("id"=>$data->id))',
               'options'=>array('class'=>'view dialog-link-session'),
            ),
            'update'=>array(
               'visible'=>'(Yii::app()->user->checkAccess("piActivitySessionsUpdate") && $data->activity->source === "iJOB") ? true : false',
               'url'=>'Yii::app()->controller->createUrl("/activities/default/sessionUpdate", array("id"=>$data->id))',
               'options'=>array('class'=>'update dialog-link-session'),
            ),
            'delete'=>array(
               'visible'=>'(Yii::app()->user->checkAccess("piActivitySessionsDelete") && $data->activity->source === "iJOB") ? true : false',
               'url'=>'Yii::app()->controller->createUrl("/activities/default/sessionDelete", array("id"=>$data->id))',
               'options'=>array('class'=>'delete dialog-link-session'),
            ),
         ),
      ),
      array('type'=>'raw',
         'value'=>'$data->registeredCount',
         'htmlOptions'=>array('style'=>'display:none;'),
         'headerHtmlOptions'=>array('style'=>'display:none;'),
      ),
   ),
));
?>
<?php $this->endContent(); ?>
</div>
<style type="text/css">
.grid-view-body
{
    height: 170px;
}
</style>

和行动......

public function run()
   {
      Yii::app()->clientScript->scriptMap['*.js'] = false;
      $activities_id = (TK::get('activities_id') ? TK::get('activities_id') : (TK::post('activities_id') ? TK::post('activities_id') : 0));
      $source = (TK::get('source') ? TK::get('source') : (TK::post('source') ? TK::post('source') : 'iJOB'));

      if ($activities_id === null)
        throw new CHttpException(500, Yii::t('activity', 'The requested activity does not exist.'));

      $model = new ActivitySession('search');
      $model->unsetAttributes();
      $model->activities_id = $activities_id;
      $model->closed = null;     

      if (isset($_GET['ActivitySession']))
         $model->attributes = $_GET['ActivitySession'];

      $params = array('model'=>$model, 'source'=>$source);

      if (Yii::app()->request->isAjaxRequest)
         $this->getController()->renderPartial('sessionGrid', $params, false, true);
      else
         $this->getController()->renderPartial('sessionGrid', $params, false, true);
   }

然后我有如下的gridview和刷新按钮......

enter image description here

在使porcessedOutput = true之后,刷新开始工作,但每次增加一个..所以刷新现在多次发生..下面是它现在发生了3次捕获。 enter image description here

我怎么能多次停止这种令人耳目一新的发生是我的问题。

1 个答案:

答案 0 :(得分:1)

只是一个想法,试试

$('#sessions .refreshdata').live('click', function(e) {
   e.preventDefault();
   $.fn.yiiGridView({ 'ajaxUpdate':['sessions-grid']  });
);

希望这会有所帮助

请你在这里展示cgridview。问题可能在于你如何设置