alex_h
(2244008)
August 21, 2010, 12:55pm
1
Банальный пример:
PostController.php
<?php class PostController extends Controller {
public function actionView() {
if(!isset($_GET['ajax'])) {
if(isset($_GET['id'])){
$post = Post::model()->find('id = :id',array(':id' => $_GET['id']));
}
$this->render('index', array('model' => $post));
}
else
$this->renderPartial('_comments', array(
'dataProvider'=>CommentTours::model()->search(),
), false, true);
} ?>
К нему: view/index.php
[html]<div> <?php echo $model->content;?> </div>
<div> <?php $this->renderPartial(’_comments’, array( ‘dataProvider’=>CommentTours::model()->search(), ), false, true);?> </div>[/html]
и view/_comments.php
<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>Comment::model()->search(),
'ajaxVar'=> true,
'ajaxUpdate'=> true,
'columns'=>array(
'id',
array('name'=>'comment',
'type'=>'raw',),
),
)); ?>
Было желание получить в CGridView пагинацию по Ajax. Но isset($_GET[‘ajax’]) выдаёт “false” и (Yii::app()->request->isAjaxRequest - “false”.
Страницы листаются, но обновляется, естественно, вся страница. А хотелось бы - только CGridView.
В чём может быть проблема?
alex_h
(2244008)
August 25, 2010, 7:55am
3
Подключены! "Старый" сайт работает нормально.
Всё, что лежит "на поверхности" я испробовал.
Почему-то CGridView фомирует URL:
[html]http://localhost/index.php/post/3/sss_ss.html?Comment_page=3[/html]
а должно быть:
[html]http://localhost/index.php/post/3/sss_ss/ajax/comment-grid/Comment_page/2.html[/html]
В настройках urlManager:
'urlManager'=>array(
'urlFormat'=>'path',
'urlSuffix'=>'.html',
'rules'=>array(
'post/<id:\d+>/<title\w+>'=>'post/view',
'post/<id:\d+>'=>'post/view',
'post'=>'post/view',
), ),
Pahanium
(Pahanium)
August 31, 2010, 12:29pm
4
Возможно это связано с тем как в стандартной реализации это делаеться:
$model=new Comment('search');
if(isset($_GET['Comment']))
$model->attributes=$_GET['Comment'];
$this->render('admin',array(
'model'=>$model,
));
Попробуй передалать как то так:
else
$model=new Comment('search');
if(isset($_GET['Comment']))
$model->attributes=$_GET['Comment'];
$this->render('_comments', array(
'model'=>$model,
));
} ?>
И соответственно в представлении поменять на ‘dataProvider’=>$model->search(),
Хотя может все тупо банально поменять рендерПартиал на просто рендер?
seb
(Serebrov)
September 3, 2010, 8:02pm
5
<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>Comment::model()->search(),
'ajaxVar'=> true,
'ajaxUpdate'=> true,
'columns'=>array(
'id',
array('name'=>'comment',
'type'=>'raw',),
),
)); ?>
Возможно, это связано с тем, что не стоило трогать ajaxVar?
$ajaxVar;
the name of the GET variable that indicates the request is an AJAX request triggered by this widget.
-->> Defaults to 'ajax'.
This is effective only when ajaxUpdate is not false.
alex_h
(2244008)
September 6, 2010, 9:49am
6
Спасибо!
Ajax - запрос до контроллера не доходит!, его перехватывает CGridView. Убрал из контроллера обработку ajax.
Сам Ajax не работал из-за строки:
Comment::model()->count('post_id = :id',array(':id' => $_GET['id']);
убрал ‘:id’ => $_GET[‘id’], и всё заработало!
Хотя, странно это.