CGridView and CListView are great widget to populate records and also provides features like ajax update, column sort, search, drop-down filter, ajax content load and many more...
Both works great but has lankness in Ajax content load. When it request for next page content then Ajax comes with entire web page content and off-course executes all scripts before send output.
Not understanding Anh...., Don't worry see it in action by your self. If your are using any CGridView/CListView then click on any next page and see response using FireBug. There will be entire page content instead only grid/list view content.
Do Fix ¶
Lets go to apply some tricks to prevent entire page content output and script execution...
Step 1: Create a partial view file for CGridView or CListView ¶
Create Partial File: ___grid_view_forsubscriber.php
$this->widget('zii.widgets.grid.CGridView', array(
'id' => $grid_id,
'dataProvider' => $subscriberActiveDataProvider,
'columns' => array(
'first_name',
'last_name',
'designation',
'company',
'subscribed_on',
),
));
Step 2: Main view file ¶
Create View File: subscribers_list.php
/* @var $this SubscriberController */
$this->pageTitle = 'Subscribers';
// Render partial file created in Step 1
$this->renderPartial('__grid_view_for_subscriber', array(
'subscriberActiveDataProvider' => $subscriberActiveDataProvider,
'grid_id' => $grid_id,
));
Step 3: Controller Class ¶
Controller Class: SubscriberController.php
class SubscriberController extends Controller {
//
public function actionList() {
$criteria = new CDbCriteria();
// Add your Query condition here
// $criteria = 'your condition here';
// Create `activeDataProvider` object for Subscriber model
// NOTE: Subscriber model is not included in this article
// Replace model name with your's
$subscriberActiveDataProvider = new CActiveDataProvider('Subscriber', array(
'criteria' => $criteria,
));
$grid_id = 'subscriber_grid';
// Check is for AJAX request by `subscriber_grid`
// ajax response only grid content instead entire web page
if(Yii::app()->request->isAjaxRequest && isset($_GET['ajax']) && $_GET['ajax'] === $grid_id)) {
// Render partial file created in Step 1
$this->renderPartial('__grid_view_for_subscriber', array(
'subscriberActiveDataProvider' => $subscriberActiveDataProvider,
'grid_id' => $grid_id,
));
Yii::app()->end();
}
// Render main view file created in Step 2
$this->render('subscribers_list', array(
'subscriberActiveDataProvider' => $subscriberActiveDataProvider,
'grid_id' => $grid_id,
));
}
//
}
: THANKS :
Interesting
So to keep the response to the ajax requests lean, you move the parts needed for the ajax call out to a separate file so that it can be called directly with render partial from the controller.
I guess it is worth while to study the output of the ajax call before the re-factoring to determine how much can be gained from this. For a complex web the gain could be substantial, but for a simple web it may not be worthwhile.
Very good tip
Thank you!
Another approach
Hi,
Thanks for your suggestion.
Perhaps another suitable approach could involve changing your view file in the following way, avoiding any code duplication:
<?php if(!Yii::app()->request->isAjaxRequest): ?> // Content not to be show on ajax requests ... <?php endif; ?> // Content to be shown on every request ...
@cgabbanini
I guess that is a matter of taste. There isn't really any code duplication in the original solution, since the main view file just includes the partial view.
I am using this method to avoid rendering whole page.
Avoiding rendering entire page when using CGridView and CListView via AJAX
best
Thank You!
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.