4

有没有办法让 CGridView在它所在的视图的初始渲染中加载数据,而是让它在初始页面加载后加载带有后续 AJAX 请求的第一页?

这主要是为了性能优化。在 CGridView 后面有一个相当慢的数据模型,我希望能够以一种快速的方式加载页面,然后在几秒钟后通过 AJAX 请求加载数据。

4

3 回答 3

4

您可以按如下方式修改操作:

public function actionIndex() {
  $dataProvider = new CActiveDataProvider('User'); // The dataprovider your grid uses
  if (!$this->isAjaxRequest()) {
    $dataProvider->criteria->addCondition('1 = 0'); // You could also use 0, but I think this is more clear
  }

  [...]
}

然后在 javascript 部分的视图中:

$(function() { // If you are using jQuery this is executed when the page is loaded
  $.fn.yiiGridView.update("{id-of-your-grid-view}");
});
于 2013-03-16T04:51:22.860 回答
0

Brewer Gorge 非常接近,多亏了他建议的答案,这让我走上了正轨。这有效:

// Controller, after creating $dataProvider, before calling $this->render...
if (!Yii::app()->request->isAjaxRequest) {
    $dataProvider->criteria->addCondition('1 = 0');
}

// View
<script type="text/javascript">
$(window).load(function() {
    $('#id-of-grid').yiiGridView('update');
});
</script>
于 2013-09-07T18:07:22.817 回答
0

只需像这样在控制器中编写代码:

$model= new Data('search');
$model->unsetAttributes();

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

if (!Yii::app()->request->isAjaxRequest)
   $data->id=0; //or something that sure model return empty
于 2014-10-09T01:42:34.437 回答