Yii: How to force CGridview to load data via AJAX only?

yii

Solution

You could modify the action as follows:

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
  }

  [...]
}

And then in your view in the javascript section:

$(function() { // If you are using jQuery this is executed when the page is loaded
  $.fn.yiiGridView.update("{id-of-your-grid-view}");
});

Problem

Is there a way to make a CGridView Not load data on the initial rendering of the view it lives on, and instead make it load the first page with a subsequent AJAX request after the initial page load? This is mostly for performance optimization. There is a data model that is rather slow behind that CGridView, and I would like to be able to have the page load in a snappy way, then have the data load up a few seconds later with an AJAX request.

Original source